Skip to content

Commit 0d701e8

Browse files
glemaitrelesteve
andauthored
MAINT use .toarray instead of .A that is deprecated (scikit-learn#27367)
Co-authored-by: Loïc Estève <loic.esteve@ymail.com>
1 parent aee4cc2 commit 0d701e8

File tree

11 files changed

+37
-31
lines changed

11 files changed

+37
-31
lines changed

sklearn/cluster/_hdbscan/tests/test_reachibility.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def test_mutual_reachability_graph_equivalence_dense_sparse():
4545
mr_graph_dense = mutual_reachability_graph(X_dense, min_samples=3)
4646
mr_graph_sparse = mutual_reachability_graph(X_sparse, min_samples=3)
4747

48-
assert_allclose(mr_graph_dense, mr_graph_sparse.A)
48+
assert_allclose(mr_graph_dense, mr_graph_sparse.toarray())
4949

5050

5151
@pytest.mark.parametrize("array_type", ["array", "sparse_csr"])

sklearn/feature_extraction/tests/test_dict_vectorizer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def test_dictvectorizer(sparse, dtype, sort, iterable):
3030

3131
if sparse:
3232
# CSR matrices can't be compared for equality
33-
assert_array_equal(X.A, v.transform(iter(D) if iterable else D).A)
33+
assert_array_equal(
34+
X.toarray(), v.transform(iter(D) if iterable else D).toarray()
35+
)
3436
else:
3537
assert_array_equal(X, v.transform(iter(D) if iterable else D))
3638

sklearn/feature_extraction/tests/test_feature_hasher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def test_hash_empty_input():
125125
feature_hasher = FeatureHasher(n_features=n_features, input_type="string")
126126
X = feature_hasher.transform(raw_X)
127127

128-
assert_array_equal(X.A, np.zeros((len(raw_X), n_features)))
128+
assert_array_equal(X.toarray(), np.zeros((len(raw_X), n_features)))
129129

130130

131131
def test_hasher_zeros():

sklearn/impute/tests/test_impute.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,7 @@ def test_simple_imputer_constant_keep_empty_features(array_type, keep_empty_feat
16841684
X_imputed = getattr(imputer, method)(X)
16851685
assert X_imputed.shape == X.shape
16861686
constant_feature = (
1687-
X_imputed[:, 0].A if array_type == "sparse" else X_imputed[:, 0]
1687+
X_imputed[:, 0].toarray() if array_type == "sparse" else X_imputed[:, 0]
16881688
)
16891689
assert_array_equal(constant_feature, fill_value)
16901690

@@ -1705,7 +1705,7 @@ def test_simple_imputer_keep_empty_features(strategy, array_type, keep_empty_fea
17051705
if keep_empty_features:
17061706
assert X_imputed.shape == X.shape
17071707
constant_feature = (
1708-
X_imputed[:, 0].A if array_type == "sparse" else X_imputed[:, 0]
1708+
X_imputed[:, 0].toarray() if array_type == "sparse" else X_imputed[:, 0]
17091709
)
17101710
assert_array_equal(constant_feature, 0)
17111711
else:

sklearn/manifold/tests/test_t_sne.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ def test_sparse_precomputed_distance(sparse_container):
452452
D_sparse = kneighbors_graph(X, n_neighbors=100, mode="distance", include_self=True)
453453
D = pairwise_distances(X)
454454
assert sp.issparse(D_sparse)
455-
assert_almost_equal(D_sparse.A, D)
455+
assert_almost_equal(D_sparse.toarray(), D)
456456

457457
tsne = TSNE(
458458
metric="precomputed", random_state=0, init="random", learning_rate="auto"

sklearn/neighbors/tests/test_neighbors.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,7 +1847,7 @@ def test_non_euclidean_kneighbors():
18471847
X, radius, metric=metric, mode="connectivity", include_self=True
18481848
).toarray()
18491849
nbrs1 = neighbors.NearestNeighbors(metric=metric, radius=radius).fit(X)
1850-
assert_array_equal(nbrs_graph, nbrs1.radius_neighbors_graph(X).A)
1850+
assert_array_equal(nbrs_graph, nbrs1.radius_neighbors_graph(X).toarray())
18511851

18521852
# Raise error when wrong parameters are supplied,
18531853
X_nbrs = neighbors.NearestNeighbors(n_neighbors=3, metric="manhattan")
@@ -1884,13 +1884,15 @@ def test_k_and_radius_neighbors_train_is_not_query():
18841884
check_object_arrays(ind, [[1], [0, 1]])
18851885

18861886
# Test the graph variants.
1887-
assert_array_equal(nn.kneighbors_graph(test_data).A, [[0.0, 1.0], [0.0, 1.0]])
18881887
assert_array_equal(
1889-
nn.kneighbors_graph([[2], [1]], mode="distance").A,
1888+
nn.kneighbors_graph(test_data).toarray(), [[0.0, 1.0], [0.0, 1.0]]
1889+
)
1890+
assert_array_equal(
1891+
nn.kneighbors_graph([[2], [1]], mode="distance").toarray(),
18901892
np.array([[0.0, 1.0], [0.0, 0.0]]),
18911893
)
18921894
rng = nn.radius_neighbors_graph([[2], [1]], radius=1.5)
1893-
assert_array_equal(rng.A, [[0, 1], [1, 1]])
1895+
assert_array_equal(rng.toarray(), [[0, 1], [1, 1]])
18941896

18951897

18961898
@pytest.mark.parametrize("algorithm", ALGORITHMS)
@@ -1912,15 +1914,15 @@ def test_k_and_radius_neighbors_X_None(algorithm):
19121914
rng = nn.radius_neighbors_graph(None, radius=1.5)
19131915
kng = nn.kneighbors_graph(None)
19141916
for graph in [rng, kng]:
1915-
assert_array_equal(graph.A, [[0, 1], [1, 0]])
1917+
assert_array_equal(graph.toarray(), [[0, 1], [1, 0]])
19161918
assert_array_equal(graph.data, [1, 1])
19171919
assert_array_equal(graph.indices, [1, 0])
19181920

19191921
X = [[0, 1], [0, 1], [1, 1]]
19201922
nn = neighbors.NearestNeighbors(n_neighbors=2, algorithm=algorithm)
19211923
nn.fit(X)
19221924
assert_array_equal(
1923-
nn.kneighbors_graph().A,
1925+
nn.kneighbors_graph().toarray(),
19241926
np.array([[0.0, 1.0, 1.0], [1.0, 0.0, 1.0], [1.0, 1.0, 0]]),
19251927
)
19261928

@@ -1978,13 +1980,15 @@ def test_k_and_radius_neighbors_duplicates(algorithm):
19781980
def test_include_self_neighbors_graph():
19791981
# Test include_self parameter in neighbors_graph
19801982
X = [[2, 3], [4, 5]]
1981-
kng = neighbors.kneighbors_graph(X, 1, include_self=True).A
1982-
kng_not_self = neighbors.kneighbors_graph(X, 1, include_self=False).A
1983+
kng = neighbors.kneighbors_graph(X, 1, include_self=True).toarray()
1984+
kng_not_self = neighbors.kneighbors_graph(X, 1, include_self=False).toarray()
19831985
assert_array_equal(kng, [[1.0, 0.0], [0.0, 1.0]])
19841986
assert_array_equal(kng_not_self, [[0.0, 1.0], [1.0, 0.0]])
19851987

1986-
rng = neighbors.radius_neighbors_graph(X, 5.0, include_self=True).A
1987-
rng_not_self = neighbors.radius_neighbors_graph(X, 5.0, include_self=False).A
1988+
rng = neighbors.radius_neighbors_graph(X, 5.0, include_self=True).toarray()
1989+
rng_not_self = neighbors.radius_neighbors_graph(
1990+
X, 5.0, include_self=False
1991+
).toarray()
19881992
assert_array_equal(rng, [[1.0, 1.0], [1.0, 1.0]])
19891993
assert_array_equal(rng_not_self, [[0.0, 1.0], [1.0, 0.0]])
19901994

@@ -2073,7 +2077,7 @@ def test_dtype_convert():
20732077
def test_sparse_metric_callable():
20742078
def sparse_metric(x, y): # Metric accepting sparse matrix input (only)
20752079
assert issparse(x) and issparse(y)
2076-
return x.dot(y.T).A.item()
2080+
return x.dot(y.T).toarray().item()
20772081

20782082
X = csr_matrix(
20792083
[[1, 1, 1, 1, 1], [1, 0, 1, 0, 1], [0, 0, 1, 0, 0]] # Population matrix

sklearn/preprocessing/tests/test_common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ def test_missing_value_handling(
131131
warnings.simplefilter("error", RuntimeWarning)
132132
Xt_sp = est_sparse.fit(X_train_sp).transform(X_test_sp)
133133

134-
assert_allclose(Xt_sp.A, Xt_dense)
134+
assert_allclose(Xt_sp.toarray(), Xt_dense)
135135
with warnings.catch_warnings():
136136
warnings.simplefilter("ignore", PendingDeprecationWarning)
137137
warnings.simplefilter("error", RuntimeWarning)
138138
Xt_inv_sp = est_sparse.inverse_transform(Xt_sp)
139139

140-
assert_allclose(Xt_inv_sp.A, Xt_inv_dense)
140+
assert_allclose(Xt_inv_sp.toarray(), Xt_inv_dense)
141141

142142

143143
@pytest.mark.parametrize(

sklearn/preprocessing/tests/test_polynomial.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ def test_polynomial_features_csc_X(deg, include_bias, interaction_only, dtype):
725725

726726
assert sparse.issparse(Xt_csc) and Xt_csc.format == "csc"
727727
assert Xt_csc.dtype == Xt_dense.dtype
728-
assert_array_almost_equal(Xt_csc.A, Xt_dense)
728+
assert_array_almost_equal(Xt_csc.toarray(), Xt_dense)
729729

730730

731731
@pytest.mark.parametrize(
@@ -752,7 +752,7 @@ def test_polynomial_features_csr_X(deg, include_bias, interaction_only, dtype):
752752

753753
assert sparse.issparse(Xt_csr) and Xt_csr.format == "csr"
754754
assert Xt_csr.dtype == Xt_dense.dtype
755-
assert_array_almost_equal(Xt_csr.A, Xt_dense)
755+
assert_array_almost_equal(Xt_csr.toarray(), Xt_dense)
756756

757757

758758
@pytest.mark.parametrize("n_features", [1, 4, 5])
@@ -811,7 +811,7 @@ def test_polynomial_features_csr_X_floats(deg, include_bias, interaction_only, d
811811

812812
assert sparse.issparse(Xt_csr) and Xt_csr.format == "csr"
813813
assert Xt_csr.dtype == Xt_dense.dtype
814-
assert_array_almost_equal(Xt_csr.A, Xt_dense)
814+
assert_array_almost_equal(Xt_csr.toarray(), Xt_dense)
815815

816816

817817
@pytest.mark.parametrize(
@@ -842,7 +842,7 @@ def test_polynomial_features_csr_X_zero_row(zero_row_index, deg, interaction_onl
842842

843843
assert sparse.issparse(Xt_csr) and Xt_csr.format == "csr"
844844
assert Xt_csr.dtype == Xt_dense.dtype
845-
assert_array_almost_equal(Xt_csr.A, Xt_dense)
845+
assert_array_almost_equal(Xt_csr.toarray(), Xt_dense)
846846

847847

848848
# This degree should always be one more than the highest degree supported by
@@ -863,7 +863,7 @@ def test_polynomial_features_csr_X_degree_4(include_bias, interaction_only):
863863

864864
assert sparse.issparse(Xt_csr) and Xt_csr.format == "csr"
865865
assert Xt_csr.dtype == Xt_dense.dtype
866-
assert_array_almost_equal(Xt_csr.A, Xt_dense)
866+
assert_array_almost_equal(Xt_csr.toarray(), Xt_dense)
867867

868868

869869
@pytest.mark.parametrize(
@@ -891,7 +891,7 @@ def test_polynomial_features_csr_X_dim_edges(deg, dim, interaction_only):
891891

892892
assert sparse.issparse(Xt_csr) and Xt_csr.format == "csr"
893893
assert Xt_csr.dtype == Xt_dense.dtype
894-
assert_array_almost_equal(Xt_csr.A, Xt_dense)
894+
assert_array_almost_equal(Xt_csr.toarray(), Xt_dense)
895895

896896

897897
@pytest.mark.parametrize("interaction_only", [True, False])

sklearn/tests/test_kernel_approximation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ def test_additive_chi2_sampler(csr_container):
115115
X_sp_trans = transform.fit_transform(csr_container(X))
116116
Y_sp_trans = transform.transform(csr_container(Y))
117117

118-
assert_array_equal(X_trans, X_sp_trans.A)
119-
assert_array_equal(Y_trans, Y_sp_trans.A)
118+
assert_array_equal(X_trans, X_sp_trans.toarray())
119+
assert_array_equal(Y_trans, Y_sp_trans.toarray())
120120

121121
# test error is raised on negative input
122122
Y_neg = Y.copy()

sklearn/utils/estimator_checks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,7 @@ def _apply_on_subsets(func, X):
14891489

14901490
if sparse.issparse(result_full):
14911491
result_full = result_full.A
1492-
result_by_batch = [x.A for x in result_by_batch]
1492+
result_by_batch = [x.toarray() for x in result_by_batch]
14931493

14941494
return np.ravel(result_full), np.ravel(result_by_batch)
14951495

0 commit comments

Comments
 (0)