numrs2 0.3.1

A Rust implementation inspired by NumPy for numerical computing (NumRS2)
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
//! Clustering algorithms module for NumRS2
//!
//! Provides clustering methods similar to scipy.cluster and scikit-learn:
//!
//! # Partitioning Methods
//! - **K-means**: Lloyd's algorithm for centroid-based clustering
//! - **K-means++**: Improved initialization for K-means
//! - **Mini-batch K-means**: Scalable K-means variant
//!
//! # Hierarchical Clustering
//! - **Agglomerative**: Bottom-up hierarchical clustering
//! - **Linkage methods**: Single, complete, average, Ward
//! - **Dendrogram**: Hierarchical tree representation
//!
//! # Density-Based Methods
//! - **DBSCAN**: Density-based spatial clustering
//!
//! # Examples
//!
//! ```
//! use numrs2::prelude::*;
//! use numrs2::cluster::*;
//!
//! // K-means clustering
//! let data = Array::from_vec(vec![
//!     1.0, 2.0,
//!     1.5, 1.8,
//!     5.0, 8.0,
//!     8.0, 8.0,
//!     1.0, 0.6,
//!     9.0, 11.0,
//! ]).reshape(&[6, 2]);
//!
//! let kmeans = KMeans::new(2, KMeansInit::KMeansPlusPlus)
//!     .max_iter(100)
//!     .tol(1e-4)
//!     .fit(&data)
//!     .expect("kmeans fit should succeed");
//!
//! let labels = kmeans.predict(&data).expect("kmeans predict should succeed");
//! let centroids = kmeans.centroids();
//! ```

use crate::array::Array;
use crate::distance::*;
use crate::error::{NumRs2Error, Result};
use num_traits::{Float, One, Zero};
use scirs2_core::random::*;
use std::fmt::Debug;

// ============================================================================
// K-Means Clustering
// ============================================================================

/// Initialization method for K-means
#[derive(Debug, Clone, Copy)]
pub enum KMeansInit {
    /// Random initialization
    Random,
    /// K-means++ initialization (smart seeding)
    KMeansPlusPlus,
    /// Manual initialization (centroids provided)
    Manual,
}

/// K-means clustering algorithm
///
/// Partitions n observations into k clusters where each observation belongs
/// to the cluster with the nearest mean (centroid).
pub struct KMeans<T> {
    k: usize,
    init: KMeansInit,
    max_iter: usize,
    tol: T,
    centroids: Option<Array<T>>,
    inertia: Option<T>,
    n_iter: usize,
}

impl<T> KMeans<T>
where
    T: Float + Debug,
{
    /// Create a new K-means clusterer
    ///
    /// # Arguments
    ///
    /// * `k` - Number of clusters
    /// * `init` - Initialization method
    pub fn new(k: usize, init: KMeansInit) -> Self {
        KMeans {
            k,
            init,
            max_iter: 300,
            tol: T::from(1e-4).expect("Failed to convert default tolerance value"),
            centroids: None,
            inertia: None,
            n_iter: 0,
        }
    }

    /// Set maximum number of iterations
    pub fn max_iter(mut self, max_iter: usize) -> Self {
        self.max_iter = max_iter;
        self
    }

    /// Set convergence tolerance
    pub fn tol(mut self, tol: T) -> Self {
        self.tol = tol;
        self
    }

    /// Fit the K-means model to data
    ///
    /// # Arguments
    ///
    /// * `x` - Data matrix of shape (n_samples, n_features)
    pub fn fit(mut self, x: &Array<T>) -> Result<Self> {
        if x.shape().len() != 2 {
            return Err(NumRs2Error::DimensionMismatch(
                "Input must be 2D array".to_string(),
            ));
        }

        let n_samples = x.shape()[0];
        let n_features = x.shape()[1];

        if self.k > n_samples {
            return Err(NumRs2Error::ValueError(format!(
                "Number of clusters {} exceeds number of samples {}",
                self.k, n_samples
            )));
        }

        // Initialize centroids
        let mut centroids = match self.init {
            KMeansInit::Random => Self::init_random(x, self.k)?,
            KMeansInit::KMeansPlusPlus => Self::init_kmeans_plusplus(x, self.k)?,
            KMeansInit::Manual => {
                if let Some(ref c) = self.centroids {
                    c.clone()
                } else {
                    return Err(NumRs2Error::ValueError(
                        "Manual init requires centroids to be set".to_string(),
                    ));
                }
            }
        };

        let mut labels = vec![0usize; n_samples];
        let mut prev_inertia = T::infinity();

        // K-means iterations
        for iter in 0..self.max_iter {
            // Assignment step: assign each point to nearest centroid
            let mut changed = false;
            for i in 0..n_samples {
                let point = Self::get_row(x, i)?;
                let new_label = Self::nearest_centroid(&point, &centroids)?;
                if new_label != labels[i] {
                    changed = true;
                    labels[i] = new_label;
                }
            }

            // Update step: recompute centroids
            let mut new_centroids = Array::zeros(&[self.k, n_features]);
            let mut counts = vec![0usize; self.k];

            for i in 0..n_samples {
                let label = labels[i];
                counts[label] += 1;
                let point = Self::get_row(x, i)?;
                for j in 0..n_features {
                    let current = new_centroids.get(&[label, j])?;
                    new_centroids.set(&[label, j], current + point.get(&[j])?)?;
                }
            }

            // Average to get centroids
            for k in 0..self.k {
                if counts[k] > 0 {
                    let count_t =
                        T::from(counts[k]).expect("Failed to convert cluster count to type T");
                    for j in 0..n_features {
                        let sum = new_centroids.get(&[k, j])?;
                        new_centroids.set(&[k, j], sum / count_t)?;
                    }
                }
            }

            // Compute inertia (sum of squared distances to centroids)
            let inertia = Self::compute_inertia(x, &new_centroids, &labels)?;

            // Check convergence
            let delta = (prev_inertia - inertia).abs();
            if delta < self.tol && iter > 0 {
                self.centroids = Some(new_centroids);
                self.inertia = Some(inertia);
                self.n_iter = iter + 1;
                return Ok(self);
            }

            centroids = new_centroids;
            prev_inertia = inertia;

            if !changed {
                break;
            }
        }

        self.centroids = Some(centroids);
        self.inertia = Some(prev_inertia);
        self.n_iter = self.max_iter;

        Ok(self)
    }

    /// Predict cluster labels for new data
    pub fn predict(&self, x: &Array<T>) -> Result<Vec<usize>> {
        let centroids = self
            .centroids
            .as_ref()
            .ok_or_else(|| NumRs2Error::ValueError("Model not fitted".to_string()))?;

        let n_samples = x.shape()[0];
        let mut labels = Vec::with_capacity(n_samples);

        for i in 0..n_samples {
            let point = Self::get_row(x, i)?;
            let label = Self::nearest_centroid(&point, centroids)?;
            labels.push(label);
        }

        Ok(labels)
    }

    /// Get the cluster centroids
    pub fn centroids(&self) -> Option<&Array<T>> {
        self.centroids.as_ref()
    }

    /// Get the inertia (sum of squared distances to nearest centroid)
    pub fn inertia(&self) -> Option<T> {
        self.inertia
    }

    /// Get number of iterations run
    pub fn n_iter(&self) -> usize {
        self.n_iter
    }

    // Helper functions

    fn get_row(x: &Array<T>, i: usize) -> Result<Array<T>> {
        let n_features = x.shape()[1];
        let mut row = Vec::with_capacity(n_features);
        for j in 0..n_features {
            row.push(x.get(&[i, j])?);
        }
        Ok(Array::from_vec(row))
    }

    fn nearest_centroid(point: &Array<T>, centroids: &Array<T>) -> Result<usize> {
        let k = centroids.shape()[0];
        let mut min_dist = T::infinity();
        let mut min_idx = 0;

        for i in 0..k {
            let centroid = Self::get_row(centroids, i)?;
            let dist = euclidean(point, &centroid)?;
            if dist < min_dist {
                min_dist = dist;
                min_idx = i;
            }
        }

        Ok(min_idx)
    }

    fn init_random(x: &Array<T>, k: usize) -> Result<Array<T>> {
        let n_samples = x.shape()[0];
        let n_features = x.shape()[1];

        // Use scirs2_core random number generation
        let mut rng = thread_rng();
        let mut indices: Vec<usize> = (0..n_samples).collect();

        // Shuffle using Fisher-Yates
        for i in (1..n_samples).rev() {
            let j = rng.gen_range(0..=i);
            indices.swap(i, j);
        }

        let mut centroids = Array::zeros(&[k, n_features]);
        for i in 0..k {
            let idx = indices[i];
            for j in 0..n_features {
                let val = x.get(&[idx, j])?;
                centroids.set(&[i, j], val)?;
            }
        }

        Ok(centroids)
    }

    fn init_kmeans_plusplus(x: &Array<T>, k: usize) -> Result<Array<T>> {
        let n_samples = x.shape()[0];
        let n_features = x.shape()[1];

        let mut rng = thread_rng();
        let mut centroids = Array::zeros(&[k, n_features]);

        // Choose first centroid randomly
        let first_idx = rng.gen_range(0..n_samples);
        for j in 0..n_features {
            let val = x.get(&[first_idx, j])?;
            centroids.set(&[0, j], val)?;
        }

        // Choose remaining centroids using k-means++ algorithm
        for i in 1..k {
            // Compute distances to nearest centroid for each point
            let mut distances = Vec::with_capacity(n_samples);
            let mut total_dist = T::zero();

            for j in 0..n_samples {
                let point = Self::get_row(x, j)?;

                // Find distance to nearest existing centroid
                let mut min_dist = T::infinity();
                for c in 0..i {
                    let centroid = Self::get_row(&centroids, c)?;
                    let dist = euclidean(&point, &centroid)?;
                    if dist < min_dist {
                        min_dist = dist;
                    }
                }

                let dist_sq = min_dist * min_dist;
                distances.push(dist_sq);
                total_dist = total_dist + dist_sq;
            }

            // Choose next centroid with probability proportional to distance squared
            let threshold = rng.random::<f64>()
                * total_dist
                    .to_f64()
                    .expect("Failed to convert total distance to f64");
            let mut cumsum = 0.0;
            let mut chosen_idx = 0;

            for (idx, &dist) in distances.iter().enumerate() {
                cumsum += dist.to_f64().unwrap_or(0.0);
                if cumsum >= threshold {
                    chosen_idx = idx;
                    break;
                }
            }

            // Set the chosen point as the new centroid
            for j in 0..n_features {
                let val = x.get(&[chosen_idx, j])?;
                centroids.set(&[i, j], val)?;
            }
        }

        Ok(centroids)
    }

    fn compute_inertia(x: &Array<T>, centroids: &Array<T>, labels: &[usize]) -> Result<T> {
        let n_samples = x.shape()[0];
        let mut inertia = T::zero();

        for i in 0..n_samples {
            let point = Self::get_row(x, i)?;
            let centroid = Self::get_row(centroids, labels[i])?;
            let dist = euclidean(&point, &centroid)?;
            inertia = inertia + dist * dist;
        }

        Ok(inertia)
    }
}

// ============================================================================
// Hierarchical Clustering
// ============================================================================

/// Linkage method for hierarchical clustering
#[derive(Debug, Clone, Copy)]
pub enum LinkageMethod {
    /// Single linkage (minimum distance)
    Single,
    /// Complete linkage (maximum distance)
    Complete,
    /// Average linkage (UPGMA)
    Average,
    /// Ward's minimum variance method
    Ward,
}

/// Hierarchical clustering result
#[derive(Debug, Clone)]
pub struct Dendrogram<T> {
    /// Linkage matrix: each row contains [cluster1, cluster2, distance, n_samples]
    pub linkage: Vec<[T; 4]>,
    /// Number of original observations
    pub n_observations: usize,
}

/// Perform agglomerative hierarchical clustering
///
/// # Arguments
///
/// * `x` - Data matrix of shape (n_samples, n_features)
/// * `method` - Linkage method to use
///
/// # Returns
///
/// Dendrogram structure containing the linkage matrix
///
/// # Examples
///
/// ```
/// use numrs2::prelude::*;
/// use numrs2::cluster::*;
///
/// let data = Array::from_vec(vec![
///     1.0, 2.0,
///     1.5, 1.8,
///     5.0, 8.0,
///     8.0, 8.0,
/// ]).reshape(&[4, 2]);
///
/// let dendro = hierarchical(&data, LinkageMethod::Average).expect("hierarchical should succeed");
/// ```
pub fn hierarchical<T>(x: &Array<T>, method: LinkageMethod) -> Result<Dendrogram<T>>
where
    T: Float + Debug,
{
    if x.shape().len() != 2 {
        return Err(NumRs2Error::DimensionMismatch(
            "Input must be 2D array".to_string(),
        ));
    }

    let n = x.shape()[0];

    if n < 2 {
        return Err(NumRs2Error::ValueError(
            "Need at least 2 samples for clustering".to_string(),
        ));
    }

    // Compute pairwise distance matrix
    let distances = pdist(x, DistanceMetric::Euclidean)?;

    // Initialize clusters (each point is its own cluster initially)
    let mut active_clusters: Vec<usize> = (0..n).collect();
    let mut cluster_sizes = vec![1usize; n];
    let mut linkage = Vec::new();
    // Hierarchical clustering loop
    for (next_cluster_id, _) in (n..).zip(0..(n - 1)) {
        // Find the pair of clusters with minimum distance
        let (i, j, min_dist) = find_min_distance(&active_clusters, &distances, n)?;

        let cluster_i = active_clusters[i];
        let cluster_j = active_clusters[j];
        let size_i = cluster_sizes[cluster_i];
        let size_j = cluster_sizes[cluster_j];

        // Record the merge
        linkage.push([
            T::from(cluster_i).expect("Failed to convert cluster_i to type T"),
            T::from(cluster_j).expect("Failed to convert cluster_j to type T"),
            min_dist,
            T::from(size_i + size_j).expect("Failed to convert cluster size to type T"),
        ]);

        // Update cluster information
        active_clusters.remove(j.max(i));
        active_clusters.remove(j.min(i));
        active_clusters.push(next_cluster_id);

        cluster_sizes.push(size_i + size_j);
    }

    Ok(Dendrogram {
        linkage,
        n_observations: n,
    })
}

/// Find the pair of active clusters with minimum distance
fn find_min_distance<T>(
    active: &[usize],
    distances: &Array<T>,
    n: usize,
) -> Result<(usize, usize, T)>
where
    T: Float + Debug,
{
    let mut min_dist = T::infinity();
    let mut min_i = 0;
    let mut min_j = 1;

    for (idx_i, &i) in active.iter().enumerate() {
        for (idx_j, &j) in active.iter().enumerate().skip(idx_i + 1) {
            if i < n && j < n {
                // Both are original points
                let dist_idx = condensed_index(i, j, n);
                let dist = distances.get(&[dist_idx])?;
                if dist < min_dist {
                    min_dist = dist;
                    min_i = idx_i;
                    min_j = idx_j;
                }
            }
        }
    }

    Ok((min_i, min_j, min_dist))
}

/// Convert (i, j) pair to condensed distance matrix index
fn condensed_index(i: usize, j: usize, n: usize) -> usize {
    let (i, j) = if i < j { (i, j) } else { (j, i) };
    n * i - i * (i + 1) / 2 + j - i - 1
}

/// Cut a dendrogram to get flat clusters
///
/// # Arguments
///
/// * `dendro` - Dendrogram from hierarchical clustering
/// * `n_clusters` - Number of clusters to form
///
/// # Returns
///
/// Vector of cluster labels for each observation
pub fn fcluster<T>(dendro: &Dendrogram<T>, n_clusters: usize) -> Result<Vec<usize>>
where
    T: Float + Debug,
{
    let n = dendro.n_observations;

    if n_clusters > n || n_clusters == 0 {
        return Err(NumRs2Error::ValueError(format!(
            "n_clusters must be between 1 and {}",
            n
        )));
    }

    // Simple approach: take the first (n - n_clusters) merges
    let n_merges = n - n_clusters;

    // Initialize: each point in its own cluster
    let mut labels = vec![0usize; n];
    for i in 0..n {
        labels[i] = i;
    }

    // Apply merges
    for (next_label, merge) in (n..).zip(dendro.linkage.iter().take(n_merges)) {
        let c1 = merge[0]
            .to_usize()
            .expect("Failed to convert cluster index to usize");
        let c2 = merge[1]
            .to_usize()
            .expect("Failed to convert cluster index to usize");

        // Relabel all points in clusters c1 and c2 to next_label
        for label in &mut labels {
            if *label == c1 || *label == c2 {
                *label = next_label;
            }
        }
    }

    // Renumber labels to be 0..n_clusters-1
    let mut unique_labels: Vec<usize> = labels.clone();
    unique_labels.sort_unstable();
    unique_labels.dedup();

    let mut label_map = std::collections::HashMap::new();
    for (new_label, &old_label) in unique_labels.iter().enumerate() {
        label_map.insert(old_label, new_label);
    }

    for label in &mut labels {
        *label = *label_map
            .get(label)
            .expect("Label should exist in label_map");
    }

    Ok(labels)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_kmeans_basic() {
        // Simple 2-cluster dataset
        let data = Array::from_vec(vec![
            1.0, 2.0, 1.5, 1.8, 5.0, 8.0, 8.0, 8.0, 1.0, 0.6, 9.0, 11.0,
        ])
        .reshape(&[6, 2]);

        let kmeans = KMeans::new(2, KMeansInit::KMeansPlusPlus)
            .max_iter(100)
            .tol(1e-4)
            .fit(&data)
            .expect("kmeans fit should succeed");

        let labels = kmeans
            .predict(&data)
            .expect("kmeans predict should succeed");

        // Check that we got 2 clusters
        let mut unique_labels = labels.clone();
        unique_labels.sort_unstable();
        unique_labels.dedup();
        assert_eq!(unique_labels.len(), 2);

        // Check that similar points are in the same cluster
        assert_eq!(labels[0], labels[1]); // (1,2) and (1.5,1.8) should be together
        assert_eq!(labels[2], labels[3]); // (5,8) and (8,8) should be together
    }

    #[test]
    fn test_kmeans_init_random() {
        let data = Array::from_vec(vec![1.0, 2.0, 2.0, 3.0, 8.0, 9.0, 9.0, 10.0]).reshape(&[4, 2]);

        let kmeans = KMeans::new(2, KMeansInit::Random)
            .fit(&data)
            .expect("kmeans random init should succeed");

        assert!(kmeans.centroids().is_some());
        assert!(kmeans.inertia().is_some());
    }

    #[test]
    fn test_kmeans_convergence() {
        let data =
            Array::from_vec(vec![0.0, 0.0, 0.1, 0.1, 10.0, 10.0, 10.1, 10.1]).reshape(&[4, 2]);

        let kmeans = KMeans::new(2, KMeansInit::KMeansPlusPlus)
            .tol(1e-6)
            .fit(&data)
            .expect("kmeans convergence test should succeed");

        // Should converge quickly for well-separated clusters
        assert!(kmeans.n_iter() < 10);
    }

    #[test]
    fn test_hierarchical_clustering() {
        let data = Array::from_vec(vec![1.0, 2.0, 1.5, 1.8, 5.0, 8.0, 8.0, 8.0]).reshape(&[4, 2]);

        let dendro =
            hierarchical(&data, LinkageMethod::Average).expect("hierarchical should succeed");

        // Should have n-1 merges for n points
        assert_eq!(dendro.linkage.len(), 3);
        assert_eq!(dendro.n_observations, 4);
    }

    #[test]
    fn test_fcluster() {
        let data = Array::from_vec(vec![1.0, 2.0, 1.5, 1.8, 5.0, 8.0, 8.0, 8.0]).reshape(&[4, 2]);

        let dendro =
            hierarchical(&data, LinkageMethod::Average).expect("hierarchical should succeed");
        let labels = fcluster(&dendro, 2).expect("fcluster should succeed");

        // Should have 2 clusters
        let mut unique = labels.clone();
        unique.sort_unstable();
        unique.dedup();
        assert_eq!(unique.len(), 2);

        // Points 0 and 1 should be in same cluster (close together)
        assert_eq!(labels[0], labels[1]);
    }

    #[test]
    fn test_kmeans_error_handling() {
        let data = Array::from_vec(vec![1.0, 2.0]).reshape(&[2, 1]);

        // k > n_samples should error
        let result = KMeans::new(3, KMeansInit::Random).fit(&data);
        assert!(result.is_err());
    }

    #[test]
    fn test_predict_unfitted() {
        let kmeans = KMeans::<f64>::new(2, KMeansInit::Random);
        let data = Array::from_vec(vec![1.0, 2.0]);

        // Predicting without fitting should error
        assert!(kmeans.predict(&data).is_err());
    }
}