@@ -57,16 +57,9 @@ cpdef DOUBLE _assign_labels_array(np.ndarray[floating, ndim=2] X,
57
57
DOUBLE min_dist
58
58
DOUBLE dist
59
59
60
- if floating is float :
61
- center_squared_norms = np.zeros(n_clusters, dtype = np.float32)
62
- x_stride = X.strides[1 ] / sizeof(float )
63
- center_stride = centers.strides[1 ] / sizeof(float )
64
- elif floating is double :
65
- center_squared_norms = np.zeros(n_clusters, dtype = np.float64)
66
- x_stride = X.strides[1 ] / sizeof(DOUBLE)
67
- center_stride = centers.strides[1 ] / sizeof(DOUBLE)
68
- else :
69
- raise ValueError (" Unknown floating type." )
60
+ center_squared_norms = np.zeros(n_clusters, dtype = X.dtype)
61
+ x_stride = X.strides[1 ] / sizeof(X.dtype)
62
+ center_stride = centers.strides[1 ] / sizeof(X.dtype)
70
63
71
64
if n_samples == distances.shape[0 ]:
72
65
store_distances = 1
@@ -76,12 +69,10 @@ cpdef DOUBLE _assign_labels_array(np.ndarray[floating, ndim=2] X,
76
69
center_squared_norms[center_idx] = sdot(
77
70
n_features, & centers[center_idx, 0 ], center_stride,
78
71
& centers[center_idx, 0 ], center_stride)
79
- elif floating is double :
72
+ else :
80
73
center_squared_norms[center_idx] = ddot(
81
74
n_features, & centers[center_idx, 0 ], center_stride,
82
75
& centers[center_idx, 0 ], center_stride)
83
- else :
84
- raise ValueError (" Unknown floating type." )
85
76
86
77
for sample_idx in range (n_samples):
87
78
min_dist = - 1
@@ -92,11 +83,9 @@ cpdef DOUBLE _assign_labels_array(np.ndarray[floating, ndim=2] X,
92
83
if floating is float :
93
84
dist += sdot(n_features, & X[sample_idx, 0 ], x_stride,
94
85
& centers[center_idx, 0 ], center_stride)
95
- elif floating is double :
86
+ else :
96
87
dist += ddot(n_features, & X[sample_idx, 0 ], x_stride,
97
88
& centers[center_idx, 0 ], center_stride)
98
- else :
99
- raise ValueError (" Unknown floating type." )
100
89
dist *= - 2
101
90
dist += center_squared_norms[center_idx]
102
91
dist += x_squared_norms[sample_idx]
@@ -139,12 +128,7 @@ cpdef DOUBLE _assign_labels_csr(X, np.ndarray[floating, ndim=1] x_squared_norms,
139
128
DOUBLE min_dist
140
129
DOUBLE dist
141
130
142
- if floating is float :
143
- center_squared_norms = np.zeros(n_clusters, dtype = np.float32)
144
- elif floating is double :
145
- center_squared_norms = np.zeros(n_clusters, dtype = np.float64)
146
- else :
147
- raise ValueError (" Unknown floating type." )
131
+ center_squared_norms = np.zeros(n_clusters, dtype = X.dtype)
148
132
149
133
if n_samples == distances.shape[0 ]:
150
134
store_distances = 1
@@ -153,11 +137,9 @@ cpdef DOUBLE _assign_labels_csr(X, np.ndarray[floating, ndim=1] x_squared_norms,
153
137
if floating is float :
154
138
center_squared_norms[center_idx] = sdot(
155
139
n_features, & centers[center_idx, 0 ], 1 , & centers[center_idx, 0 ], 1 )
156
- elif floating is double :
140
+ else :
157
141
center_squared_norms[center_idx] = ddot(
158
142
n_features, & centers[center_idx, 0 ], 1 , & centers[center_idx, 0 ], 1 )
159
- else :
160
- raise ValueError (" Unknown floating type." )
161
143
162
144
for sample_idx in range (n_samples):
163
145
min_dist = - 1
@@ -317,12 +299,7 @@ def _centers_dense(np.ndarray[floating, ndim=2] X,
317
299
n_features = X.shape[1 ]
318
300
cdef int i, j, c
319
301
cdef np.ndarray[floating, ndim= 2 ] centers
320
- if floating is float :
321
- centers = np.zeros((n_clusters, n_features), dtype = np.float32)
322
- elif floating is double :
323
- centers = np.zeros((n_clusters, n_features), dtype = np.float64)
324
- else :
325
- raise ValueError (" Unknown floating type." )
302
+ centers = np.zeros((n_clusters, n_features), dtype = X.dtype)
326
303
327
304
n_samples_in_cluster = bincount(labels, minlength = n_clusters)
328
305
empty_clusters = np.where(n_samples_in_cluster == 0 )[0 ]
@@ -386,12 +363,7 @@ def _centers_sparse(X, np.ndarray[INT, ndim=1] labels, n_clusters,
386
363
cdef np.ndarray[np.npy_intp, ndim= 1 , mode= " c" ] empty_clusters = \
387
364
np.where(n_samples_in_cluster == 0 )[0 ]
388
365
389
- if floating is float :
390
- centers = np.zeros((n_clusters, n_features), dtype = np.float32)
391
- elif floating is double :
392
- centers = np.zeros((n_clusters, n_features), dtype = np.float64)
393
- else :
394
- raise ValueError (" Unknown floating type." )
366
+ centers = np.zeros((n_clusters, n_features), dtype = X.dtype)
395
367
396
368
# maybe also relocate small clusters?
397
369
0 commit comments