Skip to content

Commit e529169

Browse files
committed
pep8 stuff as well removed testing stuff
1 parent 5865a8f commit e529169

File tree

6 files changed

+589
-607
lines changed

6 files changed

+589
-607
lines changed

scikits/learn/svm/base.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
from . import _liblinear
66
from ..base import BaseEstimator
77

8+
89
def _get_class_weight(class_weight, y):
910
"""
1011
Estimate class weights for unbalanced datasets.
1112
"""
1213
if class_weight == 'auto':
1314
uy = np.unique(y)
1415
weight_label = np.asarray(uy, dtype=np.int32, order='C')
15-
weight = np.array([1.0 / np.sum(y==i) for i in uy],
16+
weight = np.array([1.0 / np.sum(y == i) for i in uy],
1617
dtype=np.float64, order='C')
1718
weight *= uy.shape[0] / np.sum(weight)
1819
else:
@@ -44,7 +45,7 @@ def __init__(self, impl, kernel, degree, gamma, coef0, cache_size,
4445

4546
if not (kernel in self._kernel_types or hasattr(kernel, '__call__')):
4647
raise ValueError("kernel should be one of %s or a callable, " \
47-
"%s was given." % ( self._kernel_types, kernel))
48+
"%s was given." % (self._kernel_types, kernel))
4849

4950
self.kernel = kernel
5051
self.impl = impl
@@ -74,7 +75,6 @@ def _get_kernel(self, X):
7475
_X = X
7576
return kernel_type, _X
7677

77-
7878
def fit(self, X, y, class_weight={}, sample_weight=[], **params):
7979
"""
8080
Fit the SVM model according to the given training data and
@@ -122,7 +122,7 @@ def fit(self, X, y, class_weight={}, sample_weight=[], **params):
122122

123123
self.class_weight, self.class_weight_label = \
124124
_get_class_weight(class_weight, y)
125-
125+
126126
# check dimensions
127127
solver_type = self._svm_types.index(self.impl)
128128
if solver_type != 2 and _X.shape[0] != y.shape[0]:
@@ -132,12 +132,12 @@ def fit(self, X, y, class_weight={}, sample_weight=[], **params):
132132

133133
if (kernel_type in [1, 2]) and (self.gamma == 0):
134134
# if custom gamma is not provided ...
135-
self.gamma = 1.0/_X.shape[0]
135+
self.gamma = 1.0 / _X.shape[0]
136136

137137
self.support_, self.support_vectors_, self.n_support_, \
138138
self.dual_coef_, self.intercept_, self.label_, self.probA_, \
139139
self.probB_ = \
140-
libsvm_train( _X, y, solver_type, kernel_type, self.degree,
140+
libsvm_train(_X, y, solver_type, kernel_type, self.degree,
141141
self.gamma, self.coef0, self.eps, self.C,
142142
self.nu, self.cache_size, self.p,
143143
self.class_weight_label, self.class_weight,
@@ -169,7 +169,7 @@ def predict(self, T):
169169
T = np.atleast_2d(np.asanyarray(T, dtype=np.float64, order='C'))
170170
kernel_type, T = self._get_kernel(T)
171171

172-
return libsvm_predict (T, self.support_vectors_,
172+
return libsvm_predict(T, self.support_vectors_,
173173
self.dual_coef_, self.intercept_,
174174
self._svm_types.index(self.impl), kernel_type,
175175
self.degree, self.gamma, self.coef0, self.eps,
@@ -249,7 +249,7 @@ def predict_log_proba(self, T):
249249

250250
def decision_function(self, T):
251251
"""
252-
Calculate the distance of the samples in T to the separating hyperplane.
252+
Calculate the distance of the samples T to the separating hyperplane.
253253
254254
Parameters
255255
----------
@@ -264,7 +264,7 @@ def decision_function(self, T):
264264
T = np.atleast_2d(np.asanyarray(T, dtype=np.float64, order='C'))
265265
kernel_type, T = self._get_kernel(T)
266266

267-
dec_func = libsvm_decision_function (T, self.support_vectors_,
267+
dec_func = libsvm_decision_function(T, self.support_vectors_,
268268
self.dual_coef_, self.intercept_,
269269
self._svm_types.index(self.impl), kernel_type,
270270
self.degree, self.gamma, self.coef0, self.eps,
@@ -275,7 +275,6 @@ def decision_function(self, T):
275275
self.support_, self.label_, self.probA_,
276276
self.probB_)
277277

278-
279278
if self.impl != 'one_class':
280279
# libsvm has the convention of returning negative values for
281280
# rightmost labels, so we invert the sign since our label_ is
@@ -297,14 +296,14 @@ class BaseLibLinear(BaseEstimator):
297296
"""
298297

299298
_solver_type_dict = {
300-
'PL2_LLR_D0' : 0, # L2 penalty, logistic regression
301-
'PL2_LL2_D1' : 1, # L2 penalty, L2 loss, dual form
302-
'PL2_LL2_D0' : 2, # L2 penalty, L2 loss, primal form
303-
'PL2_LL1_D1' : 3, # L2 penalty, L1 Loss, dual form
304-
'MC_SVC' : 4, # Multi-class Support Vector Classification
305-
'PL1_LL2_D0' : 5, # L1 penalty, L2 Loss, primal form
306-
'PL1_LLR_D0' : 6, # L1 penalty, logistic regression
307-
'PL2_LLR_D1' : 7, # L2 penalty, logistic regression, dual form
299+
'PL2_LLR_D0' : 0, # L2 penalty, logistic regression
300+
'PL2_LL2_D1' : 1, # L2 penalty, L2 loss, dual form
301+
'PL2_LL2_D0' : 2, # L2 penalty, L2 loss, primal form
302+
'PL2_LL1_D1' : 3, # L2 penalty, L1 Loss, dual form
303+
'MC_SVC' : 4, # Multi-class Support Vector Classification
304+
'PL1_LL2_D0' : 5, # L1 penalty, L2 Loss, primal form
305+
'PL1_LLR_D0' : 6, # L1 penalty, logistic regression
306+
'PL2_LLR_D1' : 7, # L2 penalty, logistic regression, dual form
308307
}
309308

310309
def __init__(self, penalty='l2', loss='l2', dual=True, eps=1e-4, C=1.0,
@@ -328,14 +327,14 @@ def _get_solver_type(self):
328327
if self.multi_class:
329328
solver_type = 'MC_SVC'
330329
else:
331-
solver_type = "P%s_L%s_D%d" % (
330+
solver_type = "P%s_L%s_D%d" % (
332331
self.penalty.upper(), self.loss.upper(), int(self.dual))
333332
if not solver_type in self._solver_type_dict:
334333
raise ValueError('Not supported set of arguments: '
335334
+ solver_type)
336335
return self._solver_type_dict[solver_type]
337336

338-
def fit(self, X, y, class_weight={},**params):
337+
def fit(self, X, y, class_weight={}, **params):
339338
"""
340339
Fit the model according to the given training data and
341340
parameters.
@@ -369,7 +368,6 @@ def fit(self, X, y, class_weight={},**params):
369368
self._get_bias(), self.C,
370369
self.class_weight_label, self.class_weight)
371370

372-
373371
return self
374372

375373
def predict(self, X):
@@ -386,9 +384,9 @@ def predict(self, X):
386384
"""
387385
X = np.asanyarray(X, dtype=np.float64, order='C')
388386
self._check_n_features(X)
389-
387+
390388
coef = self.raw_coef_
391-
389+
392390
return _liblinear.predict_wrap(X, coef,
393391
self._get_solver_type(),
394392
self.eps, self.C,
@@ -413,9 +411,9 @@ def decision_function(self, X):
413411
"""
414412
X = np.atleast_2d(np.asanyarray(X, dtype=np.float64, order='C'))
415413
self._check_n_features(X)
416-
414+
417415
coef = self.raw_coef_
418-
416+
419417
dec_func = _liblinear.decision_function_wrap(X, coef,
420418
self._get_solver_type(),
421419
self.eps, self.C,
@@ -428,24 +426,25 @@ def decision_function(self, X):
428426
return -dec_func
429427
else:
430428
return dec_func
431-
432429

433430
def _check_n_features(self, X):
434431
n_features = self.raw_coef_.shape[1]
435-
if self.fit_intercept: n_features -= 1
432+
if self.fit_intercept:
433+
n_features -= 1
436434
if X.shape[1] != n_features:
437435
raise ValueError("X.shape[1] should be %d, not %d." % (n_features,
438436
X.shape[1]))
437+
439438
@property
440439
def intercept_(self):
441440
if self.fit_intercept:
442-
return self.intercept_scaling * self.raw_coef_[:,-1]
441+
return self.intercept_scaling * self.raw_coef_[:, -1]
443442
return 0.0
444443

445444
@property
446445
def coef_(self):
447446
if self.fit_intercept:
448-
return self.raw_coef_[:,:-1]
447+
return self.raw_coef_[:, : -1]
449448
return self.raw_coef_
450449

451450
def predict_proba(self, T):

scikits/learn/svm/sparse/base.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from .. import _liblinear
99

10+
1011
class SparseBaseLibSVM(BaseLibSVM):
1112

1213
_kernel_types = ['linear', 'poly', 'rbf', 'sigmoid', 'precomputed']
@@ -21,7 +22,7 @@ def __init__(self, impl, kernel, degree, gamma, coef0, cache_size,
2122

2223
assert kernel in self._kernel_types, \
2324
"kernel should be one of %s, "\
24-
"%s was given." % ( self._kernel_types, kernel)
25+
"%s was given." % (self._kernel_types, kernel)
2526

2627
self.kernel = kernel
2728
self.impl = impl
@@ -37,20 +38,19 @@ def __init__(self, impl, kernel, degree, gamma, coef0, cache_size,
3738
self.probability = probability
3839

3940
# container for when we call fit
40-
self._support_data = np.empty (0, dtype=np.float64, order='C')
41-
self._support_indices = np.empty (0, dtype=np.int32, order='C')
42-
self._support_indptr = np.empty (0, dtype=np.int32, order='C')
41+
self._support_data = np.empty(0, dtype=np.float64, order='C')
42+
self._support_indices = np.empty(0, dtype=np.int32, order='C')
43+
self._support_indptr = np.empty(0, dtype=np.int32, order='C')
4344

4445
# strictly speaking, dual_coef is not sparse (see Notes above)
45-
self._dual_coef_data = np.empty (0, dtype=np.float64, order='C')
46-
self._dual_coef_indices = np.empty (0, dtype=np.int32, order='C')
47-
self._dual_coef_indptr = np.empty (0, dtype=np.int32, order='C')
48-
self.intercept_ = np.empty (0, dtype=np.float64, order='C')
46+
self._dual_coef_data = np.empty(0, dtype=np.float64, order='C')
47+
self._dual_coef_indices = np.empty(0, dtype=np.int32, order='C')
48+
self._dual_coef_indptr = np.empty(0, dtype=np.int32, order='C')
49+
self.intercept_ = np.empty(0, dtype=np.float64, order='C')
4950

5051
# only used in classification
5152
self.n_support = np.empty(0, dtype=np.int32, order='C')
5253

53-
5454
def fit(self, X, y, class_weight={}, sample_weight=[], **params):
5555
"""
5656
Fit the SVM model according to the given training data and
@@ -92,7 +92,7 @@ def fit(self, X, y, class_weight={}, sample_weight=[], **params):
9292
import scipy.sparse
9393
X = scipy.sparse.csr_matrix(X)
9494
X.data = np.asanyarray(X.data, dtype=np.float64, order='C')
95-
y = np.asanyarray(y, dtype=np.float64, order='C')
95+
y = np.asanyarray(y, dtype=np.float64, order='C')
9696
sample_weight = np.asanyarray(sample_weight, dtype=np.float64,
9797
order='C')
9898

@@ -104,9 +104,9 @@ def fit(self, X, y, class_weight={}, sample_weight=[], **params):
104104

105105
if (kernel_type == 2) and (self.gamma == 0):
106106
# if custom gamma is not provided ...
107-
self.gamma = 1.0/X.shape[0]
107+
self.gamma = 1.0 / X.shape[0]
108108

109-
self.label_, self.probA_, self.probB_ = libsvm_sparse_train (
109+
self.label_, self.probA_, self.probB_ = libsvm_sparse_train(
110110
X.shape[1], X.data, X.indices, X.indptr, y,
111111
solver_type, kernel_type, self.degree, self.gamma,
112112
self.coef0, self.eps, self.C, self._support_data,
@@ -128,7 +128,7 @@ def fit(self, X, y, class_weight={}, sample_weight=[], **params):
128128
self.support_vectors_ = scipy.sparse.csr_matrix((self._support_data,
129129
self._support_indices,
130130
self._support_indptr),
131-
(n_SV, X.shape[1]) )
131+
(n_SV, X.shape[1]))
132132

133133
self.dual_coef_ = scipy.sparse.csr_matrix((self._dual_coef_data,
134134
dual_coef_indices,
@@ -137,7 +137,6 @@ def fit(self, X, y, class_weight={}, sample_weight=[], **params):
137137
)
138138
return self
139139

140-
141140
def predict(self, T):
142141
"""
143142
This function does classification or regression on an array of
@@ -162,15 +161,15 @@ def predict(self, T):
162161
T.data = np.asanyarray(T.data, dtype=np.float64, order='C')
163162
kernel_type = self._kernel_types.index(self.kernel)
164163

165-
return libsvm_sparse_predict (T.data, T.indices, T.indptr,
164+
return libsvm_sparse_predict(T.data, T.indices, T.indptr,
166165
self.support_vectors_.data,
167166
self.support_vectors_.indices,
168167
self.support_vectors_.indptr,
169168
self.dual_coef_.data, self.intercept_,
170169
self._svm_types.index(self.impl), kernel_type,
171170
self.degree, self.gamma, self.coef0, self.eps,
172-
self.C, self.class_weight_label, self.class_weight, self.nu,
173-
self.cache_size, self.p, self.shrinking,
171+
self.C, self.class_weight_label, self.class_weight,
172+
self.nu, self.cache_size, self.p, self.shrinking,
174173
self.probability, self.n_support, self.label_,
175174
self.probA_, self.probB_)
176175

@@ -208,8 +207,8 @@ def fit(self, X, y, class_weight={}, **params):
208207
_liblinear.csr_train_wrap(X.shape[1], X.data, X.indices,
209208
X.indptr, y,
210209
self._get_solver_type(),
211-
self.eps, self._get_bias(), self.C, self.class_weight_label,
212-
self.class_weight)
210+
self.eps, self._get_bias(), self.C,
211+
self.class_weight_label, self.class_weight)
213212

214213
return self
215214

@@ -270,7 +269,5 @@ def decision_function(self, X):
270269
return -dec_func
271270
else:
272271
return dec_func
273-
274-
275272

276273
set_verbosity_wrap(0)

0 commit comments

Comments
 (0)