pub fn kmeans<P: DataPoint>(k: usize, datapoints: Vec<P>) -> Vec<Cluster<P>> {
    // Initialize cluster means
    let mut clusters = _initialize_clusters(k, datapoints.clone());

    // Make the initial clusterings
    for point in datapoints.iter() {
        _cluster(point, &mut clusters);
    }

    // Store the current clustering for comparison later
    let mut prev_clusters = clusters.clone();

    // Recalculate the means based on the points in the cluster
    for cluster in clusters.iter_mut() {
        cluster.recalculate_centroid();
        cluster.points.clear();
    }
    // Cluster again
    for point in datapoints.iter() {
        _cluster(point, &mut clusters);
    }

    // Rinse, repeat; until the clusters cease to change
    while prev_clusters != clusters {
        prev_clusters = clusters.clone();
        for cluster in clusters.iter_mut() {
            cluster.recalculate_centroid();
            cluster.points.clear();
        }
        for point in datapoints.iter() {
            _cluster(point, &mut clusters);
        }
    }

    // Return the final clustering
    clusters
}
/// Clustering algorithm using k-means++
Clustering algorithm using k-means
Clustering algorithm using k - means
pub fn kmeans<P: DataPoint>(k: usize, datapoints: Vec<P>) -> Vec<Cluster<P>> {
    // Initialize cluster means
    let mut clusters = _initialize_clusters(k, datapoints.clone());
    // Make the initial clusterings
    for point in datapoints.iter() {
        _cluster(point, &mut clusters);
    }
    // Store the current clustering for comparison later
    let mut prev_clusters = clusters.clone();
    // Recalculate the means based on the points in the cluster
    for cluster in clusters.iter_mut() {
        cluster.recalculate_centroid();
        cluster.points.clear();
    }
    // Cluster again
    for point in datapoints.iter() {
        _cluster(point, &mut clusters);
    }
    // Rinse, repeat; until the clusters cease to change
    while prev_clusters != clusters {
        prev_clusters = clusters.clone();
        for cluster in clusters.iter_mut() {
            cluster.recalculate_centroid();
            cluster.points.clear();
        }
        for point in datapoints.iter() {
            _cluster(point, &mut clusters);
        }
    }
    // Return the final clustering
    clusters
}
pub fn kmeans < P : DataPoint > ( k : usize , datapoints : Vec < P > ) -> Vec < Cluster < P > > { // Initialize cluster means let mut clusters = _initialize_clusters ( k , datapoints . clone ( ) ) ; // Make the initial clusterings for point in datapoints . iter ( ) { _cluster ( point , & mut clusters ) ; } // Store the current clustering for comparison later let mut prev_clusters = clusters . clone ( ) ; // Recalculate the means based on the points in the cluster for cluster in clusters . iter_mut ( ) { cluster . recalculate_centroid ( ) ; cluster . points . clear ( ) ; } // Cluster again for point in datapoints . iter ( ) { _cluster ( point , & mut clusters ) ; } // Rinse, repeat; until the clusters cease to change while prev_clusters != clusters { prev_clusters = clusters . clone ( ) ; for cluster in clusters . iter_mut ( ) { cluster . recalculate_centroid ( ) ; cluster . points . clear ( ) ; } for point in datapoints . iter ( ) { _cluster ( point , & mut clusters ) ; } } // Return the final clustering clusters }
Clustering algorithm using k-means++
Clustering algorithm using k - means ++
k
usize
datapoints
Vec<P>
k
usize
datapoints
Vec<P>