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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
//! Clustering module
//!
//! Provides clustering algorithms for grouping data points into clusters.
use crate::optimized::{OptimizedDataFrame, ColumnView};
use crate::error::{Result, Error};
use crate::ml::pipeline::Transformer;
use crate::column::{Float64Column, Column, ColumnTrait};
use rand::Rng;
use rand::SeedableRng;
use rand::rngs::StdRng;
use crate::utils::rand_compat::GenRangeCompat;
use std::collections::{HashMap, HashSet};
/// K-Means clustering algorithm
pub struct KMeans {
/// Number of clusters
k: usize,
/// Maximum number of iterations
max_iter: usize,
/// Convergence threshold
tol: f64,
/// Random seed
random_seed: Option<u64>,
/// Cluster centroids
centroids: Vec<Vec<f64>>,
/// Labels for each data point
labels: Vec<usize>,
/// Feature names
feature_names: Vec<String>,
/// Inertia (sum of squared distances within clusters)
inertia: f64,
/// Number of iterations until convergence
n_iter: usize,
/// Whether the model has been fitted
fitted: bool,
}
impl KMeans {
/// Create a new KMeans instance
pub fn new(k: usize, max_iter: usize, tol: f64, random_seed: Option<u64>) -> Self {
KMeans {
k,
max_iter,
tol,
random_seed,
centroids: Vec::new(),
labels: Vec::new(),
feature_names: Vec::new(),
inertia: 0.0,
n_iter: 0,
fitted: false,
}
}
/// Get cluster labels
pub fn labels(&self) -> &[usize] {
&self.labels
}
/// Get cluster centroids
pub fn centroids(&self) -> &[Vec<f64>] {
&self.centroids
}
/// Get inertia
pub fn inertia(&self) -> f64 {
self.inertia
}
/// Get the number of iterations until convergence
pub fn n_iter(&self) -> usize {
self.n_iter
}
/// Compute squared Euclidean distance
fn squared_euclidean_distance(x: &[f64], y: &[f64]) -> f64 {
x.iter()
.zip(y.iter())
.map(|(&xi, &yi)| (xi - yi).powi(2))
.sum()
}
/// Initialize cluster centroids using k-means++
fn kmeans_plus_plus_init(&self, data: &[Vec<f64>]) -> Vec<Vec<f64>> {
let n_samples = data.len();
let n_features = data[0].len();
let mut rng = match self.random_seed {
Some(seed) => StdRng::seed_from_u64(seed),
None => StdRng::seed_from_u64(rand::random()),
};
// Select the first centroid randomly
let first_idx = rng.gen_range(0..n_samples);
let mut centroids = vec![data[first_idx].clone()];
// Select the remaining k-1 centroids
for _ in 1..self.k {
// Compute squared distances to the nearest centroid for each data point
let mut distances = vec![0.0; n_samples];
let mut sum_distances = 0.0;
for (i, point) in data.iter().enumerate() {
// Squared distance to the nearest centroid
let closest_dist = centroids
.iter()
.map(|c| Self::squared_euclidean_distance(point, c))
.fold(f64::INFINITY, |a, b| a.min(b));
distances[i] = closest_dist;
sum_distances += closest_dist;
}
// Select the next centroid with probability proportional to squared distance
let mut cumsum = 0.0;
let threshold = rng.gen_range(0.0..sum_distances);
for (i, &dist) in distances.iter().enumerate() {
cumsum += dist;
if cumsum >= threshold {
centroids.push(data[i].clone());
break;
}
}
}
centroids
}
/// Helper method to extract numeric values from column
fn extract_numeric_values(&self, col: &ColumnView) -> Result<Vec<f64>> {
match col.column_type() {
crate::column::ColumnType::Float64 => {
let mut values = Vec::with_capacity(col.len());
let float_col = col.as_float64().ok_or_else(||
Error::ColumnTypeMismatch {
name: col.column().name().unwrap_or("").to_string(),
expected: crate::column::ColumnType::Float64,
found: col.column_type(),
}
)?;
for i in 0..col.len() {
if let Ok(Some(value)) = float_col.get(i) {
values.push(value);
} else {
values.push(0.0); // Treat NA as 0 (or implement an appropriate strategy)
}
}
Ok(values)
},
crate::column::ColumnType::Int64 => {
let mut values = Vec::with_capacity(col.len());
let int_col = col.as_int64().ok_or_else(||
Error::ColumnTypeMismatch {
name: col.column().name().unwrap_or("").to_string(),
expected: crate::column::ColumnType::Int64,
found: col.column_type(),
}
)?;
for i in 0..col.len() {
if let Ok(Some(value)) = int_col.get(i) {
values.push(value as f64);
} else {
values.push(0.0); // Treat NA as 0
}
}
Ok(values)
},
_ => Err(Error::Type(format!("Column type {:?} cannot be converted to numeric", col.column_type())))
}
}
}
impl Transformer for KMeans {
fn fit(&mut self, df: &OptimizedDataFrame) -> Result<()> {
// Extract numeric columns only
let numeric_columns: Vec<String> = df.column_names()
.into_iter()
.filter(|col_name| {
if let Ok(col_view) = df.column(col_name) {
col_view.as_float64().is_some() || col_view.as_int64().is_some()
} else {
false
}
})
.map(|s| s.to_string())
.collect();
if numeric_columns.is_empty() {
return Err(Error::InvalidOperation(
"DataFrame must contain at least one numeric column for KMeans".to_string()
));
}
self.feature_names = numeric_columns.clone();
// Prepare data
let n_samples = df.row_count();
let n_features = self.feature_names.len();
let mut data = vec![vec![0.0; n_features]; n_samples];
// Load data
for (col_idx, col_name) in self.feature_names.iter().enumerate() {
let column = df.column(col_name)?;
let values = self.extract_numeric_values(&column)?;
for row_idx in 0..n_samples {
if row_idx < values.len() {
data[row_idx][col_idx] = values[row_idx];
}
}
}
// Initialize cluster centroids using k-means++
self.centroids = self.kmeans_plus_plus_init(&data);
// Main loop of k-means algorithm
let mut prev_inertia = f64::INFINITY;
self.labels = vec![0; n_samples];
for iter in 0..self.max_iter {
// Assign each data point to the nearest cluster
let mut new_labels = vec![0; n_samples];
let mut inertia = 0.0;
for (i, point) in data.iter().enumerate() {
let mut min_dist = f64::INFINITY;
let mut closest_centroid = 0;
for (j, centroid) in self.centroids.iter().enumerate() {
let dist = Self::squared_euclidean_distance(point, centroid);
if dist < min_dist {
min_dist = dist;
closest_centroid = j;
}
}
new_labels[i] = closest_centroid;
inertia += min_dist;
}
self.labels = new_labels;
self.inertia = inertia;
// Update cluster centroids
let mut new_centroids = vec![vec![0.0; n_features]; self.k];
let mut counts = vec![0; self.k];
for (i, point) in data.iter().enumerate() {
let cluster = self.labels[i];
counts[cluster] += 1;
for j in 0..n_features {
new_centroids[cluster][j] += point[j];
}
}
for i in 0..self.k {
if counts[i] > 0 {
for j in 0..n_features {
new_centroids[i][j] /= counts[i] as f64;
}
}
}
// Convergence check
let mut centroid_shift = 0.0;
for (old, new) in self.centroids.iter().zip(new_centroids.iter()) {
centroid_shift += Self::squared_euclidean_distance(old, new);
}
self.centroids = new_centroids;
// If the change in inertia is below the threshold, convergence is achieved
let inertia_change = (prev_inertia - self.inertia).abs();
if inertia_change / prev_inertia < self.tol {
self.n_iter = iter + 1;
break;
}
if centroid_shift < self.tol {
self.n_iter = iter + 1;
break;
}
prev_inertia = self.inertia;
// If this is the last iteration
if iter == self.max_iter - 1 {
self.n_iter = self.max_iter;
}
}
self.fitted = true;
Ok(())
}
fn transform(&self, df: &OptimizedDataFrame) -> Result<OptimizedDataFrame> {
if !self.fitted {
return Err(Error::InvalidOperation(
"KMeans has not been fitted yet".to_string()
));
}
// Copy the original DataFrame
let mut result = df.clone();
// Assign each data point to the nearest cluster
let n_samples = df.row_count();
let n_features = self.feature_names.len();
let mut data = vec![vec![0.0; n_features]; n_samples];
// Load data
for (col_idx, col_name) in self.feature_names.iter().enumerate() {
let column = df.column(col_name)?;
let values = self.extract_numeric_values(&column)?;
for row_idx in 0..n_samples {
if row_idx < values.len() {
data[row_idx][col_idx] = values[row_idx];
}
}
}
// Assign each data point to the nearest cluster
let mut labels = Vec::with_capacity(n_samples);
let mut distances = Vec::with_capacity(n_samples);
for point in &data {
let mut min_dist = f64::INFINITY;
let mut closest_centroid = 0;
for (j, centroid) in self.centroids.iter().enumerate() {
let dist = Self::squared_euclidean_distance(point, centroid);
if dist < min_dist {
min_dist = dist;
closest_centroid = j;
}
}
labels.push(closest_centroid as i64);
distances.push(min_dist.sqrt()); // Euclidean distance
}
// Add cluster labels and distances as new columns
let labels_column = Column::Int64(crate::column::Int64Column::with_name(labels, "cluster".to_string()));
let distances_column = Column::Float64(Float64Column::with_name(distances, "distance_to_centroid".to_string()));
result.add_column("cluster".to_string(), labels_column)?;
result.add_column("distance_to_centroid".to_string(), distances_column)?;
Ok(result)
}
fn fit_transform(&mut self, df: &OptimizedDataFrame) -> Result<OptimizedDataFrame> {
self.fit(df)?;
self.transform(df)
}
}
/// Hierarchical clustering algorithm
pub struct AgglomerativeClustering {
/// Number of clusters
n_clusters: usize,
/// Linkage method
linkage: Linkage,
/// Distance metric
metric: DistanceMetric,
/// Cluster labels
labels: Vec<usize>,
/// Feature names
feature_names: Vec<String>,
/// Whether the model has been fitted
fitted: bool,
}
/// Linkage method
pub enum Linkage {
/// Single linkage (minimum distance)
Single,
/// Complete linkage (maximum distance)
Complete,
/// Average linkage
Average,
/// Ward linkage
Ward,
}
/// Distance metric
pub enum DistanceMetric {
/// Euclidean distance
Euclidean,
/// Manhattan distance
Manhattan,
/// Cosine distance
Cosine,
}
impl AgglomerativeClustering {
/// Create a new AgglomerativeClustering instance
pub fn new(n_clusters: usize, linkage: Linkage, metric: DistanceMetric) -> Self {
AgglomerativeClustering {
n_clusters,
linkage,
metric,
labels: Vec::new(),
feature_names: Vec::new(),
fitted: false,
}
}
/// Get cluster labels
pub fn labels(&self) -> &[usize] {
&self.labels
}
/// Compute distance between two data points
fn compute_distance(&self, x: &[f64], y: &[f64]) -> f64 {
match self.metric {
DistanceMetric::Euclidean => {
// Euclidean distance
x.iter()
.zip(y.iter())
.map(|(&xi, &yi)| (xi - yi).powi(2))
.sum::<f64>()
.sqrt()
}
DistanceMetric::Manhattan => {
// Manhattan distance
x.iter()
.zip(y.iter())
.map(|(&xi, &yi)| (xi - yi).abs())
.sum()
}
DistanceMetric::Cosine => {
// Cosine distance
let dot_product: f64 = x.iter().zip(y.iter()).map(|(&xi, &yi)| xi * yi).sum();
let norm_x: f64 = x.iter().map(|&xi| xi.powi(2)).sum::<f64>().sqrt();
let norm_y: f64 = y.iter().map(|&yi| yi.powi(2)).sum::<f64>().sqrt();
if norm_x > 0.0 && norm_y > 0.0 {
1.0 - dot_product / (norm_x * norm_y)
} else {
1.0 // Maximum distance
}
}
}
}
/// Compute distance between two clusters
fn compute_cluster_distance(
&self,
cluster1: &[usize],
cluster2: &[usize],
data: &[Vec<f64>],
distances: &HashMap<(usize, usize), f64>,
) -> f64 {
match self.linkage {
Linkage::Single => {
// Single linkage: minimum distance
let mut min_dist = f64::INFINITY;
for &i in cluster1 {
for &j in cluster2 {
let dist = if i < j {
*distances.get(&(i, j)).unwrap_or(&f64::INFINITY)
} else {
*distances.get(&(j, i)).unwrap_or(&f64::INFINITY)
};
min_dist = min_dist.min(dist);
}
}
min_dist
}
Linkage::Complete => {
// Complete linkage: maximum distance
let mut max_dist: f64 = 0.0;
for &i in cluster1 {
for &j in cluster2 {
let dist = if i < j {
*distances.get(&(i, j)).unwrap_or(&0.0)
} else {
*distances.get(&(j, i)).unwrap_or(&0.0)
};
max_dist = max_dist.max(dist);
}
}
max_dist
}
Linkage::Average => {
// Average linkage: average distance
let mut sum_dist = 0.0;
let mut count = 0;
for &i in cluster1 {
for &j in cluster2 {
let dist = if i < j {
*distances.get(&(i, j)).unwrap_or(&0.0)
} else {
*distances.get(&(j, i)).unwrap_or(&0.0)
};
sum_dist += dist;
count += 1;
}
}
if count > 0 {
sum_dist / count as f64
} else {
f64::INFINITY
}
}
Linkage::Ward => {
// Ward linkage: increase in variance
let n1 = cluster1.len();
let n2 = cluster2.len();
if n1 == 0 || n2 == 0 {
return f64::INFINITY;
}
// Centroid of cluster 1
let mut centroid1 = vec![0.0; data[0].len()];
for &i in cluster1 {
for j in 0..data[0].len() {
centroid1[j] += data[i][j];
}
}
for j in 0..centroid1.len() {
centroid1[j] /= n1 as f64;
}
// Centroid of cluster 2
let mut centroid2 = vec![0.0; data[0].len()];
for &i in cluster2 {
for j in 0..data[0].len() {
centroid2[j] += data[i][j];
}
}
for j in 0..centroid2.len() {
centroid2[j] /= n2 as f64;
}
// Distance between centroids
let mut dist = 0.0;
for j in 0..centroid1.len() {
dist += (centroid1[j] - centroid2[j]).powi(2);
}
dist = dist.sqrt();
// Ward linkage distance
(n1 * n2) as f64 * dist / (n1 + n2) as f64
}
}
}
/// Helper method to extract numeric values from column
fn extract_numeric_values(&self, col: &ColumnView) -> Result<Vec<f64>> {
match col.column_type() {
crate::column::ColumnType::Float64 => {
let mut values = Vec::with_capacity(col.len());
let float_col = col.as_float64().ok_or_else(||
Error::ColumnTypeMismatch {
name: col.column().name().unwrap_or("").to_string(),
expected: crate::column::ColumnType::Float64,
found: col.column_type(),
}
)?;
for i in 0..col.len() {
if let Ok(Some(value)) = float_col.get(i) {
values.push(value);
} else {
values.push(0.0); // Treat NA as 0 (or implement an appropriate strategy)
}
}
Ok(values)
},
crate::column::ColumnType::Int64 => {
let mut values = Vec::with_capacity(col.len());
let int_col = col.as_int64().ok_or_else(||
Error::ColumnTypeMismatch {
name: col.column().name().unwrap_or("").to_string(),
expected: crate::column::ColumnType::Int64,
found: col.column_type(),
}
)?;
for i in 0..col.len() {
if let Ok(Some(value)) = int_col.get(i) {
values.push(value as f64);
} else {
values.push(0.0); // Treat NA as 0
}
}
Ok(values)
},
_ => Err(Error::Type(format!("Column type {:?} cannot be converted to numeric", col.column_type())))
}
}
}
impl Transformer for AgglomerativeClustering {
fn fit(&mut self, df: &OptimizedDataFrame) -> Result<()> {
// Extract numeric columns only
let numeric_columns: Vec<String> = df.column_names()
.into_iter()
.filter(|col_name| {
if let Ok(col_view) = df.column(col_name) {
col_view.as_float64().is_some() || col_view.as_int64().is_some()
} else {
false
}
})
.map(|s| s.to_string())
.collect();
if numeric_columns.is_empty() {
return Err(Error::InvalidOperation(
"DataFrame must contain at least one numeric column for AgglomerativeClustering".to_string()
));
}
self.feature_names = numeric_columns.clone();
// Prepare data
let n_samples = df.row_count();
let n_features = self.feature_names.len();
let mut data = vec![vec![0.0; n_features]; n_samples];
// Load data
for (col_idx, col_name) in self.feature_names.iter().enumerate() {
let column = df.column(col_name)?;
let values = self.extract_numeric_values(&column)?;
for row_idx in 0..n_samples {
if row_idx < values.len() {
data[row_idx][col_idx] = values[row_idx];
}
}
}
// Compute distances between all pairs
let mut distances = HashMap::new();
for i in 0..n_samples {
for j in i+1..n_samples {
let dist = self.compute_distance(&data[i], &data[j]);
distances.insert((i, j), dist);
}
}
// Initialize each data point as its own cluster
let mut clusters: Vec<Vec<usize>> = (0..n_samples).map(|i| vec![i]).collect();
// Repeat until the number of clusters reaches the target
while clusters.len() > self.n_clusters {
// Find the two closest clusters
let mut min_dist = f64::INFINITY;
let mut merge_i = 0;
let mut merge_j = 0;
for i in 0..clusters.len() {
for j in i+1..clusters.len() {
let dist = self.compute_cluster_distance(&clusters[i], &clusters[j], &data, &distances);
if dist < min_dist {
min_dist = dist;
merge_i = i;
merge_j = j;
}
}
}
// Merge clusters
let cluster_j = clusters.remove(merge_j); // Remove from higher index
let cluster_i = &mut clusters[merge_i];
cluster_i.extend(cluster_j);
}
// Assign final cluster labels
self.labels = vec![0; n_samples];
for (cluster_idx, cluster) in clusters.iter().enumerate() {
for &sample_idx in cluster {
self.labels[sample_idx] = cluster_idx;
}
}
self.fitted = true;
Ok(())
}
fn transform(&self, df: &OptimizedDataFrame) -> Result<OptimizedDataFrame> {
if !self.fitted {
return Err(Error::InvalidOperation(
"AgglomerativeClustering has not been fitted yet".to_string()
));
}
// Check if the data size matches
if df.row_count() != self.labels.len() {
return Err(Error::InvalidOperation(
"Number of samples in the input DataFrame does not match the number of samples used during fitting".to_string()
));
}
// Copy the original DataFrame
let mut result = df.clone();
// Add cluster labels as a new column
let labels: Vec<i64> = self.labels.iter().map(|&l| l as i64).collect();
let labels_column = Column::Int64(crate::column::Int64Column::with_name(labels, "cluster".to_string()));
result.add_column("cluster".to_string(), labels_column)?;
Ok(result)
}
fn fit_transform(&mut self, df: &OptimizedDataFrame) -> Result<OptimizedDataFrame> {
self.fit(df)?;
self.transform(df)
}
}
/// DBSCAN (Density-Based Spatial Clustering of Applications with Noise) algorithm
pub struct DBSCAN {
/// Epsilon (neighborhood radius)
eps: f64,
/// Minimum number of points
min_samples: usize,
/// Distance metric
metric: DistanceMetric,
/// Cluster labels
labels: Vec<i64>,
/// Feature names
feature_names: Vec<String>,
/// Whether the model has been fitted
fitted: bool,
}
impl DBSCAN {
/// Create a new DBSCAN instance
pub fn new(eps: f64, min_samples: usize, metric: DistanceMetric) -> Self {
DBSCAN {
eps,
min_samples,
metric,
labels: Vec::new(),
feature_names: Vec::new(),
fitted: false,
}
}
/// Get cluster labels
pub fn labels(&self) -> &[i64] {
&self.labels
}
/// Compute distance between two data points
fn compute_distance(&self, x: &[f64], y: &[f64]) -> f64 {
match self.metric {
DistanceMetric::Euclidean => {
// Euclidean distance
x.iter()
.zip(y.iter())
.map(|(&xi, &yi)| (xi - yi).powi(2))
.sum::<f64>()
.sqrt()
}
DistanceMetric::Manhattan => {
// Manhattan distance
x.iter()
.zip(y.iter())
.map(|(&xi, &yi)| (xi - yi).abs())
.sum()
}
DistanceMetric::Cosine => {
// Cosine distance
let dot_product: f64 = x.iter().zip(y.iter()).map(|(&xi, &yi)| xi * yi).sum();
let norm_x: f64 = x.iter().map(|&xi| xi.powi(2)).sum::<f64>().sqrt();
let norm_y: f64 = y.iter().map(|&yi| yi.powi(2)).sum::<f64>().sqrt();
if norm_x > 0.0 && norm_y > 0.0 {
1.0 - dot_product / (norm_x * norm_y)
} else {
1.0 // Maximum distance
}
}
}
}
/// Find the neighbors of a given point
fn region_query(&self, point_idx: usize, data: &[Vec<f64>]) -> Vec<usize> {
let mut neighbors = Vec::new();
for (i, point) in data.iter().enumerate() {
if i != point_idx && self.compute_distance(&data[point_idx], point) <= self.eps {
neighbors.push(i);
}
}
neighbors
}
/// Expand the cluster from a given point
fn expand_cluster(
&self,
point_idx: usize,
neighbors: &[usize],
cluster_id: i64,
labels: &mut [i64],
data: &[Vec<f64>],
visited: &mut HashSet<usize>,
) {
labels[point_idx] = cluster_id;
let mut i = 0;
let mut neighbors_vec = neighbors.to_vec();
while i < neighbors_vec.len() {
let current_point = neighbors_vec[i];
// Process unvisited points
if !visited.contains(¤t_point) {
visited.insert(current_point);
let current_neighbors = self.region_query(current_point, data);
if current_neighbors.len() >= self.min_samples {
// Add density-reachable points
for &neighbor in ¤t_neighbors {
if !neighbors_vec.contains(&neighbor) {
neighbors_vec.push(neighbor);
}
}
}
}
// If the label has not been assigned yet, add to this cluster
if labels[current_point] == -1 {
labels[current_point] = cluster_id;
}
i += 1;
}
}
/// Helper method to extract numeric values from column
fn extract_numeric_values(&self, col: &ColumnView) -> Result<Vec<f64>> {
match col.column_type() {
crate::column::ColumnType::Float64 => {
let mut values = Vec::with_capacity(col.len());
let float_col = col.as_float64().ok_or_else(||
Error::ColumnTypeMismatch {
name: col.column().name().unwrap_or("").to_string(),
expected: crate::column::ColumnType::Float64,
found: col.column_type(),
}
)?;
for i in 0..col.len() {
if let Ok(Some(value)) = float_col.get(i) {
values.push(value);
} else {
values.push(0.0); // Treat NA as 0 (or implement an appropriate strategy)
}
}
Ok(values)
},
crate::column::ColumnType::Int64 => {
let mut values = Vec::with_capacity(col.len());
let int_col = col.as_int64().ok_or_else(||
Error::ColumnTypeMismatch {
name: col.column().name().unwrap_or("").to_string(),
expected: crate::column::ColumnType::Int64,
found: col.column_type(),
}
)?;
for i in 0..col.len() {
if let Ok(Some(value)) = int_col.get(i) {
values.push(value as f64);
} else {
values.push(0.0); // Treat NA as 0
}
}
Ok(values)
},
_ => Err(Error::Type(format!("Column type {:?} cannot be converted to numeric", col.column_type())))
}
}
}
impl Transformer for DBSCAN {
fn fit(&mut self, df: &OptimizedDataFrame) -> Result<()> {
// Extract numeric columns only
let numeric_columns: Vec<String> = df.column_names()
.into_iter()
.filter(|col_name| {
if let Ok(col_view) = df.column(col_name) {
col_view.as_float64().is_some() || col_view.as_int64().is_some()
} else {
false
}
})
.map(|s| s.to_string())
.collect();
if numeric_columns.is_empty() {
return Err(Error::InvalidOperation(
"DataFrame must contain at least one numeric column for DBSCAN".to_string()
));
}
self.feature_names = numeric_columns.clone();
// Prepare data
let n_samples = df.row_count();
let n_features = self.feature_names.len();
let mut data = vec![vec![0.0; n_features]; n_samples];
// Load data
for (col_idx, col_name) in self.feature_names.iter().enumerate() {
let column = df.column(col_name)?;
let values = self.extract_numeric_values(&column)?;
for row_idx in 0..n_samples {
if row_idx < values.len() {
data[row_idx][col_idx] = values[row_idx];
}
}
}
// Initialize labels (unassigned: -1)
self.labels = vec![-1; n_samples];
let mut visited = HashSet::new();
let mut cluster_id = 0;
// Process each point
for i in 0..n_samples {
if !visited.contains(&i) {
visited.insert(i);
// Find neighbors
let neighbors = self.region_query(i, &data);
if neighbors.len() < self.min_samples {
// Treat as noise
self.labels[i] = -1;
} else {
// Start a new cluster
// Expand the cluster (manually implemented to avoid self-referencing issues)
self.labels[i] = cluster_id;
// Copy the neighbor list for processing
let mut process_queue = neighbors.clone();
let mut index = 0;
// Process all points in the queue
while index < process_queue.len() {
let current_point = process_queue[index];
index += 1;
// Process unvisited points
if !visited.contains(¤t_point) {
visited.insert(current_point);
// Get neighbors of the current point
let current_neighbors: Vec<usize> = data.iter().enumerate()
.filter(|(idx, point)| {
*idx != current_point &&
self.compute_distance(&data[current_point], point) <= self.eps
})
.map(|(idx, _)| idx)
.collect::<Vec<_>>();
// If the point is a core point, process its edges as well
if current_neighbors.len() >= self.min_samples {
for &neighbor in ¤t_neighbors {
if !process_queue.contains(&neighbor) {
process_queue.push(neighbor);
}
}
}
}
// Assign label
if self.labels[current_point] == -1 {
self.labels[current_point] = cluster_id;
}
}
cluster_id += 1;
}
}
}
self.fitted = true;
Ok(())
}
fn transform(&self, df: &OptimizedDataFrame) -> Result<OptimizedDataFrame> {
if !self.fitted {
return Err(Error::InvalidOperation(
"DBSCAN has not been fitted yet".to_string()
));
}
// Check if the data size matches
if df.row_count() != self.labels.len() {
return Err(Error::InvalidOperation(
"Number of samples in the input DataFrame does not match the number of samples used during fitting".to_string()
));
}
// Copy the original DataFrame
let mut result = df.clone();
// Add cluster labels as a new column
let int_column = crate::column::Int64Column::with_name(self.labels.clone(), "cluster".to_string());
let labels_column = Column::Int64(int_column);
result.add_column("cluster".to_string(), labels_column)?;
Ok(result)
}
fn fit_transform(&mut self, df: &OptimizedDataFrame) -> Result<OptimizedDataFrame> {
self.fit(df)?;
self.transform(df)
}
}