kmedoids/
initialization.rs

1/// Random initialization (requires the `rand` crate)
2///
3/// This is simply a call to `rand::seq::index::sample`.
4///
5/// * `n` - size of the data set
6/// * `k` - number of clusters to find
7/// * `rng` - random number generator
8///
9/// returns a vector of medoid indexes in 0..n-1
10///
11/// ## Example
12///
13/// Given a dissimilarity matrix of size n x n, use:
14/// ```
15/// let mut meds = kmedoids::random_initialization(10, 2, &mut rand::thread_rng());
16/// println!("Chosen medoids: {:?}", meds);
17/// ```
18#[cfg(feature = "rand")]
19#[inline]
20pub fn random_initialization(n: usize, k: usize, rng: &mut impl rand::Rng) -> Vec<usize> {
21	rand::seq::index::sample(rng, n, k).into_vec()
22}
23
24/// Use the first objects as initial medoids.
25///
26/// * `k` - number of clusters to find
27///
28/// returns 0..k-1 as initial medoids
29///
30/// ## Example
31///
32/// Given a dissimilarity matrix of size n x n, use:
33/// ```
34/// let mut meds = kmedoids::first_k(2);
35/// println!("Chosen medoids: {:?}", meds);
36/// ```
37#[inline]
38pub fn first_k(k: usize) -> Vec<usize> {
39	(0..k).collect()
40}