Skip to content

Commit f6163e7

Browse files
lesteveamueller
authored andcommitted
Make scipy.sparse.csgraph imports explicit (#10145)
This can cause problems in scipy 1.0
1 parent f485a9e commit f6163e7

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

sklearn/manifold/spectral_embedding_.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from scipy.linalg import eigh
1212
from scipy.sparse.linalg import eigsh, lobpcg
1313
from scipy.sparse.csgraph import connected_components
14+
from scipy.sparse.csgraph import laplacian as csgraph_laplacian
1415

1516
from ..base import BaseEstimator
1617
from ..externals import six
@@ -234,8 +235,8 @@ def spectral_embedding(adjacency, n_components=8, eigen_solver=None,
234235
warnings.warn("Graph is not fully connected, spectral embedding"
235236
" may not work as expected.")
236237

237-
laplacian, dd = sparse.csgraph.laplacian(adjacency, normed=norm_laplacian,
238-
return_diag=True)
238+
laplacian, dd = csgraph_laplacian(adjacency, normed=norm_laplacian,
239+
return_diag=True)
239240
if (eigen_solver == 'arpack' or eigen_solver != 'lobpcg' and
240241
(not sparse.isspmatrix(laplacian) or n_nodes < 5 * n_components)):
241242
# lobpcg used with eigen_solver='amg' has bugs for low number of nodes

sklearn/manifold/tests/test_spectral_embedding.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from numpy.testing import assert_array_equal
44

55
from scipy import sparse
6+
from scipy.sparse import csgraph
67
from scipy.linalg import eigh
78

89
from sklearn.manifold.spectral_embedding_ import SpectralEmbedding
@@ -254,8 +255,8 @@ def test_spectral_embedding_unnormalized():
254255
drop_first=False)
255256

256257
# Verify using manual computation with dense eigh
257-
laplacian, dd = sparse.csgraph.laplacian(sims, normed=False,
258-
return_diag=True)
258+
laplacian, dd = csgraph.laplacian(sims, normed=False,
259+
return_diag=True)
259260
_, diffusion_map = eigh(laplacian)
260261
embedding_2 = diffusion_map.T[:n_components] * dd
261262
embedding_2 = _deterministic_vector_sign_flip(embedding_2).T

sklearn/semi_supervised/label_propagation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import warnings
6161
import numpy as np
6262
from scipy import sparse
63+
from scipy.sparse import csgraph
6364

6465
from ..base import BaseEstimator, ClassifierMixin
6566
from ..externals import six
@@ -514,7 +515,7 @@ def _build_graph(self):
514515
self.nn_fit = None
515516
n_samples = self.X_.shape[0]
516517
affinity_matrix = self._get_kernel(self.X_)
517-
laplacian = sparse.csgraph.laplacian(affinity_matrix, normed=True)
518+
laplacian = csgraph.laplacian(affinity_matrix, normed=True)
518519
laplacian = -laplacian
519520
if sparse.isspmatrix(laplacian):
520521
diag_mask = (laplacian.row == laplacian.col)

sklearn/utils/graph.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# License: BSD 3 clause
1212

1313
from scipy import sparse
14+
from scipy.sparse import csgraph
1415

1516
from .graph_shortest_path import graph_shortest_path # noqa
1617
from .deprecation import deprecated
@@ -73,11 +74,11 @@ def single_source_shortest_path_length(graph, source, cutoff=None):
7374
"version 0.19 and will be removed in 0.21. Use "
7475
"scipy.sparse.csgraph.connected_components instead.")
7576
def connected_components(*args, **kwargs):
76-
return sparse.csgraph.connected_components(*args, **kwargs)
77+
return csgraph.connected_components(*args, **kwargs)
7778

7879

7980
@deprecated("sklearn.utils.graph.graph_laplacian was deprecated in version "
8081
"0.19 and will be removed in 0.21. Use "
8182
"scipy.sparse.csgraph.laplacian instead.")
8283
def graph_laplacian(*args, **kwargs):
83-
return sparse.csgraph.laplacian(*args, **kwargs)
84+
return csgraph.laplacian(*args, **kwargs)

0 commit comments

Comments
 (0)