KMeansClassifier

Struct KMeansClassifier 

Source
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>

Source

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(&centroids, 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>

Source

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);
Source

pub fn k(&self) -> usize

Get the number of classes.

Source

pub fn iters(&self) -> usize

Get the number of iterations.

Source

pub fn init_algorithm(&self) -> &InitAlg

Get the initialization algorithm.

Source

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(&centroids, 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

pub fn set_iters(&mut self, iters: usize)

Set the number of iterations.

Trait Implementations§

Source§

impl<InitAlg: Debug + Initializer> Debug for KMeansClassifier<InitAlg>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<InitAlg: Initializer> UnSupModel<Matrix<f64>, Vector<usize>> for KMeansClassifier<InitAlg>

Source§

fn predict(&self, inputs: &Matrix<f64>) -> LearningResult<Vector<usize>>

Predict classes from data.

Model must be trained.

Source§

fn train(&mut self, inputs: &Matrix<f64>) -> LearningResult<()>

Train the classifier using input data.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.