Skip to content

Commit 40597f1

Browse files
committed
Merge pull request scikit-learn#3169 from mjbommar/issue-3167-eradicate-todense
PR re: issue 3167 to eradicate .todense()
2 parents 6c86a0e + f4506d0 commit 40597f1

File tree

19 files changed

+62
-65
lines changed

19 files changed

+62
-65
lines changed

examples/applications/plot_model_complexity_influence.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def plot_influence(conf, mse_values, prediction_times, complexities):
119119

120120

121121
def _count_nonzero_coefficients(estimator):
122-
a = estimator.coef_.todense()
122+
a = estimator.coef_.toarray()
123123
return np.count_nonzero(a)
124124

125125
###############################################################################

examples/linear_model/lasso_dense_vs_sparse_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
print("Sparse Lasso done in %fs" % (time() - t0))
6060

6161
t0 = time()
62-
dense_lasso.fit(Xs.todense(), y)
62+
dense_lasso.fit(Xs.toarray(), y)
6363
print("Dense Lasso done in %fs" % (time() - t0))
6464

6565
print("Distance between coefficients : %s"

sklearn/cluster/bicluster/tests/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ def test_get_submatrix():
3535
for X in (data, csr_matrix(data)):
3636
submatrix = get_submatrix(rows, cols, X)
3737
if issparse(submatrix):
38-
submatrix = submatrix.todense()
38+
submatrix = submatrix.toarray()
3939
assert_array_equal(submatrix, [[2, 3],
4040
[6, 7],
4141
[18, 19]])
4242
submatrix[:] = -1
4343
if issparse(X):
44-
X = X.todense()
44+
X = X.toarray()
4545
assert_true(np.all(X != -1))

sklearn/cluster/tests/test_hierarchical.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,15 @@ def test_agglomerative_clustering():
167167
clustering = AgglomerativeClustering(
168168
n_clusters=10,
169169
connectivity=sparse.lil_matrix(
170-
connectivity.todense()[:10, :10]),
170+
connectivity.toarray()[:10, :10]),
171171
linkage=linkage)
172172
assert_raises(ValueError, clustering.fit, X)
173173

174174
# Test that using ward with another metric than euclidean raises an
175175
# exception
176176
clustering = AgglomerativeClustering(
177177
n_clusters=10,
178-
connectivity=connectivity.todense(),
178+
connectivity=connectivity.toarray(),
179179
affinity="manhattan",
180180
linkage="ward")
181181
assert_raises(ValueError, clustering.fit, X)

sklearn/cluster/tests/test_spectral.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def test_discretize(seed=8):
188188
y_true)),
189189
shape=(n_samples,
190190
n_class + 1))
191-
y_true_noisy = (y_indicator.todense()
191+
y_true_noisy = (y_indicator.toarray()
192192
+ 0.1 * random_state.randn(n_samples,
193193
n_class + 1))
194194
y_pred = discretize(y_true_noisy, random_state)

sklearn/datasets/tests/test_svmlight_format.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,13 @@ def test_load_with_qid():
165165
7 qid:2 1:0.87 2:0.12""")
166166
X, y = load_svmlight_file(BytesIO(data), query_id=False)
167167
assert_array_equal(y, [3, 2, 7])
168-
assert_array_equal(X.todense(), [[.53, .12], [.13, .1], [.87, .12]])
168+
assert_array_equal(X.toarray(), [[.53, .12], [.13, .1], [.87, .12]])
169169
res1 = load_svmlight_files([BytesIO(data)], query_id=True)
170170
res2 = load_svmlight_file(BytesIO(data), query_id=True)
171171
for X, y, qid in (res1, res2):
172172
assert_array_equal(y, [3, 2, 7])
173173
assert_array_equal(qid, [1, 1, 2])
174-
assert_array_equal(X.todense(), [[.53, .12], [.13, .1], [.87, .12]])
174+
assert_array_equal(X.toarray(), [[.53, .12], [.13, .1], [.87, .12]])
175175

176176

177177
@raises(ValueError)

sklearn/decomposition/tests/test_pca.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def test_sparse_randomized_pca_inverse():
248248
Y = pca.transform(X)
249249

250250
Y_inverse = pca.inverse_transform(Y)
251-
assert_almost_equal(X.todense(), Y_inverse, decimal=2)
251+
assert_almost_equal(X.toarray(), Y_inverse, decimal=2)
252252

253253
# same as above with whitening (approximate reconstruction)
254254
pca = assert_warns(DeprecationWarning, RandomizedPCA(n_components=2,

sklearn/feature_extraction/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def _to_graph(n_x, n_y, n_z, mask=None, img=None,
124124
(n_voxels, n_voxels),
125125
dtype=dtype)
126126
if return_as is np.ndarray:
127-
return graph.todense()
127+
return graph.toarray()
128128
return return_as(graph)
129129

130130

sklearn/feature_selection/tests/test_base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ def test_transform_sparse():
6262
sel = StepSelector()
6363
Xt_actual = sel.fit(sparse(X)).transform(sparse(X))
6464
Xt_actual2 = sel.fit_transform(sparse(X))
65-
assert_array_equal(Xt, Xt_actual.todense())
66-
assert_array_equal(Xt, Xt_actual2.todense())
65+
assert_array_equal(Xt, Xt_actual.toarray())
66+
assert_array_equal(Xt, Xt_actual2.toarray())
6767

6868
# Check dtype matches
6969
assert_equal(np.int32, sel.transform(sparse(X).astype(np.int32)).dtype)
@@ -96,7 +96,7 @@ def test_inverse_transform_sparse():
9696
sparse = sp.csc_matrix
9797
sel = StepSelector()
9898
Xinv_actual = sel.fit(sparse(X)).inverse_transform(sparse(Xt))
99-
assert_array_equal(Xinv, Xinv_actual.todense())
99+
assert_array_equal(Xinv, Xinv_actual.toarray())
100100

101101
# Check dtype matches
102102
assert_equal(np.int32,

sklearn/linear_model/tests/test_sparse_coordinate_descent.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_sparse_coef():
2020
clf.coef_ = [1, 2, 3]
2121

2222
assert_true(sp.isspmatrix(clf.sparse_coef_))
23-
assert_equal(clf.sparse_coef_.todense().tolist()[0], clf.coef_)
23+
assert_equal(clf.sparse_coef_.toarray().tolist()[0], clf.coef_)
2424

2525

2626
def test_normalize_option():
@@ -168,7 +168,7 @@ def _test_sparse_enet_not_as_toy_dataset(alpha, fit_intercept, positive):
168168
d_clf = ElasticNet(alpha=alpha, l1_ratio=0.8, fit_intercept=fit_intercept,
169169
max_iter=max_iter, tol=1e-7, positive=positive,
170170
warm_start=True)
171-
d_clf.fit(X_train.todense(), y_train)
171+
d_clf.fit(X_train.toarray(), y_train)
172172

173173
assert_almost_equal(d_clf.dual_gap_, 0, 4)
174174
assert_greater(d_clf.score(X_test, y_test), 0.85)
@@ -207,7 +207,7 @@ def test_sparse_lasso_not_as_toy_dataset():
207207

208208
# check the convergence is the same as the dense version
209209
d_clf = Lasso(alpha=0.1, fit_intercept=False, max_iter=max_iter, tol=1e-7)
210-
d_clf.fit(X_train.todense(), y_train)
210+
d_clf.fit(X_train.toarray(), y_train)
211211
assert_almost_equal(d_clf.dual_gap_, 0, 4)
212212
assert_greater(d_clf.score(X_test, y_test), 0.85)
213213

@@ -254,7 +254,7 @@ def test_same_output_sparse_dense_lasso_and_enet_cv():
254254
clfs = ElasticNetCV(max_iter=100, cv=5, normalize=normalize)
255255
ignore_warnings(clfs.fit)(X, y)
256256
clfd = ElasticNetCV(max_iter=100, cv=5, normalize=normalize)
257-
ignore_warnings(clfd.fit)(X.todense(), y)
257+
ignore_warnings(clfd.fit)(X.toarray(), y)
258258
assert_almost_equal(clfs.alpha_, clfd.alpha_, 7)
259259
assert_almost_equal(clfs.intercept_, clfd.intercept_, 7)
260260
assert_array_almost_equal(clfs.mse_path_, clfd.mse_path_)
@@ -263,7 +263,7 @@ def test_same_output_sparse_dense_lasso_and_enet_cv():
263263
clfs = LassoCV(max_iter=100, cv=4, normalize=normalize)
264264
ignore_warnings(clfs.fit)(X, y)
265265
clfd = LassoCV(max_iter=100, cv=4, normalize=normalize)
266-
ignore_warnings(clfd.fit)(X.todense(), y)
266+
ignore_warnings(clfd.fit)(X.toarray(), y)
267267
assert_almost_equal(clfs.alpha_, clfd.alpha_, 7)
268268
assert_almost_equal(clfs.intercept_, clfd.intercept_, 7)
269269
assert_array_almost_equal(clfs.mse_path_, clfd.mse_path_)

0 commit comments

Comments
 (0)