16
16
import numpy as np
17
17
from scipy import optimize , sparse
18
18
19
- from .base import LinearClassifierMixin , SparseCoefMixin , BaseEstimator
19
+ from .base import (LinearClassifierMixin , SparseCoefMixin , BaseEstimator ,
20
+ LinearModel )
20
21
from .sag import sag_solver
21
22
from ..feature_selection .from_model import _LearntSelectorMixin
22
23
from ..preprocessing import LabelEncoder , LabelBinarizer
@@ -987,7 +988,9 @@ class LogisticRegression(BaseEstimator, LinearClassifierMixin,
987
988
988
989
fit_intercept : bool, default: True
989
990
Specifies if a constant (a.k.a. bias or intercept) should be
990
- added to the decision function.
991
+ added to the decision function. If set to false, no intercept
992
+ will be used in calculations (e.g. data is expected to be already
993
+ centered).
991
994
992
995
intercept_scaling : float, default: 1
993
996
Useful only if solver is liblinear.
@@ -1001,6 +1004,19 @@ class LogisticRegression(BaseEstimator, LinearClassifierMixin,
1001
1004
To lessen the effect of regularization on synthetic feature weight
1002
1005
(and therefore on the intercept) intercept_scaling has to be increased.
1003
1006
1007
+ normalize : boolean, optional, default: False
1008
+ If True, the regressors X will be normalized before regression.
1009
+ This parameter is ignored when `fit_intercept` is set to False.
1010
+ When the regressors are normalized, note that this makes the
1011
+ hyperparameters learnt more robust and almost independent of the number
1012
+ of samples. The same property is not valid for standardized data.
1013
+ However, if you wish to standardize, please use
1014
+ `preprocessing.StandardScaler` before calling `fit` on an estimator
1015
+ with `normalize=False`.
1016
+
1017
+ copy_X : boolean, optional, default: True
1018
+ If True, X will be copied; else, it may be overwritten.
1019
+
1004
1020
class_weight : dict or 'balanced', default: None
1005
1021
Weights associated with classes in the form ``{class_label: weight}``.
1006
1022
If not given, all classes are supposed to have weight one.
@@ -1114,16 +1130,19 @@ class LogisticRegression(BaseEstimator, LinearClassifierMixin,
1114
1130
"""
1115
1131
1116
1132
def __init__ (self , penalty = 'l2' , dual = False , tol = 1e-4 , C = 1.0 ,
1117
- fit_intercept = True , intercept_scaling = 1 , class_weight = None ,
1118
- random_state = None , solver = 'liblinear' , max_iter = 100 ,
1119
- multi_class = 'ovr' , verbose = 0 , warm_start = False , n_jobs = 1 ):
1133
+ fit_intercept = True , intercept_scaling = 1 , normalize = False ,
1134
+ copy_X = True , class_weight = None , random_state = None ,
1135
+ solver = 'liblinear' , max_iter = 100 , multi_class = 'ovr' ,
1136
+ verbose = 0 , warm_start = False , n_jobs = 1 ):
1120
1137
1121
1138
self .penalty = penalty
1122
1139
self .dual = dual
1123
1140
self .tol = tol
1124
1141
self .C = C
1125
1142
self .fit_intercept = fit_intercept
1126
1143
self .intercept_scaling = intercept_scaling
1144
+ self .normalize = normalize
1145
+ self .copy_X = copy_X
1127
1146
self .class_weight = class_weight
1128
1147
self .random_state = random_state
1129
1148
self .solver = solver
@@ -1176,13 +1195,18 @@ def fit(self, X, y, sample_weight=None):
1176
1195
_check_solver_option (self .solver , self .multi_class , self .penalty ,
1177
1196
self .dual )
1178
1197
1198
+ X , y , X_offset , y_offset , X_scale = self ._preprocess_data (
1199
+ X , y , self .fit_intercept , self .normalize , self .copy_X ,
1200
+ sample_weight = sample_weight )
1201
+
1179
1202
if self .solver == 'liblinear' :
1180
1203
self .coef_ , self .intercept_ , n_iter_ = _fit_liblinear (
1181
1204
X , y , self .C , self .fit_intercept , self .intercept_scaling ,
1182
1205
self .class_weight , self .penalty , self .dual , self .verbose ,
1183
1206
self .max_iter , self .tol , self .random_state ,
1184
1207
sample_weight = sample_weight )
1185
1208
self .n_iter_ = np .array ([n_iter_ ])
1209
+ self ._set_intercept (X_offset , y_offset , X_scale , self .intercept_ )
1186
1210
return self
1187
1211
1188
1212
if self .solver == 'sag' :
@@ -1251,6 +1275,7 @@ def fit(self, X, y, sample_weight=None):
1251
1275
if self .fit_intercept :
1252
1276
self .intercept_ = self .coef_ [:, - 1 ]
1253
1277
self .coef_ = self .coef_ [:, :- 1 ]
1278
+ self ._set_intercept (X_offset , y_offset , X_scale , self .intercept_ )
1254
1279
1255
1280
return self
1256
1281
0 commit comments