Skip to content

Commit ae9f7ef

Browse files
committed
BUG: type conversion in spectral_embedding
Under some version of numpy, inplace operations with different types lead to errors rather than implicit casting. Fixes scikit-learn#2153
1 parent 16e971c commit ae9f7ef

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

sklearn/manifold/tests/test_spectral_embedding.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ def test_spectral_embedding_two_components(seed=36):
6969
se_precomp = SpectralEmbedding(n_components=1, affinity="precomputed",
7070
random_state=np.random.RandomState(seed))
7171
embedded_coordinate = se_precomp.fit_transform(affinity)
72+
# Some numpy versions are touchy with types
73+
embedded_coordinate = \
74+
se_precomp.fit_transform(affinity.astype(np.float32))
7275
# thresholding on the first components using 0.
7376
label_ = np.array(embedded_coordinate.ravel() < 0, dtype="float")
7477
assert_equal(normalized_mutual_info_score(true_label, label_), 1.0)

sklearn/utils/graph.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ def _laplacian_dense(graph, normed=False, return_diag=False):
173173
w[w_zeros] = 1
174174
lap /= w
175175
lap /= w[:, np.newaxis]
176-
lap.flat[::n_nodes + 1] = 1 - w_zeros
176+
lap.flat[::n_nodes + 1] = (1 - w_zeros).astype(lap.dtype)
177177
else:
178-
lap.flat[::n_nodes + 1] = w
178+
lap.flat[::n_nodes + 1] = w.astype(lap.dtype)
179179

180180
if return_diag:
181181
return lap, w

0 commit comments

Comments
 (0)