7
7
8
8
import numpy as np
9
9
from scipy .linalg import eigh , qr , solve , svd
10
- from scipy .sparse import csr_matrix , eye
10
+ from scipy .sparse import csr_matrix , eye , lil_matrix
11
11
from scipy .sparse .linalg import eigsh
12
12
13
13
from ..base import (
@@ -229,6 +229,7 @@ def _locally_linear_embedding(
229
229
)
230
230
231
231
M_sparse = eigen_solver != "dense"
232
+ M_container_constructor = lil_matrix if M_sparse else np .zeros
232
233
233
234
if method == "standard" :
234
235
W = barycenter_kneighbors_graph (
@@ -239,7 +240,7 @@ def _locally_linear_embedding(
239
240
# depending on the solver, we'll do this differently
240
241
if M_sparse :
241
242
M = eye (* W .shape , format = W .format ) - W
242
- M = ( M .T * M ). tocsr ()
243
+ M = M .T * M
243
244
else :
244
245
M = (W .T * W - W .T - W ).toarray ()
245
246
M .flat [:: M .shape [0 ] + 1 ] += 1 # W = W - I = W - I
@@ -262,7 +263,7 @@ def _locally_linear_embedding(
262
263
Yi = np .empty ((n_neighbors , 1 + n_components + dp ), dtype = np .float64 )
263
264
Yi [:, 0 ] = 1
264
265
265
- M = np . zeros ((N , N ), dtype = np .float64 )
266
+ M = M_container_constructor ((N , N ), dtype = np .float64 )
266
267
267
268
use_svd = n_neighbors > d_in
268
269
@@ -295,9 +296,6 @@ def _locally_linear_embedding(
295
296
nbrs_x , nbrs_y = np .meshgrid (neighbors [i ], neighbors [i ])
296
297
M [nbrs_x , nbrs_y ] += np .dot (w , w .T )
297
298
298
- if M_sparse :
299
- M = csr_matrix (M )
300
-
301
299
elif method == "modified" :
302
300
if n_neighbors < n_components :
303
301
raise ValueError ("modified LLE requires n_neighbors >= n_components" )
@@ -361,7 +359,8 @@ def _locally_linear_embedding(
361
359
362
360
# Now calculate M.
363
361
# This is the [N x N] matrix whose null space is the desired embedding
364
- M = np .zeros ((N , N ), dtype = np .float64 )
362
+ M = M_container_constructor ((N , N ), dtype = np .float64 )
363
+
365
364
for i in range (N ):
366
365
s_i = s_range [i ]
367
366
@@ -397,19 +396,16 @@ def _locally_linear_embedding(
397
396
M [nbrs_x , nbrs_y ] += np .dot (Wi , Wi .T )
398
397
Wi_sum1 = Wi .sum (1 )
399
398
M [i , neighbors [i ]] -= Wi_sum1
400
- M [neighbors [i ], i ] -= Wi_sum1
399
+ M [neighbors [i ], [ i ] ] -= Wi_sum1
401
400
M [i , i ] += s_i
402
401
403
- if M_sparse :
404
- M = csr_matrix (M )
405
-
406
402
elif method == "ltsa" :
407
403
neighbors = nbrs .kneighbors (
408
404
X , n_neighbors = n_neighbors + 1 , return_distance = False
409
405
)
410
406
neighbors = neighbors [:, 1 :]
411
407
412
- M = np . zeros ((N , N ))
408
+ M = M_container_constructor ((N , N ), dtype = np . float64 )
413
409
414
410
use_svd = n_neighbors > d_in
415
411
@@ -432,7 +428,11 @@ def _locally_linear_embedding(
432
428
433
429
nbrs_x , nbrs_y = np .meshgrid (neighbors [i ], neighbors [i ])
434
430
M [nbrs_x , nbrs_y ] -= GiGiT
435
- M [neighbors [i ], neighbors [i ]] += 1
431
+
432
+ M [neighbors [i ], neighbors [i ]] += np .ones (shape = n_neighbors )
433
+
434
+ if M_sparse :
435
+ M = M .tocsr ()
436
436
437
437
return null_space (
438
438
M ,
0 commit comments