From 5bf5266eb2c39767843d5ae40c4f6b6fa4a8ae83 Mon Sep 17 00:00:00 2001 From: Gael Varoquaux <gael.varoquaux@normalesup.org> Date: Sun, 6 May 2012 19:04:19 +0200 Subject: [PATCH] ENH: make LinearSVC copyiable Fixes #820 --- sklearn/svm/base.py | 5 +++++ sklearn/svm/tests/test_svm.py | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/sklearn/svm/base.py b/sklearn/svm/base.py index 2dfe119ef0..1c56302110 100644 --- a/sklearn/svm/base.py +++ b/sklearn/svm/base.py @@ -755,6 +755,11 @@ class BaseLibLinear(BaseEstimator): raise ValueError( "cannot use sparse input in %r trained on dense data" % type(self).__name__) + if not self.raw_coef_.flags['F_CONTIGUOUS']: + warnings.warn('Coefficients are the fortran-contiguous. ' + 'Copying them.', RuntimeWarning, + stacklevel=3) + self.raw_coef_ = np.asfortranarray(self.raw_coef_) return X def _get_intercept_(self): diff --git a/sklearn/svm/tests/test_svm.py b/sklearn/svm/tests/test_svm.py index e8af5fc5f1..b77eca63f9 100644 --- a/sklearn/svm/tests/test_svm.py +++ b/sklearn/svm/tests/test_svm.py @@ -3,11 +3,13 @@ Testing for Support Vector Machine module (sklearn.svm) TODO: remove hard coded numerical results when possible """ +import copy +import warnings import numpy as np from numpy.testing import assert_array_equal, assert_array_almost_equal, \ assert_almost_equal -from nose.tools import assert_raises, assert_true +from nose.tools import assert_raises, assert_true, assert_equal from sklearn import svm, linear_model, datasets, metrics, base from sklearn.datasets.samples_generator import make_classification @@ -151,7 +153,7 @@ def test_precomputed(): assert_almost_equal(np.mean(pred == iris.target), .99, decimal=2) -def test_SVR(): +def test_svr(): """ Test Support Vector Regression """ @@ -598,6 +600,15 @@ def test_linearsvc_verbose(): os.dup2(stdout, 1) # restore original stdout +def test_linearsvc_deepcopy(): + rng = check_random_state(0) + clf = svm.LinearSVC() + clf.fit(rng.rand(10, 2), rng.randint(0, 2, size=10)) + with warnings.catch_warnings(record=True) as warn_queue: + copy.deepcopy(clf).predict(rng.rand(2)) + assert_equal(len(warn_queue), 1) + + def test_svc_clone_with_callable_kernel(): a = svm.SVC(kernel=lambda x, y: np.dot(x, y.T), probability=True) b = base.clone(a) -- GitLab