Function kmedoids::alternating[][src]

pub fn alternating<M, N>(
    mat: &M,
    mut med: &mut Vec<usize>,
    maxiter: usize
) -> (N, Vec<usize>, usize) where
    N: NumAssignOps + Signed + Zero + PartialOrd + Copy + SafeAdd + Display,
    M: ArrayAdapter<N>, 

Run the Alternating algorithm, a k-means-style alternate optimization.

This is fairly fast (also O(n²) as the FasterPAM method), but because the newly chosen medoid must cover the entire existing cluster, it tends to get stuck in worse local optima as the alternatives. Hence, it is not really recommended to use this algorithm (also known as "Alternate" in classic facility location literature, and re-invented by Park and Jun 2009)

  • type M - matrix data type such as ndarray::Array2 or kmedoids::arrayadapter::LowerTriangle
  • type N - number data type such as i32 or f64 (must be signed)
  • mat - a pairwise distance matrix
  • med - the list of medoids
  • maxiter - the maximum number of iterations allowed

returns a tuple containing:

  • the final loss
  • the final cluster assignment
  • the number of iterations needed

Panics

  • panics when the dissimilarity matrix is not square
  • panics when k is 0 or larger than N

Example

Given a dissimilarity matrix of size 4 x 4, use:

let data = ndarray::arr2(&[[0,1,2,3],[1,0,4,5],[2,4,0,6],[3,5,6,0]]);
let mut meds = kmedoids::random_initialization(4, 2, &mut rand::thread_rng());
let (loss, assi, n_iter) = kmedoids::alternating(&data, &mut meds, 100);
println!("Loss is: {}", loss);