@@ -31,12 +31,12 @@ np.import_array()
31
31
32
32
33
33
def init_bounds_dense (
34
- np.ndarray[ floating , ndim = 2 , mode = ' c ' ] X, # IN
35
- floating[:, ::1] centers , # IN
36
- floating[:, ::1] center_half_distances , # IN
37
- int[::1] labels , # OUT
38
- floating[::1] upper_bounds , # OUT
39
- floating[:, ::1] lower_bounds ): # OUT
34
+ floating[:, ::1 ] X , # IN READ- ONLY
35
+ floating[:, ::1] centers , # IN
36
+ floating[:, ::1] center_half_distances , # IN
37
+ int[::1] labels , # OUT
38
+ floating[::1] upper_bounds , # OUT
39
+ floating[:, ::1] lower_bounds ): # OUT
40
40
""" Initialize upper and lower bounds for each sample for dense input data.
41
41
42
42
Given X, centers and the pairwise distances divided by 2.0 between the
@@ -182,17 +182,17 @@ def init_bounds_sparse(
182
182
183
183
184
184
def elkan_iter_chunked_dense (
185
- np.ndarray[ floating , ndim = 2 , mode = ' c ' ] X, # IN
186
- floating[::1] sample_weight , # IN
187
- floating[:, ::1] centers_old , # IN
188
- floating[:, ::1] centers_new , # OUT
189
- floating[::1] weight_in_clusters , # OUT
190
- floating[:, ::1] center_half_distances , # IN
191
- floating[::1] distance_next_center , # IN
192
- floating[::1] upper_bounds , # INOUT
193
- floating[:, ::1] lower_bounds , # INOUT
194
- int[::1] labels , # INOUT
195
- floating[::1] center_shift , # OUT
185
+ floating[:, ::1 ] X , # IN READ- ONLY
186
+ floating[::1] sample_weight , # IN READ- ONLY
187
+ floating[:, ::1] centers_old , # IN
188
+ floating[:, ::1] centers_new , # OUT
189
+ floating[::1] weight_in_clusters , # OUT
190
+ floating[:, ::1] center_half_distances , # IN
191
+ floating[::1] distance_next_center , # IN
192
+ floating[::1] upper_bounds , # INOUT
193
+ floating[:, ::1] lower_bounds , # INOUT
194
+ int[::1] labels , # INOUT
195
+ floating[::1] center_shift , # OUT
196
196
int n_threads ,
197
197
bint update_centers = True ):
198
198
""" Single iteration of K-means Elkan algorithm with dense input.
@@ -292,7 +292,7 @@ def elkan_iter_chunked_dense(
292
292
end = start + n_samples_chunk
293
293
294
294
_update_chunk_dense(
295
- & X[start, 0 ],
295
+ X[start: end ],
296
296
sample_weight[start: end],
297
297
centers_old,
298
298
center_half_distances,
@@ -334,11 +334,8 @@ def elkan_iter_chunked_dense(
334
334
335
335
336
336
cdef void _update_chunk_dense(
337
- floating * X, # IN
338
- # expecting C alinged 2D array. XXX: Can be
339
- # replaced by const memoryview when cython min
340
- # version is >= 0.3
341
- floating[::1 ] sample_weight, # IN
337
+ floating[:, ::1 ] X, # IN READ-ONLY
338
+ floating[::1 ] sample_weight, # IN READ-ONLY
342
339
floating[:, ::1 ] centers_old, # IN
343
340
floating[:, ::1 ] center_half_distances, # IN
344
341
floating[::1 ] distance_next_center, # IN
@@ -383,7 +380,7 @@ cdef void _update_chunk_dense(
383
380
# between the sample and its current assigned center.
384
381
if not bounds_tight:
385
382
upper_bound = _euclidean_dense_dense(
386
- X + i * n_features , & centers_old[label, 0 ], n_features, False )
383
+ & X[i, 0 ] , & centers_old[label, 0 ], n_features, False )
387
384
lower_bounds[i, label] = upper_bound
388
385
bounds_tight = 1
389
386
@@ -394,7 +391,7 @@ cdef void _update_chunk_dense(
394
391
or (upper_bound > center_half_distances[label, j])):
395
392
396
393
distance = _euclidean_dense_dense(
397
- X + i * n_features , & centers_old[j, 0 ], n_features, False )
394
+ & X[i, 0 ] , & centers_old[j, 0 ], n_features, False )
398
395
lower_bounds[i, j] = distance
399
396
if distance < upper_bound:
400
397
label = j
@@ -406,7 +403,7 @@ cdef void _update_chunk_dense(
406
403
if update_centers:
407
404
weight_in_clusters[label] += sample_weight[i]
408
405
for k in range (n_features):
409
- centers_new[label * n_features + k] += X[i * n_features + k] * sample_weight[i]
406
+ centers_new[label * n_features + k] += X[i, k] * sample_weight[i]
410
407
411
408
412
409
def elkan_iter_chunked_sparse (
0 commit comments