@@ -750,7 +750,8 @@ impl KMeans {
750750 centroids. row_mut ( min_cluster) . assign ( & new_centroid) ;
751751 if !self . euclidean {
752752 let norm =
753- ( centroids. row ( min_cluster) . dot ( & centroids. row ( min_cluster) ) ) . sqrt ( ) ;
753+ ( centroids. row ( min_cluster) . dot ( & centroids. row ( min_cluster) ) )
754+ . sqrt ( ) ;
754755 if norm > 0.0 {
755756 centroids. row_mut ( min_cluster) . mapv_inplace ( |x| x / norm) ;
756757 }
@@ -1056,9 +1057,9 @@ mod tests {
10561057 let data = Array2 :: from_shape_vec (
10571058 ( 12 , 2 ) ,
10581059 vec ! [
1059- 1.0 , 1.0 , 1.1 , 1.1 , 1.2 , 1.2 , 1.3 , 1.3 , // 4 points near (1,1)
1060- 5.0 , 5.0 , 5.1 , 5.1 , 5.2 , 5.2 , 5.3 , 5.3 , // 4 points near (5,5)
1061- 9.0 , 9.0 , 9.1 , 9.1 , 9.2 , 9.2 , 9.3 , 9.3 , // 4 points near (9,9)
1060+ 1.0 , 1.0 , 1.1 , 1.1 , 1.2 , 1.2 , 1.3 , 1.3 , // 4 points near (1,1)
1061+ 5.0 , 5.0 , 5.1 , 5.1 , 5.2 , 5.2 , 5.3 , 5.3 , // 4 points near (5,5)
1062+ 9.0 , 9.0 , 9.1 , 9.1 , 9.2 , 9.2 , 9.3 , 9.3 , // 4 points near (9,9)
10621063 ] ,
10631064 )
10641065 . unwrap ( ) ;
@@ -1067,9 +1068,9 @@ mod tests {
10671068 . with_use_medoids ( true )
10681069 . with_balanced ( true )
10691070 . with_euclidean ( true )
1070- . with_max_balance_diff ( 1 ) // Force tight balance (was 0, but 0 is not allowed)
1071+ . with_max_balance_diff ( 1 ) // Force tight balance (was 0, but 0 is not allowed)
10711072 . with_verbose ( false ) ;
1072-
1073+
10731074 let clusters = kmeans. train ( data. view ( ) , None ) . unwrap ( ) ;
10741075
10751076 // Verify that each cluster has exactly 4 points (perfectly balanced)
@@ -1083,18 +1084,29 @@ mod tests {
10831084 for ( cluster_id, & medoid_idx) in medoids. iter ( ) . enumerate ( ) {
10841085 let medoid_point = data. row ( medoid_idx) ;
10851086 let centroid = centroids. row ( cluster_id) ;
1086-
1087+
10871088 // The medoid point should exactly match the centroid since we're using medoids
10881089 for j in 0 ..medoid_point. len ( ) {
1089- assert ! ( ( medoid_point[ j] - centroid[ j] ) . abs( ) < 1e-6 ,
1090- "Medoid point {:?} doesn't match centroid {:?} for cluster {}" ,
1091- medoid_point, centroid, cluster_id) ;
1090+ assert ! (
1091+ ( medoid_point[ j] - centroid[ j] ) . abs( ) < 1e-6 ,
1092+ "Medoid point {:?} doesn't match centroid {:?} for cluster {}" ,
1093+ medoid_point,
1094+ centroid,
1095+ cluster_id
1096+ ) ;
10921097 }
1093-
1098+
10941099 // Verify the medoid is actually assigned to its own cluster
1095- let medoid_cluster = clusters. iter ( ) . position ( |cluster| cluster. contains ( & medoid_idx) ) ;
1096- assert_eq ! ( medoid_cluster, Some ( cluster_id) ,
1097- "Medoid {} not found in its own cluster {}" , medoid_idx, cluster_id) ;
1100+ let medoid_cluster = clusters
1101+ . iter ( )
1102+ . position ( |cluster| cluster. contains ( & medoid_idx) ) ;
1103+ assert_eq ! (
1104+ medoid_cluster,
1105+ Some ( cluster_id) ,
1106+ "Medoid {} not found in its own cluster {}" ,
1107+ medoid_idx,
1108+ cluster_id
1109+ ) ;
10981110 }
10991111 }
11001112 }
0 commit comments