Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions sklearn/svm/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,15 @@ def _sparse_fit(self, X, y, sample_weight, solver_type, kernel,
n_class = 1
n_SV = self.support_vectors_.shape[0]

dual_coef_indices = np.tile(np.arange(n_SV), n_class)
dual_coef_indptr = np.arange(0, dual_coef_indices.size + 1,
dual_coef_indices.size / n_class)
self.dual_coef_ = sp.csr_matrix(
(dual_coef_data, dual_coef_indices, dual_coef_indptr),
(n_class, n_SV))
if n_SV == 0 or dual_coef_data.size == 0:
self.dual_coef_ = sp.csr_matrix((n_class, 0), dtype=np.float64)
else:
dual_coef_indices = np.tile(np.arange(n_SV), n_class)
dual_coef_indptr = np.arange(0, dual_coef_indices.size + 1,
dual_coef_indices.size / n_class)
self.dual_coef_ = sp.csr_matrix(
(dual_coef_data, dual_coef_indices, dual_coef_indptr),
(n_class, n_SV))

def predict(self, X):
"""Perform regression on samples in X.
Expand Down Expand Up @@ -907,9 +910,10 @@ def _fit_liblinear(X, y, C, fit_intercept, intercept_scaling, class_weight,
bias = -1.0
if fit_intercept:
if intercept_scaling <= 0:
raise ValueError("Intercept scaling is %r but needs to be greater than 0."
" To disable fitting an intercept,"
" set fit_intercept=False." % intercept_scaling)
raise ValueError(
"Intercept scaling is %r but needs to be greater than 0."
" To disable fitting an intercept, set fit_intercept=False."
% intercept_scaling)
else:
bias = intercept_scaling

Expand Down
30 changes: 29 additions & 1 deletion sklearn/svm/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,34 @@
iris.data = sparse.csr_matrix(iris.data)


def test_sparse_svr_empty_support_vectors():
X = np.array([[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 1, 0],
[0, 0, 0, 1]], dtype=np.float64)
y = np.array([0.04, 0.04, 0.10, 0.16], dtype=np.float64)
params = dict(kernel='linear', gamma=1.0, C=316.227766017, epsilon=0.1)

dense_model = svm.SVR(**params).fit(X, y)
assert dense_model.support_vectors_.shape == (0, X.shape[1])

X_sparse = sparse.csr_matrix(X)
sparse_model = svm.SVR(**params).fit(X_sparse, y)

assert sparse.isspmatrix_csr(sparse_model.support_vectors_)
assert sparse.isspmatrix_csr(sparse_model.dual_coef_)
assert sparse_model.support_vectors_.shape == (0, X.shape[1])
assert sparse_model.dual_coef_.shape == (1, 0)
assert sparse_model.dual_coef_.dtype == np.float64
assert sparse_model.support_.size == 0
assert sparse_model.dual_coef_.nnz == 0

assert_array_equal(sparse_model._n_support, dense_model._n_support)
assert_array_equal(sparse_model.support_, dense_model.support_)
assert_array_almost_equal(sparse_model.predict(X_sparse),
dense_model.predict(X))


def check_svm_model_equal(dense_svm, sparse_svm, X_train, y_train, X_test):
dense_svm.fit(X_train.toarray(), y_train)
if sparse.isspmatrix(X_test):
Expand Down Expand Up @@ -149,7 +177,7 @@ def test_svc_iris():
for k in ('linear', 'poly', 'rbf'):
sp_clf = svm.SVC(kernel=k).fit(iris.data, iris.target)
clf = svm.SVC(kernel=k).fit(iris.data.toarray(),
iris.target)
iris.target)

assert_array_almost_equal(clf.support_vectors_,
sp_clf.support_vectors_.toarray())
Expand Down