DeepIron 0.1.4

A simple Rust library for machine learning and deep learning.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! A set of structs and functions for k-means clustering.

use crate::data_loader::DataFrameTransformer;
use crate::model::activation_functions::{ActivationFunction, ActivationFunctionType};
use crate::model::loss_functions::{LossFunction, LossFunctionType};
use crate::model::*;
use polars::prelude::*;
use polars::series::Series;

/// A struct that defines a k-means clustering model
///
/// # Example
///
/// ```
/// 
/// let model = Model::KMeans::new_random(3, EndCondition::MaxIter(100));
/// 
/// model.fit(&x);
/// 
/// let  = model.predict(&x);
///
/// ```
pub struct KMeans {
    /// The type of initialisation for the centroids.
    pub init_type: InitType,
    /// The number of clusters to create.
    pub n_clusters: usize,
    /// The coordinates of the centroids (each column is a centroid)
    pub centroid_coordinates: DataFrame,
    /// The condition for ending the k-means algorithm.
    pub end_condition: EndCondition,
}

#[derive(Clone)]
/// An enum that defines the types of initialisation for the positions of the centroids in the k-means model.
pub enum InitType {
    /// Randomly initialise the centroids.
    Random,
    /// Use user-defined initial centroids.
    UserDefined(DataFrame),
    /// Initialise the centroids to be equidistant from each other.
    Equidistant,
}

#[derive(Clone)]
/// An enum that defines the conditions for ending the k-means algorithm.
pub enum EndCondition {
    /// The maximum number of iterations.
    /// The algorithm will stop when the centroids have converged or the maximum number of iterations is reached.
    MaxIter(usize),
    /// The tolerance for the change in the centroids.
    /// The algorithm will stop when the change in the centroids is less than the tolerance.
    Tol(f64),
}

impl KMeans {
    /// Create a new k-means model with the specified number of randomly initialised clusters and specified end condition.
    ///
    /// # Arguments
    ///
    /// * `n_clusters` - The number of clusters to create.
    /// 
    /// * `end_condition` - The condition for ending the k-means algorithm.
    ///
    /// # Example
    ///
    /// ```
    /// let model = Model::KMeans::new_random(3, EndCondition::MaxIter(100));
    /// ```
    pub fn new_random(n_clusters: usize, end_condition: EndCondition) -> KMeans {
        KMeans {
            init_type: InitType::Random,
            n_clusters,
            centroid_coordinates: DataFrame::new::<Series>(vec![]).unwrap(),
            end_condition
        }
    }

    /// Create a new k-means model with the specified number of clusters, user-defined initial centroids, and specified end condition.
    /// 
    /// # Arguments
    /// 
    /// * `n_clusters` - The number of clusters to create.
    /// 
    /// * `centroids` - A DataFrame containing the initial centroid coordinates.
    /// 
    /// * `end_condition` - The condition for ending the k-means algorithm.
    /// 
    /// # Example
    /// 
    /// ```
    /// 
    /// let model = Model::KMeans::new_user_defined(3, centroids, EndCondition::MaxIter(100));
    /// 
    /// ```
    pub fn new_user_defined(n_clusters: usize, centroids: DataFrame, end_condition: EndCondition) -> KMeans {
        KMeans {
            init_type: InitType::UserDefined(centroids),
            n_clusters,
            centroid_coordinates: DataFrame::new::<Series>(vec![]).unwrap(),
            end_condition
        }
    }

    /// Create a new k-means model with the specified number of clusters, equidistant initial centroids, and specified end condition.
    /// 
    /// # Arguments
    /// 
    /// * `n_clusters` - The number of clusters to create.
    /// 
    /// * `end_condition` - The condition for ending the k-means algorithm.
    /// 
    /// # Example
    /// 
    /// ```
    /// 
    /// let model = Model::KMeans::new_equidistant(3, EndCondition::MaxIter(100));
    /// 
    /// ```
    pub fn new_equidistant(n_clusters: usize, end_condition: EndCondition) -> KMeans {
        KMeans {
            init_type: InitType::Equidistant,
            n_clusters,
            centroid_coordinates: DataFrame::new::<Series>(vec![]).unwrap(),
            end_condition
        }
    }

    /// Calculate the Euclidean distance between two points.
    /// 
    /// # Arguments
    /// 
    /// * `point1` - A Series containing the coordinates of the first point.
    /// 
    /// * `point2` - A Series containing the coordinates of the second point.
    /// 
    /// # Returns
    /// 
    /// * `f64` - The Euclidean distance between the two points.
    /// 
    /// # Example
    /// 
    /// ```
    /// 
    /// let distance = Model::KMeans::euclidean_distance(&point1, &point2);
    /// 
    /// ```
    fn euclidean_distance(point1: &Series, point2: &Series) -> f64 {
        let point1: Vec<f64> = point1.f64().unwrap().into_iter().map(|x| x.unwrap()).collect();
        let point2: Vec<f64> = point2.f64().unwrap().into_iter().map(|x| x.unwrap()).collect();

        let mut sum: f64 = 0.0;

        for (i, _) in point1.iter().enumerate() {
            sum += (point1[i] - point2[i]).powi(2);
        }
        sum.sqrt()
    }

    /// Check for convergence of the centroids.
    /// 
    /// # Arguments
    /// 
    /// * `old_centroids` - A DataFrame containing the old centroid coordinates.
    /// 
    /// * `new_centroids` - A DataFrame containing the new centroid coordinates.
    /// 
    /// * `tol` - The tolerance for the change in the centroids.
    /// 
    /// # Returns
    /// 
    /// * `bool` - A boolean indicating whether the centroids have converged.
    /// 
    /// # Example
    /// 
    /// ```
    /// 
    /// let converged = Model::KMeans::check_convergence(&old_centroids, &new_centroids, 0.01);
    /// 
    /// ```
    fn check_convergence(old_centroids: &DataFrame, new_centroids: &DataFrame, tol: f64) -> bool {
        let mut converged: bool = true;

        for i in 0..old_centroids.width() {
            let old_centroid: Vec<f64> = old_centroids.get_col_by_index(i).unwrap().f64().unwrap().into_iter().map(|x| x.unwrap()).collect();
            let new_centroid: Vec<f64> = new_centroids.get_col_by_index(i).unwrap().f64().unwrap().into_iter().map(|x| x.unwrap()).collect();
            for j in 0..old_centroid.len() {
                if (old_centroid[j] - new_centroid[j]).abs() > tol {
                    converged = false;
                    break;
                }
            }
        }
        converged
    }

    /// Initialise the centroids for a k-means model.
    /// 
    /// # Arguments
    /// 
    /// * `x` - A DataFrame of features.
    /// 
    /// # Returns
    /// 
    /// * `Result<(), PolarsError>` - A Result indicating whether the centroids were successfully initialised.
    /// 
    /// # Example
    /// 
    /// ```
    /// 
    /// model = Model::KMeans::new_random(3);
    /// model.initialise_centroids(&x);
    /// 
    /// ```
    fn initialise_centroids(&mut self, x: &DataFrame) -> Result<(), PolarsError> {
        let mut centroids: DataFrame = DataFrame::new::<Series>(vec![]).unwrap();

        // Find min and max for each column in the input dataframe (feature)
        let min: Vec<f64> = x.min().get(0).unwrap().into_iter().map(|x: AnyValue| if let AnyValue::Float64(x) = x { x } else { panic!("Invalid type") }).collect();
        let max: Vec<f64> = x.max().get(0).unwrap().into_iter().map(|x: AnyValue| if let AnyValue::Float64(x) = x { x } else { panic!("Invalid type") }).collect();

        match &self.init_type {
            InitType::Random => {
                for _ in 0..self.n_clusters {
                    let mut centroid: Vec<f64> = Vec::with_capacity(x.width());
                    for _ in 0..x.width() {
                        centroid.push(rand::random::<f64>() * (max[0] - min[0]) + min[0]);
                    }
                    // Each column of the DataFrame is a centroid
                    centroids.with_column(Series::new(&centroids.width().to_string(), centroid))?;
                }
                self.centroid_coordinates = centroids;
            }

            InitType::UserDefined(user_centroids) => {
                if user_centroids.width() != self.n_clusters {
                    panic!("Number of user-defined centroids does not match number of clusters");
                }
                if user_centroids.height() != x.width() {
                    panic!("Number of features in user-defined centroids does not match number of features in data");
                }
                self.centroid_coordinates = user_centroids.clone();
            }

            InitType::Equidistant => {
                let diff: Vec<f64> = max.iter().zip(min.iter()).map(|(x, y)| x - y).collect();
                let equidist: Vec<f64> = diff.iter().map(|x| x / (self.n_clusters as f64)).collect();
                for i in 0..self.n_clusters {
                    let mut centroid: Vec<f64> = Vec::with_capacity(x.width());
                    for j in 0..x.width() {
                        centroid.push(min[j] + (i as f64) * equidist[j]);
                    }
                    centroids.with_column(Series::new(&centroids.width().to_string(), centroid))?;
                }
                self.centroid_coordinates = centroids;
            }
        }
        Ok(())
    }

    /// Assigns a cluster to each data point based on the current centroid coordinates.
    /// 
    /// # Arguments
    /// 
    /// * `x` - A DataFrame of features.
    /// 
    /// # Returns
    /// 
    /// * `Vec<usize>` - A vector containing the cluster assignments for each data point.
    /// 
    /// # Example
    /// 
    /// ```
    /// 
    /// let cluster_assignments = model.assign_clusters(&x);
    /// 
    /// ```
    fn assign_clusters(&self, x: &DataFrame) -> Vec<usize> {
        let mut cluster_assignments: Vec<usize> = Vec::with_capacity(x.height());
        for i in 0..x.height() {
            let mut min_distance: f64 = f64::INFINITY;
            let mut cluster: usize = 0;
            for j in 0..self.n_clusters {
                let point_1: Series = x.get(i).unwrap().into_iter().map(|x: AnyValue| if let AnyValue::Float64(x) = x { x } else { panic!("Invalid type") }).collect();
                let centroid: Series = self.centroid_coordinates.get_col_by_index(j).unwrap();
                let distance: f64 = KMeans::euclidean_distance(&point_1, &centroid);
                if distance < min_distance {
                    min_distance = distance;
                    cluster = j;
                }
            }
            cluster_assignments.push(cluster);
        }
        cluster_assignments
    }
}

impl model::ClusterModeller for KMeans {
    /// Fit the model to the data.
    /// 
    /// # Arguments
    /// 
    /// * `x` - A DataFrame of features.
    /// 
    /// # Example
    /// 
    /// ```
    /// 
    /// model.fit(&x);
    /// 
    /// ```
    /// 
    /// # Returns
    /// 
    /// * `Result<(), PolarsError>` - A Result indicating whether the model was successfully fitted.
    fn fit(
        &mut self,
        x: &DataFrame,
    ) -> Result<(), PolarsError> {

        self.initialise_centroids(x)?;

        let mut old_centroids: DataFrame = DataFrame::new::<Series>(vec![]).unwrap();
        let mut new_centroids: DataFrame = DataFrame::new::<Series>(vec![]).unwrap();

        match self.end_condition {
            EndCondition::MaxIter(max_iter) => {
                for _ in 0..max_iter {
                    old_centroids = self.centroid_coordinates.clone();
                    new_centroids = DataFrame::new::<Series>(vec![]).unwrap();
                    let cluster_assignments: Vec<usize> = self.assign_clusters(x);
                    for i in 0..self.n_clusters {
                        let cluster_indices: Vec<usize> = cluster_assignments.iter().enumerate().filter(|(_, &x)| x == i).map(|(x, _)| x).collect();
                        let cluster_data: DataFrame = DataFrame::select_rows(&x, cluster_indices)?;
                        let mut new_centroid: Vec<f64> = Vec::with_capacity(x.width());
                        for j in 0..x.width() {
                            new_centroid.push(cluster_data.get_col_by_index(j).unwrap().mean().unwrap());
                        }
                        new_centroids.with_column(Series::new(&new_centroids.width().to_string(), new_centroid))?;
                    }
                    self.centroid_coordinates = new_centroids.clone();
                    if KMeans::check_convergence(&old_centroids, &new_centroids, 0.0) {
                        break;
                    }
                }
            }

            EndCondition::Tol(tol) => {
                loop {
                    old_centroids = self.centroid_coordinates.clone();
                    new_centroids = DataFrame::new::<Series>(vec![]).unwrap();
                    let cluster_assignments: Vec<usize> = self.assign_clusters(x);
                    for i in 0..self.n_clusters {
                        let cluster_indices: Vec<usize> = cluster_assignments.iter().enumerate().filter(|(_, &x)| x == i).map(|(x, _)| x).collect();
                        let cluster_data: DataFrame = DataFrame::select_rows(&x, cluster_indices)?;
                        let mut new_centroid: Vec<f64> = Vec::with_capacity(cluster_data.width());

                        if cluster_data.height() == 0 {
                            // If there are no points in the cluster, keep the old centroid
                            new_centroid = self.centroid_coordinates.get_col_by_index(i).unwrap().f64().unwrap().into_iter().map(|x| x.unwrap()).collect();
                        } else {
                            // Else, calculate the new centroid by taking the mean of the points in the cluster
                            for j in 0..cluster_data.width() {
                                new_centroid.push(cluster_data.get_col_by_index(j).unwrap().mean().unwrap());
                            }
                        }
                        new_centroids.with_column(Series::new(&new_centroids.width().to_string(), new_centroid))?;
                    }
                    self.centroid_coordinates = new_centroids.clone();
                    if KMeans::check_convergence(&old_centroids, &new_centroids, tol) {
                        break;
                    }
                }
            }
        }
        Ok(())
    }

    /// Predict the cluster assignments for a DataFrame of features.
    /// 
    /// # Arguments
    /// 
    /// * `x` - A DataFrame of features.
    /// 
    /// # Returns
    /// 
    /// * `Result<Series, PolarsError>` - A result containing the predicted target values.
    /// 
    /// # Example
    /// 
    /// ```
    /// 
    /// let cluster_assignments = model.predict(&x);
    /// 
    /// ```
    fn predict(&mut self, x: &DataFrame) -> Result<Series, PolarsError> {
        if self.centroid_coordinates.width() == 0 {
            self.initialise_centroids(x)?;
        }
        let cluster_assignments: Vec<usize> = self.assign_clusters(x);
        let cluster_assignments: Vec<f64> = cluster_assignments.iter().map(|x| *x as f64).collect();
        Ok(Series::new("clusters", cluster_assignments))
    }

    /// Calculate the compactness of the clusters using sum of squared errors.
    /// 
    /// # Arguments
    /// 
    /// * `x` - A DataFrame of features.
    /// 
    /// # Returns
    /// 
    /// * `Result<f64, PolarsError>` - A result containing the compactness of the clusters.
    /// 
    /// # Example
    /// 
    /// ```
    /// 
    /// let compactness = model.compactness(&x);
    /// 
    /// ```
    fn compactness(&mut self, x: &DataFrame) -> Result<f64, PolarsError> {
        let cluster_assignments: Series = self.predict(x)?;
        let cluster_assignments: Vec<usize> = cluster_assignments.f64().unwrap().into_iter().map(|x| x.unwrap() as usize).collect();
        let mut compactness: f64 = 0.0;
        for i in 0..x.height() {
            let cluster: usize = cluster_assignments[i];
            let point: Series = x.get(i).unwrap().into_iter().map(|x: AnyValue| if let AnyValue::Float64(x) = x { x } else { panic!("Invalid type") }).collect();
            let centroid: Series = self.centroid_coordinates.get_col_by_index(cluster).unwrap();
            let distance: f64 = KMeans::euclidean_distance(&point, &centroid);
            compactness += distance.powi(2);
        }
        Ok(compactness)
    }
}