8000 ENH liblinear: get rid of n_class sign by switching class signs in li… · scikit-learn/scikit-learn@5427f81 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5427f81

Browse files
committed
ENH liblinear: get rid of n_class sign by switching class signs in liblinar implementation.
1 parent d0d595d commit 5427f81

File tree

4 files changed

+14
-122
lines changed

4 files changed

+14
-122
lines changed

sklearn/svm/base.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -518,8 +518,6 @@ def _check_n_features(self, X):
518518
def intercept_(self):
519519
if self.fit_intercept:
520520
ret = self.intercept_scaling * self.raw_coef_[:, -1]
521-
if len(self.label_) <= 2:
522-
ret *= -1
523521
return ret
524522
return 0.0
525523

@@ -532,13 +530,8 @@ def coef_(self):
532530

533531
# as coef_ is readonly property, mark the returned value as immutable
534532
# to avoid silencing potential bugs
535-
if len(self.label_) <= 2:
536-
ret *= -1
537-
ret.flags.writeable = False
538-
return ret
539-
else:
540-
ret.flags.writeable = False
541-
return ret
533+
ret.flags.writeable = False
534+
return ret
542535

543536
def _get_bias(self):
544537
if self.fit_intercept:

sklearn/svm/liblinear.c

Lines changed: 5 additions & 99 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sklearn/svm/liblinear.pyx

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,6 @@ def decision_function_wrap(
192192
if copy_predict_values(T.data, model, T.shape, dec_values.data, n_class) < 0:
193193
raise MemoryError("We've run out of of memory")
194194

195-
if n_class <= 2:
196-
# in the two-class case, the decision sign needs be flipped
197-
# due to liblinear's design
198-
dec_values *= -1
199-
200195
### FREE
201196
free_parameter(param)
202197
free_and_destroy_model(&model)
@@ -241,11 +236,6 @@ def csr_decision_function_wrap(
241236
dec_values.data, n_class) < 0:
242237
raise MemoryError("We've run out of of memory")
243238

244-
if n_class <= 2:
245-
# in the two-class case, the decision sign needs be flipped
246-
# due to liblinear's design
247-
dec_values *= -1
248-
249239
### FREE
250240
free_parameter(param)
251241
free_and_destroy_model(&model)

sklearn/svm/src/liblinear/linear.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,9 +1925,9 @@ model* train(const problem *prob, const parameter *param)
19251925
int e0 = start[0]+count[0];
19261926
k=0;
19271927
for(; k<e0; k++)
1928-
sub_prob.y[k] = +1;
1929-
for(; k<sub_prob.l; k++)
19301928
sub_prob.y[k] = -1;
1929+
for(; k<sub_prob.l; k++)
1930+
sub_prob.y[k] = +1;
19311931

19321932
train_one(&sub_prob, param, &model_->w[0], weighted_C[0], weighted_C[1]);
19331933
}
@@ -2053,7 +2053,7 @@ int predict_values(const struct model *model_, const struct feature_node *x, dou
20532053
}
20542054

20552055
if(nr_class==2)
2056-
return (dec_values[0]>0)?model_->label[0]:model_->label[1];
2056+
return (dec_values[0]>0)?model_->label[1]:model_->label[0];
20572057
else
20582058
{
20592059
int dec_max_idx = 0;
@@ -2091,7 +2091,10 @@ int predict_probability(const struct model *model_, const struct feature_node *x
20912091
prob_estimates[i]=1/(1+exp(-prob_estimates[i]));
20922092

20932093
if(nr_class==2) // for binary classification
2094-
prob_estimates[1]=1.-prob_estimates[0];
2094+
{
2095+
prob_estimates[1]=prob_estimates[0];
2096+
prob_estimates[0]=1.-prob_estimates[0];
2097+
}
20952098
else
20962099
{
20972100
double sum=0;

0 commit comments

Comments
 (0)
0