11
11
# Lars Buitinck
12
12
# Maryan Morel <[email protected] >
13
13
# Giorgio Patrini <[email protected] >
14
+ # Maria Telenczuk <https://github.com/maikia>
14
15
# License: BSD 3 clause
15
16
16
17
from abc import ABCMeta , abstractmethod
49
50
# intercept oscillation.
50
51
51
52
53
+ # FIXME in 1.2: parameter 'normalize' should be removed from linear models
54
+ # in cases where now normalize=False. The default value of 'normalize' should
55
+ # be changed to False in linear models where now normalize=True
56
+ def _deprecate_normalize (normalize , default , estimator_name ):
57
+ """ Normalize is to be deprecated from linear models and a use of
58
+ a pipeline with a StandardScaler is to be recommended instead.
59
+ Here the appropriate message is selected to be displayed to the user
60
+ depending on the default normalize value (as it varies between the linear
61
+ models and normalize value selected by the user).
62
+
63
+ Parameters
64
+ ----------
65
+ normalize : bool,
66
+ normalize value passed by the user
67
+
68
+ default : bool,
69
+ default normalize value used by the estimator
70
+
71
+ estimator_name : string,
72
+ name of the linear estimator which calls this function.
73
+ The name will be used for writing the deprecation warnings
74
+
75
+ Returns
76
+ -------
77
+ normalize : bool,
78
+ normalize value which should further be used by the estimator at this
79
+ stage of the depreciation process
80
+
81
+ Notes
82
+ -----
83
+ This function should be updated in 1.2 depending on the value of
84
+ `normalize`:
85
+ - True, warning: `normalize` was deprecated in 1.2 and will be removed in
86
+ 1.4. Suggest to use pipeline instead.
87
+ - False, `normalize` was deprecated in 1.2 and it will be removed in 1.4.
88
+ Leave normalize to its default value.
89
+ - `deprecated` - this should only be possible with default == False as from
90
+ 1.2 `normalize` in all the linear models should be either removed or the
91
+ default should be set to False.
92
+ This function should be completely removed in 1.4.
93
+ """
94
+
95
+ if normalize not in [True , False , 'deprecated' ]:
96
+ raise ValueError ("Leave 'normalize' to its default value or set it "
97
+ "to True or False" )
98
+
99
+ if normalize == 'deprecated' :
100
+ _normalize = default
101
+ else :
102
+ _normalize = normalize
103
+
104
+ if default and normalize == 'deprecated' :
105
+ warnings .warn (
106
+ "The default of 'normalize' will be set to False in version 1.2 "
107
+ "and deprecated in version 1.4. \n Pass normalize=False and use "
108
+ "Pipeline with a StandardScaler in a preprocessing stage if you "
109
+ "wish to reproduce the previous behavior:\n "
110
+ "model = make_pipeline(StandardScaler(with_mean=False), \n "
111
+ f"{ estimator_name } (normalize=False))\n "
112
+ "If you wish to use additional parameters in "
113
+ "the fit() you can include them as follows:\n "
114
+ "kwargs = {model.steps[-1][0] + "
115
+ "'__<your_param_name>': <your_param_value>}\n "
116
+ "model.fit(X, y, **kwargs)" , FutureWarning
117
+ )
118
+ elif normalize != 'deprecated' and normalize and not default :
119
+ warnings .warn (
120
+ "'normalize' was deprecated in version 1.0 and will be "
121
+ "removed in 1.2 \n If you still wish to normalize use "
122
+ "Pipeline with a StandardScaler in a preprocessing stage if you "
123
+ "wish to reproduce the previous behavior:\n "
124
+ "model = make_pipeline(StandardScaler(with_mean=False), "
125
+ f"{ estimator_name } ()). \n If you wish to use additional "
126
+ "parameters in the fit() you can include them as follows: "
127
+ "kwargs = {model.steps[-1][0] + "
128
+ "'__<your_param_name>': <your_param_value>}\n "
129
+ "model.fit(X, y, **kwargs)" , FutureWarning
130
+ )
131
+ elif not normalize and not default :
132
+ warnings .warn (
133
+ "'normalize' was deprecated in version 1.0 and will be"
134
+ " removed in 1.2 Don't set 'normalize' parameter"
135
+ " and leave it to its default value" , FutureWarning
136
+ )
137
+
138
+ return _normalize
139
+
140
+
52
141
def make_dataset (X , y , sample_weight , random_state = None ):
53
142
"""Create ``Dataset`` abstraction for sparse and dense inputs.
54
143
@@ -407,6 +496,10 @@ class LinearRegression(MultiOutputMixin, RegressorMixin, LinearModel):
407
496
:class:`~sklearn.preprocessing.StandardScaler` before calling ``fit``
408
497
on an estimator with ``normalize=False``.
409
498
499
+ .. deprecated:: 1.0
500
+ `normalize` was deprecated in version 1.0 and will be
501
+ removed in 1.2.
502
+
410
503
copy_X : bool, default=True
411
504
If True, X will be copied; else, it may be overwritten.
412
505
@@ -476,8 +569,8 @@ class LinearRegression(MultiOutputMixin, RegressorMixin, LinearModel):
476
569
array([16.])
477
570
"""
478
571
@_deprecate_positional_args
479
- def __init__ (self , * , fit_intercept = True , normalize = False , copy_X = True ,
480
- n_jobs = None , positive = False ):
572
+ def __init__ (self , * , fit_intercept = True , normalize = 'deprecated' ,
573
+ copy_X = True , n_jobs = None , positive = False ):
481
574
self .fit_intercept = fit_intercept
482
575
self .normalize = normalize
483
576
self .copy_X = copy_X
@@ -507,6 +600,11 @@ def fit(self, X, y, sample_weight=None):
507
600
self : returns an instance of self.
508
601
"""
509
602
603
+ _normalize = _deprecate_normalize (
604
+ self .normalize , default = False ,
605
+ estimator_name = self .__class__ .__name__
606
+ )
607
+
510
608
n_jobs_ = self .n_jobs
511
609
512
610
accept_sparse = False if self .positive else ['csr' , 'csc' , 'coo' ]
@@ -519,7 +617,7 @@ def fit(self, X, y, sample_weight=None):
519
617
dtype = X .dtype )
520
618
521
619
X , y , X_offset , y_offset , X_scale = self ._preprocess_data (
522
- X , y , fit_intercept = self .fit_intercept , normalize = self . normalize ,
620
+ X , y , fit_intercept = self .fit_intercept , normalize = _normalize ,
523
621
copy = self .copy_X , sample_weight = sample_weight ,
524
622
return_mean = True )
525
623
@@ -651,10 +749,12 @@ def _pre_fit(X, y, Xy, precompute, normalize, fit_intercept, copy,
651
749
check_input = check_input , sample_weight = sample_weight )
652
750
if sample_weight is not None :
653
751
X , y = _rescale_data (X , y , sample_weight = sample_weight )
752
+
753
+ # FIXME: 'normalize' to be removed in 1.2
654
754
if hasattr (precompute , '__array__' ):
655
755
if (fit_intercept and not np .allclose (X_offset , np .zeros (n_features ))
656
- or normalize and not np .allclose (X_scale ,
657
- np . ones ( n_features ) )):
756
+ or normalize and not np .allclose (X_scale , np . ones ( n_features )
757
+ )):
658
758
warnings .warn (
659
759
"Gram matrix was provided but X was centered to fit "
660
760
"intercept, or X was normalized : recomputing Gram matrix." ,
0 commit comments