Skip to content

Commit f9687ca

Browse files
qinhanmin2014jnothman
authored andcommitted
TST Manually scramble the indices in svm tests (scikit-learn#12890)
* [scipy-dev] manually scramble the indices * [scipy-dev] review comment
1 parent 9498fa6 commit f9687ca

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

sklearn/svm/tests/test_sparse.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,19 @@ def test_unsorted_indices():
111111
# make sure dense and sparse SVM give the same result
112112
assert_array_almost_equal(coef_dense, coef_sorted.toarray())
113113

114-
X_sparse_unsorted = X_sparse[np.arange(X.shape[0])]
115-
X_test_unsorted = X_test[np.arange(X_test.shape[0])]
114+
# reverse each row's indices
115+
def scramble_indices(X):
116+
new_data = []
117+
new_indices = []
118+
for i in range(1, len(X.indptr)):
119+
row_slice = slice(*X.indptr[i - 1: i + 1])
120+
new_data.extend(X.data[row_slice][::-1])
121+
new_indices.extend(X.indices[row_slice][::-1])
122+
return sparse.csr_matrix((new_data, new_indices, X.indptr),
123+
shape=X.shape)
124+
125+
X_sparse_unsorted = scramble_indices(X_sparse)
126+
X_test_unsorted = scramble_indices(X_test)
116127

117128
# make sure we scramble the indices
118129
assert_false(X_sparse_unsorted.has_sorted_indices)

0 commit comments

Comments
 (0)