pub struct KMeansClassifier<InitAlg: Initializer> { /* private fields */ }Expand description
K-Means Classification model.
Contains option for centroids. Specifies iterations and number of classes.
§Usage
This model is used through the UnSupModel trait. The model is
trained via the train function with a matrix containing rows of
feature vectors.
The model will not check to ensure the data coming in is all valid. This responsibility lies with the user (for now).
Implementations§
Source§impl KMeansClassifier<KPlusPlus>
impl KMeansClassifier<KPlusPlus>
Sourcepub fn new(k: usize) -> KMeansClassifier<KPlusPlus>
pub fn new(k: usize) -> KMeansClassifier<KPlusPlus>
Constructs untrained k-means classifier model.
Requires number of classes to be specified. Defaults to 100 iterations and kmeans++ initialization.
§Examples
use rusty_machine::learning::k_means::KMeansClassifier;
let model = KMeansClassifier::new(5);Examples found in repository?
examples/k-means_generating_cluster.rs (line 59)
44fn main() {
45 println!("K-Means clustering example:");
46
47 const SAMPLES_PER_CENTROID: usize = 2000;
48
49 println!("Generating {0} samples from each centroids:",
50 SAMPLES_PER_CENTROID);
51 // Choose two cluster centers, at (-0.5, -0.5) and (0, 0.5).
52 let centroids = Matrix::new(2, 2, vec![-0.5, -0.5, 0.0, 0.5]);
53 println!("{}", centroids);
54
55 // Generate some data randomly around the centroids
56 let samples = generate_data(¢roids, SAMPLES_PER_CENTROID, 0.4);
57
58 // Create a new model with 2 clusters
59 let mut model = KMeansClassifier::new(2);
60
61 // Train the model
62 println!("Training the model...");
63 // Our train function returns a Result<(), E>
64 model.train(&samples).unwrap();
65
66 let centroids = model.centroids().as_ref().unwrap();
67 println!("Model Centroids:\n{:.3}", centroids);
68
69 // Predict the classes and partition into
70 println!("Classifying the samples...");
71 let classes = model.predict(&samples).unwrap();
72 let (first, second): (Vec<usize>, Vec<usize>) = classes.data().iter().partition(|&x| *x == 0);
73
74 println!("Samples closest to first centroid: {}", first.len());
75 println!("Samples closest to second centroid: {}", second.len());
76}Source§impl<InitAlg: Initializer> KMeansClassifier<InitAlg>
impl<InitAlg: Initializer> KMeansClassifier<InitAlg>
Sourcepub fn new_specified(
k: usize,
iters: usize,
algo: InitAlg,
) -> KMeansClassifier<InitAlg>
pub fn new_specified( k: usize, iters: usize, algo: InitAlg, ) -> KMeansClassifier<InitAlg>
Constructs untrained k-means classifier model.
Requires number of classes, number of iterations, and the initialization algorithm to use.
§Examples
use rusty_machine::learning::k_means::{KMeansClassifier, Forgy};
let model = KMeansClassifier::new_specified(5, 42, Forgy);Sourcepub fn init_algorithm(&self) -> &InitAlg
pub fn init_algorithm(&self) -> &InitAlg
Get the initialization algorithm.
Sourcepub fn centroids(&self) -> &Option<Matrix<f64>>
pub fn centroids(&self) -> &Option<Matrix<f64>>
Get the centroids Option<Matrix<f64>>.
Examples found in repository?
examples/k-means_generating_cluster.rs (line 66)
44fn main() {
45 println!("K-Means clustering example:");
46
47 const SAMPLES_PER_CENTROID: usize = 2000;
48
49 println!("Generating {0} samples from each centroids:",
50 SAMPLES_PER_CENTROID);
51 // Choose two cluster centers, at (-0.5, -0.5) and (0, 0.5).
52 let centroids = Matrix::new(2, 2, vec![-0.5, -0.5, 0.0, 0.5]);
53 println!("{}", centroids);
54
55 // Generate some data randomly around the centroids
56 let samples = generate_data(¢roids, SAMPLES_PER_CENTROID, 0.4);
57
58 // Create a new model with 2 clusters
59 let mut model = KMeansClassifier::new(2);
60
61 // Train the model
62 println!("Training the model...");
63 // Our train function returns a Result<(), E>
64 model.train(&samples).unwrap();
65
66 let centroids = model.centroids().as_ref().unwrap();
67 println!("Model Centroids:\n{:.3}", centroids);
68
69 // Predict the classes and partition into
70 println!("Classifying the samples...");
71 let classes = model.predict(&samples).unwrap();
72 let (first, second): (Vec<usize>, Vec<usize>) = classes.data().iter().partition(|&x| *x == 0);
73
74 println!("Samples closest to first centroid: {}", first.len());
75 println!("Samples closest to second centroid: {}", second.len());
76}Trait Implementations§
Source§impl<InitAlg: Debug + Initializer> Debug for KMeansClassifier<InitAlg>
impl<InitAlg: Debug + Initializer> Debug for KMeansClassifier<InitAlg>
Source§impl<InitAlg: Initializer> UnSupModel<Matrix<f64>, Vector<usize>> for KMeansClassifier<InitAlg>
impl<InitAlg: Initializer> UnSupModel<Matrix<f64>, Vector<usize>> for KMeansClassifier<InitAlg>
Auto Trait Implementations§
impl<InitAlg> Freeze for KMeansClassifier<InitAlg>where
InitAlg: Freeze,
impl<InitAlg> RefUnwindSafe for KMeansClassifier<InitAlg>where
InitAlg: RefUnwindSafe,
impl<InitAlg> Send for KMeansClassifier<InitAlg>where
InitAlg: Send,
impl<InitAlg> Sync for KMeansClassifier<InitAlg>where
InitAlg: Sync,
impl<InitAlg> Unpin for KMeansClassifier<InitAlg>where
InitAlg: Unpin,
impl<InitAlg> UnwindSafe for KMeansClassifier<InitAlg>where
InitAlg: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more