@@ -550,12 +550,12 @@ def logistic_regression_path(X, y, pos_class=None, Cs=10, fit_intercept=True,
550
550
pos_class = classes [1 ]
551
551
552
552
# If class_weights is a dict (provided by the user), the weights
553
- # are assigned to the original labels. If it is "auto ", then
553
+ # are assigned to the original labels. If it is "balanced ", then
554
554
# the class_weights are assigned after masking the labels with a OvR.
555
555
sample_weight = np .ones (X .shape [0 ])
556
556
le = LabelEncoder ()
557
557
558
- if isinstance (class_weight , dict ):
558
+ if isinstance (class_weight , dict ) or multi_class == 'multinomial' :
559
559
if solver == "liblinear" :
560
560
if classes .size == 2 :
561
561
# Reconstruct the weights with keys 1 and -1
@@ -567,7 +567,7 @@ def logistic_regression_path(X, y, pos_class=None, Cs=10, fit_intercept=True,
567
567
"solver cannot handle multiclass with "
568
568
"class_weight of type dict. Use the lbfgs, "
569
569
"newton-cg solvers or set "
570
- "class_weight='auto '" )
570
+ "class_weight='balanced '" )
571
571
else :
572
572
class_weight_ = compute_class_weight (class_weight , classes , y )
573
573
sample_weight = class_weight_ [le .fit_transform (y )]
@@ -576,13 +576,16 @@ def logistic_regression_path(X, y, pos_class=None, Cs=10, fit_intercept=True,
576
576
# multinomial case this is not necessary.
577
577
if multi_class == 'ovr' :
578
578
w0 = np .zeros (n_features + int (fit_intercept ))
579
- mask_classes = [- 1 , 1 ]
579
+ mask_classes = np . array ( [- 1 , 1 ])
580
580
mask = (y == pos_class )
581
- y [mask ] = 1
582
- y [~ mask ] = - 1
583
- # To take care of object dtypes, i.e 1 and -1 are in the form of
584
- # strings.
585
- y = as_float_array (y , copy = False )
581
+ y_bin = np .ones (y .shape , dtype = np .float64 )
582
+ y_bin [~ mask ] = - 1.
583
+ # for compute_class_weight
584
+
585
+ if class_weight in ("auto" , "balanced" ):
586
+ class_weight_ = compute_class_weight (class_weight , mask_classes ,
587
+ y_bin )
588
+ sample_weight = class_weight_ [le .fit_transform (y_bin )]
586
589
587
590
else :
588
591
lbin = LabelBinarizer ()
@@ -591,11 +594,6 @@ def logistic_regression_path(X, y, pos_class=None, Cs=10, fit_intercept=True,
591
594
Y_bin = np .hstack ([1 - Y_bin , Y_bin ])
592
595
w0 = np .zeros ((Y_bin .shape [1 ], n_features + int (fit_intercept )),
593
596
order = 'F' )
594
- mask_classes = classes
595
-
596
- if class_weight == "auto" :
597
- class_weight_ = compute_class_weight (class_weight , mask_classes , y )
598
- sample_weight = class_weight_ [le .fit_transform (y )]
599
597
600
598
if coef is not None :
601
599
# it must work both giving the bias term and not
@@ -632,7 +630,7 @@ def logistic_regression_path(X, y, pos_class=None, Cs=10, fit_intercept=True,
632
630
grad = lambda x , * args : _multinomial_loss_grad (x , * args )[1 ]
633
631
hess = _multinomial_grad_hess
634
632
else :
635
- target = y
633
+ target = y_bin
636
634
if solver == 'lbfgs' :
637
635
func = _logistic_loss_and_grad
638
636
elif solver == 'newton-cg' :
@@ -664,7 +662,7 @@ def logistic_regression_path(X, y, pos_class=None, Cs=10, fit_intercept=True,
664
662
tol = tol )
665
663
elif solver == 'liblinear' :
666
664
coef_ , intercept_ , _ , = _fit_liblinear (
667
- X , y , C , fit_intercept , intercept_scaling , class_weight ,
665
+ X , target , C , fit_intercept , intercept_scaling , class_weight ,
668
666
penalty , dual , verbose , max_iter , tol , random_state )
669
667
if fit_intercept :
670
668
w0 = np .concatenate ([coef_ .ravel (), intercept_ ])
0 commit comments