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