@@ -550,12 +550,12 @@ def logistic_regression_path(X, y, pos_class=None, Cs=10, fit_intercept=True,
550550 pos_class = classes [1 ]
551551
552552 # 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
554554 # the class_weights are assigned after masking the labels with a OvR.
555555 sample_weight = np .ones (X .shape [0 ])
556556 le = LabelEncoder ()
557557
558- if isinstance (class_weight , dict ):
558+ if isinstance (class_weight , dict ) or multi_class == 'multinomial' :
559559 if solver == "liblinear" :
560560 if classes .size == 2 :
561561 # 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,
567567 "solver cannot handle multiclass with "
568568 "class_weight of type dict. Use the lbfgs, "
569569 "newton-cg solvers or set "
570- "class_weight='auto '" )
570+ "class_weight='balanced '" )
571571 else :
572572 class_weight_ = compute_class_weight (class_weight , classes , y )
573573 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,
576576 # multinomial case this is not necessary.
577577 if multi_class == 'ovr' :
578578 w0 = np .zeros (n_features + int (fit_intercept ))
579- mask_classes = [- 1 , 1 ]
579+ mask_classes = np . array ( [- 1 , 1 ])
580580 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 )]
586589
587590 else :
588591 lbin = LabelBinarizer ()
@@ -591,11 +594,6 @@ def logistic_regression_path(X, y, pos_class=None, Cs=10, fit_intercept=True,
591594 Y_bin = np .hstack ([1 - Y_bin , Y_bin ])
592595 w0 = np .zeros ((Y_bin .shape [1 ], n_features + int (fit_intercept )),
593596 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 )]
599597
600598 if coef is not None :
601599 # 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,
632630 grad = lambda x , * args : _multinomial_loss_grad (x , * args )[1 ]
633631 hess = _multinomial_grad_hess
634632 else :
635- target = y
633+ target = y_bin
636634 if solver == 'lbfgs' :
637635 func = _logistic_loss_and_grad
638636 elif solver == 'newton-cg' :
@@ -664,7 +662,7 @@ def logistic_regression_path(X, y, pos_class=None, Cs=10, fit_intercept=True,
664662 tol = tol )
665663 elif solver == 'liblinear' :
666664 coef_ , intercept_ , _ , = _fit_liblinear (
667- X , y , C , fit_intercept , intercept_scaling , class_weight ,
665+ X , target , C , fit_intercept , intercept_scaling , class_weight ,
668666 penalty , dual , verbose , max_iter , tol , random_state )
669667 if fit_intercept :
670668 w0 = np .concatenate ([coef_ .ravel (), intercept_ ])
0 commit comments