55from . import _liblinear
66from ..base import BaseEstimator
77
8+
89def _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 ):
0 commit comments