Skip to content

Commit a115e91

Browse files
committed
COSMIT refactor LibSVM wrapper for safety and readability
* don't pass fit-time parameters to prediction functions * do memory deallocation in a finally clause * trim docstrings and comments TODO: * fit needs the finally clause as well * sparse LibSVM should get the same treatment (test_svc_clone_with_callable_kernel might fail sometimes. That appears to be unrelated.)
1 parent ca2142e commit a115e91

File tree

4 files changed

+2298
-2022
lines changed

4 files changed

+2298
-2022
lines changed

sklearn/svm/base.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ class BaseLibSVM(six.with_metaclass(ABCMeta, BaseEstimator)):
5959
6060
Parameter documentation is in the derived `SVC` class.
6161
"""
62-
# see ./classes.py for SVC class.
6362

63+
# The order of these must match the integer values in LibSVM.
64+
# XXX These are actually the same in the dense case. Need to factor
65+
# this out.
6466
_sparse_kernels = ["linear", "poly", "rbf", "sigmoid", "precomputed"]
6567

6668
@abstractmethod
@@ -291,13 +293,10 @@ def _dense_predict(self, X):
291293

292294
return libsvm.predict(
293295
X, self.support_, self.support_vectors_, self.n_support_,
294-
self.dual_coef_, self._intercept_,
295-
self._label, self.probA_, self.probB_,
296-
svm_type=svm_type,
297-
kernel=kernel, C=C, nu=self.nu,
298-
probability=self.probability, degree=self.degree,
299-
shrinking=self.shrinking, tol=self.tol, cache_size=self.cache_size,
300-
coef0=self.coef0, gamma=self._gamma, epsilon=self.epsilon)
296+
self.dual_coef_, self._intercept_, self._label,
297+
self.probA_, self.probB_, svm_type=svm_type, kernel=kernel,
298+
degree=self.degree, coef0=self.coef0, gamma=self._gamma,
299+
cache_size=self.cache_size)
301300

302301
def _sparse_predict(self, X):
303302
X = sp.csr_matrix(X, dtype=np.float64)
@@ -354,8 +353,6 @@ def decision_function(self, X):
354353
X = self._validate_for_predict(X)
355354
X = self._compute_kernel(X)
356355

357-
C = 0.0 # C is not useful here
358-
359356
kernel = self.kernel
360357
if callable(kernel):
361358
kernel = 'precomputed'
@@ -365,10 +362,8 @@ def decision_function(self, X):
365362
self.dual_coef_, self._intercept_, self._label,
366363
self.probA_, self.probB_,
367364
svm_type=LIBSVM_IMPL.index(self._impl),
368-
kernel=kernel, C=C, nu=self.nu,
369-
probability=self.probability, degree=self.degree,
370-
shrinking=self.shrinking, tol=self.tol, cache_size=self.cache_size,
371-
coef0=self.coef0, gamma=self._gamma, epsilon=self.epsilon)
365+
kernel=kernel, degree=self.degree, cache_size=self.cache_size,
366+
coef0=self.coef0, gamma=self._gamma)
372367

373368
# In binary case, we need to flip the sign of coef, intercept and
374369
# decision function.
@@ -539,10 +534,8 @@ def _dense_predict_proba(self, X):
539534
X, self.support_, self.support_vectors_, self.n_support_,
540535
self.dual_coef_, self._intercept_, self._label,
541536
self.probA_, self.probB_,
542-
svm_type=svm_type, kernel=kernel, C=C, nu=self.nu,
543-
probability=self.probability, degree=self.degree,
544-
shrinking=self.shrinking, tol=self.tol, cache_size=self.cache_size,
545-
coef0=self.coef0, gamma=self._gamma, epsilon=self.epsilon)
537+
svm_type=svm_type, kernel=kernel, degree=self.degree,
538+
cache_size=self.cache_size, coef0=self.coef0, gamma=self._gamma)
546539

547540
return pprob
548541

0 commit comments

Comments
 (0)