|
5 | 5 | from numpy.testing import (assert_array_almost_equal, assert_array_equal,
|
6 | 6 | assert_equal)
|
7 | 7 |
|
8 |
| -from nose.tools import assert_raises, assert_true |
| 8 | +from nose.tools import assert_raises, assert_true, assert_false |
9 | 9 | from nose.tools import assert_equal as nose_assert_equal
|
10 |
| -from sklearn.datasets.samples_generator import make_classification |
| 10 | +from sklearn.datasets import make_classification, load_digits |
11 | 11 | from sklearn.svm.tests import test_svm
|
12 | 12 | from sklearn.utils import ConvergenceWarning
|
13 | 13 | from sklearn.utils.extmath import safe_sparse_dot
|
@@ -69,6 +69,37 @@ def test_svc():
|
69 | 69 | sp_clf.predict_proba(T2), 4)
|
70 | 70 |
|
71 | 71 |
|
| 72 | +def test_unsorted_indices(): |
| 73 | + # test that the result with sorted and unsorted indices in csr is the same |
| 74 | + # we use a subset of digits as iris, blobs or make_classification didn't |
| 75 | + # show the problem |
| 76 | + digits = load_digits() |
| 77 | + X, y = digits.data[:50], digits.target[:50] |
| 78 | + X_test = sparse.csr_matrix(digits.data[50:100]) |
| 79 | + |
| 80 | + X_sparse = sparse.csr_matrix(X) |
| 81 | + coef_dense = svm.SVC(kernel='linear', probability=True).fit(X, y).coef_ |
| 82 | + sparse_svc = svm.SVC(kernel='linear', probability=True).fit(X_sparse, y) |
| 83 | + coef_sorted = sparse_svc.coef_ |
| 84 | + # make sure dense and sparse SVM give the same result |
| 85 | + assert_array_almost_equal(coef_dense, coef_sorted.toarray()) |
| 86 | + |
| 87 | + X_sparse_unsorted = X_sparse[np.arange(X.shape[0])] |
| 88 | + X_test_unsorted = X_test[np.arange(X_test.shape[0])] |
| 89 | + |
| 90 | + # make sure we scramble the indices |
| 91 | + assert_false(X_sparse_unsorted.has_sorted_indices) |
| 92 | + assert_false(X_test_unsorted.has_sorted_indices) |
| 93 | + |
| 94 | + unsorted_svc = svm.SVC(kernel='linear', |
| 95 | + probability=True).fit(X_sparse_unsorted, y) |
| 96 | + coef_unsorted = unsorted_svc.coef_ |
| 97 | + # make sure unsorted indices give same result |
| 98 | + assert_array_almost_equal(coef_unsorted.toarray(), coef_sorted.toarray()) |
| 99 | + assert_array_almost_equal(sparse_svc.predict_proba(X_test_unsorted), |
| 100 | + sparse_svc.predict_proba(X_test)) |
| 101 | + |
| 102 | + |
72 | 103 | def test_svc_with_custom_kernel():
|
73 | 104 | kfunc = lambda x, y: safe_sparse_dot(x, y.T)
|
74 | 105 | clf_lin = svm.SVC(kernel='linear').fit(X_sp, Y)
|
|
0 commit comments