@@ -348,23 +348,23 @@ def __init__(self, features, center=True, rescale=True,
348
348
# Discard distance to self.
349
349
self .kernel_width = np .mean ([np .mean (d [1 :]) for d in D ])
350
350
351
- countV = list (map (len , NN ))
352
- count = sum (countV )
353
- spi = np .zeros ((count ))
354
- spj = np .zeros ((count ))
355
- spv = np .zeros ((count ))
351
+ n_edges = [len (x ) - 1 for x in NN ] # remove distance to self
352
+ value = np .empty (sum (n_edges ), dtype = np .float )
353
+ row = np .empty_like (value , dtype = np .int )
354
+ col = np .empty_like (value , dtype = np .int )
356
355
357
356
start = 0
358
- for i in range (N ):
359
- length = countV [i ] - 1
360
- distance = np .power (D [i ][1 :], 2 )
361
- spi [start :start + length ] = np .kron (np .ones ((length )), i )
362
- spj [start :start + length ] = NN [i ][1 :]
363
- spv [start :start + length ] = np .exp (- distance / self .kernel_width )
364
- start = start + length
365
-
366
-
367
- W = sparse .csc_matrix ((spv , (spi , spj )), shape = (N , N ))
357
+ for vertex in range (N ):
358
+ if kind == 'knn' :
359
+ assert n_edges [vertex ] == k
360
+ end = start + n_edges [vertex ]
361
+ distance = np .power (D [vertex ][1 :], 2 )
362
+ value [start :end ] = np .exp (- distance / self .kernel_width )
363
+ row [start :end ] = np .full (n_edges [vertex ], vertex )
364
+ col [start :end ] = NN [vertex ][1 :]
365
+ start = end
366
+
367
+ W = sparse .csc_matrix ((value , (row , col )), shape = (N , N ))
368
368
369
369
# Enforce symmetry. May have been broken by k-NN. Checking symmetry
370
370
# with np.abs(W - W.T).sum() is as costly as the symmetrization itself.
0 commit comments