-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathols_step_sk.py
More file actions
413 lines (385 loc) · 15.8 KB
/
ols_step_sk.py
File metadata and controls
413 lines (385 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
"""forward/backward features selection based on AIC/BIC.
This module do classical forward or backward selection
based on AIC on BIC using SciKit-Learn LinearRegression
(found in sklearn.linear_model)
"""
from sklearn.linear_model import LinearRegression
import numpy as np
import pandas as pd
class LinearRegressionSelectionFeatureIC(LinearRegression):
"""
Ordinary least squares Linear Regression with feature selection (AIC/BIC).
LinearRegression fits a linear model with coefficients w = (w1, ..., wp)
to minimize the residual sum of squares between the observed targets in
the dataset, and the targets predicted by the linear approximation. Feature
are selected using backward/forward/both algorithm using BIC or AIC.
Parameters
----------
fit_intercept : bool, default=True
Whether to calculate the intercept for this model. If set
to False, no intercept will be used in calculations
(i.e. data is expected to be centered).
normalize : bool, default=False
This parameter is ignored when ``fit_intercept`` is set to False.
If True, the regressors X will be normalized before regression by
subtracting the mean and dividing by the l2-norm.
If you wish to standardize, please use
:class:`~sklearn.preprocessing.StandardScaler` before calling ``fit``
on an estimator with ``normalize=False``.
.. deprecated:: 1.0
`normalize` was deprecated in version 1.0 and will be
removed in 1.2.
copy_X : bool, default=True
If True, X will be copied; else, it may be overwritten.
n_jobs : int, default=None
The number of jobs to use for the computation. This will only provide
speedup in case of sufficiently large problems, that is if firstly
`n_targets > 1` and secondly `X` is sparse or if `positive` is set
to `True`. ``None`` means 1 unless in a
:obj:`joblib.parallel_backend` context. ``-1`` means using all
processors. See :term:`Glossary <n_jobs>` for more details.
positive : bool, default=False
When set to ``True``, forces the coefficients to be positive. This
option is only supported for dense arrays.
start : a list of int giving the columns index of the starting model
(ie the starting point); if empty only intercept in model
(defaut is empty)
lower : a list of int giving the columns index of the lower model
(ie the minimal model allowed); if empty only intercept in model
(defaut is empty)
upper : a list of int giving the columns index of the upper model
(ie the maximal model allowed) or "max" to select all variable
in X (default to "max")
direction : either "both", "forward" or "backward"
crit : either "aic"/"AIC" or "bic"/"BIC"
verbose : int, if 0 no verbose
Returns
----------
coef_ : array of shape (n_features, ) or (n_targets, n_features)
Estimated coefficients for the linear regression problem.
If multiple targets are passed during the fit (y 2D), this
is a 2D array of shape (n_targets, n_features), while if only
one target is passed, this is a 1D array of length n_features.
rank_ : int
Rank of matrix `X`. Only available when `X` is dense.
singular_ : array of shape (min(X, y),)
Singular values of `X`. Only available when `X` is dense.
intercept_ : float or array of shape (n_targets,)
Independent term in the linear model. Set to 0.0 if
`fit_intercept = False`.
n_features_in_ : int
Number of features seen during :term:`fit`.
.. versionadded:: 0.24
feature_names_in_ : ndarray of shape (`n_features_in_`,)
Names of features seen during :term:`fit`. Defined only when `X`
has feature names that are all strings.
.. versionadded:: 1.0
selected_features_ : list of variables included in the model
See Also
--------
Ridge : Ridge regression addresses some of the
problems of Ordinary Least Squares by imposing a penalty on the
size of the coefficients with l2 regularization.
Lasso : The Lasso is a linear model that estimates
sparse coefficients with l1 regularization.
ElasticNet : Elastic-Net is a linear regression
model trained with both l1 and l2 -norm regularization of the
coefficients.
"""
def __init__(
self,
*,
fit_intercept=True,
normalize="deprecated",
copy_X=True,
n_jobs=None,
positive=False,
start=[],
lower=[],
upper="max",
crit="bic",
direction="both",
verbose=0,
):
"""Setter for LinearRegressionSelectionFeatureIC class.
Initialize object with all needed attributes.
"""
self.fit_intercept = fit_intercept
self.normalize = normalize
self.copy_X = copy_X
self.n_jobs = n_jobs
self.positive = positive
self.start = start
self.lower = lower
self.upper = upper
self.crit = crit
self.direction = direction
self.verbose = verbose
def fit(self, X, y, sample_weight=None):
"""
Fit linear model.
Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
Training data.
y : array-like of shape (n_samples,) or (n_samples, n_targets)
Target values. Will be cast to X's dtype if necessary.
sample_weight : array-like of shape (n_samples,), default=None
Individual weights for each sample.
Returns
-------
self : object
Fitted Estimator.
"""
sel = self.olsstep(X, y)
self.selected_features_ = sel
if len(sel)>0:
if self.verbose>1:
print("starting fitting")
if isinstance(X, pd.DataFrame):
XX = X.iloc[:, sel]
else:
XX = np.copy(X[:, sel])
return super().fit(XX, y, sample_weight)
else:
if self.verbose>1:
print("starting fitting")
XX = np.ones((X.shape[0], 1))
return super().fit(XX, y, sample_weight)
def olsstep(self, XX, yy):
"""Stepwise selection for linear model with sklearn.
Parameters:
-----------
self: object of class LinearRegressionSelectionFeatureIC
XX (pandas DataFrame or numpy array): Dataframe or array with all
possible predictors
yy (pandas DataFrame or numpy array): dataframe or array with response
Returns:
--------
list: list of indexes selected by selected by forward/backward
or both algorithm with crit criterion (intercept is excluded)
"""
# direction
if self.verbose>1:
print("selection step (", XX.shape[1],"is intercept )")
if isinstance(self.upper, str):
if self.upper == "max":
self.upper = list(range(XX.shape[1]))
else:
raise ValueError("upper must be a list of int or 'max'")
if not (self.direction == "both" or self.direction == "forward" or
self.direction == "backward"):
raise ValueError(
"direction error (should be both, forward or backward)")
# self.criterion
if not (self.crit == "aic" or self.crit == "AIC" or
self.crit == "bic" or self.crit == "BIC"):
raise ValueError("criterion error (should be AIC/aic or BIC/bic)")
# dimensions
n = XX.shape[0]
p = XX.shape[1]
# test of indexes
if len(self.start) > 0:
res = test_index(p, self.start)
if not res:
raise ValueError("index error in start")
if len(self.lower) > 0:
res = test_index(p, self.lower)
if not res:
raise ValueError("index error in lower")
if len(self.upper) == 0:
raise ValueError("no index in upper")
else:
res = test_index(p, self.upper)
if not res:
raise ValueError("index error in upper")
# use numpy ndarray and intercept
if isinstance(XX, pd.DataFrame):
X = np.append(XX.values, np.ones((n, 1)), axis=1)
else:
X = np.append(XX, np.ones((n, 1)), axis=1)
if isinstance(yy, pd.DataFrame):
y = yy.values
else:
y = yy
# explanatory variables for the 3 models (and add intercept)
if len(self.start) > 0:
start_explanatory = set(self.start) | {p}
else:
start_explanatory = {p}
if len(self.lower) > 0:
lower_explanatory = set(self.lower) | {p}
else:
lower_explanatory = {p}
upper_explanatory = set(self.upper) | {p}
# setting up the set "add" which contains the possible variable to add
if self.direction == "both" or self.direction == "forward":
add = upper_explanatory - start_explanatory
# setting up the set "remove" which contains the
# possible variable to remove
if self.direction == "both" or self.direction == "backward":
remove = start_explanatory - lower_explanatory
# current point
selected = start_explanatory
Xs = X[:, list(selected)]
reglin = LinearRegression(fit_intercept=False).fit(Xs, y)
if self.crit == "aic" or self.crit == "AIC":
current_score = aic(reglin, Xs, y)
elif self.crit == "bic" or self.crit == "BIC":
current_score = bic(reglin, Xs, y)
if self.verbose > 1:
print("----------------------------------------------")
print(f"Crit: {current_score:.3e}, Starting with: {selected}")
# main loop
while True:
scores_with_candidates = []
if self.direction == "both" or self.direction == "backward":
for candidate in remove:
tobetested = selected - set([candidate])
Xtbt = X[:, list(tobetested)]
if self.verbose > 2:
print("----------------------------------------------")
print(f"var: {tobetested} == (n, p): {Xtbt.shape}")
reglin = LinearRegression(fit_intercept=False).fit(Xtbt, y)
if self.crit == "aic" or self.crit == "AIC":
score = aic(reglin, Xtbt, y)
elif self.crit == "bic" or self.crit == "BIC":
score = bic(reglin, Xtbt, y)
if self.verbose > 2:
print((score, "-", candidate))
scores_with_candidates.append((score, "-", candidate))
if self.direction == "both" or self.direction == "forward":
for candidate in add:
tobetested = selected | set([candidate])
Xtbt = X[:, list(tobetested)]
if self.verbose > 2:
print("----------------------------------------------")
print(f"var: {tobetested} == (n, p): {Xtbt.shape}")
reglin = LinearRegression(fit_intercept=False).fit(Xtbt, y)
if self.crit == "aic" or self.crit == "AIC":
score = aic(reglin, Xtbt, y)
elif self.crit == "bic" or self.crit == "BIC":
score = bic(reglin, Xtbt, y)
if self.verbose > 2:
print((score, "+", candidate))
scores_with_candidates.append((score, "+", candidate))
scores_with_candidates.sort()
best_new_score, dircur, best_candidate = scores_with_candidates.pop(0)
if current_score > best_new_score:
if dircur == "+":
add = add - set([best_candidate])
selected = selected | set([best_candidate])
if self.direction == "both":
remove = remove | set([best_candidate])
else:
remove = remove - set([best_candidate])
selected = selected - set([best_candidate])
if self.direction == "both":
add = add | set([best_candidate])
current_score = best_new_score
if self.verbose > 1:
print("----------------------------------------------")
print(f"Crit: {current_score:.3e}, New Current: {selected}")
else:
break
ll = list(selected)
if len(ll) > 1:
ll.sort()
ll.pop()
if self.verbose > 1:
print("----------------------------------------------")
if (self.fit_intercept):
print(f"Crit: {current_score:.3e}, Final (intercept must be added): {ll}")
else:
print(f"Crit: {current_score:.3e}, Final : {ll}")
elif self.verbose == 1:
print("Final:", ll)
return ll
else:
if ll[0] == p:
if self.verbose > 1:
print("----------------------------------------------")
print(f"Crit: {current_score:.3e}, Final (intercept must be added): {ll}")
elif self.verbose == 1:
print(f"Final (intercept): {ll}")
return []
else:
print(ll)
raise ValueError("only one variable but no intercept")
return None
def predict(self, X):
"""
Predict using the linear model.
Parameters
----------
X : array-like or sparse matrix, shape (n_samples, n_features)
Samples.
Returns
-------
C : array, shape (n_samples,)
Returns predicted values.
"""
if isinstance(X, pd.DataFrame):
XX = X.iloc[:, self.selected_features_]
else:
XX = X[:, self.selected_features_]
return super().predict(XX)
def bic(modele, X, y):
"""BIC for scikitlearn regression model.
Parameters:
----------
modele (scikitlearn linear_model.LinearRegression): linear regression model
X (numpy array or pandas array): explanatory variables
y (numpy array): response variable
X (numpy array): variables explicatives
y (numpy array): variable a expliquer
Returns:
--------
BIC: BIC criterion smaller is better
"""
n = X.shape[0]
yhat = modele.predict(X)
xi = X.shape[1]
e2 = np.square(y - yhat)
bic = (n * (1 + np.log(2 * np.pi)) + n * np.log(e2.sum()/n)
+ (xi + 2) * np.log(n))
return bic
def aic(modele, X, y):
"""AIC for scikitlearn regression model.
Parameters:
----------
modele (scikitlearn linear_model.LinearRegression): linear regression model
X (numpy array or pandas array): explanatory variables
y (numpy array): response variable
Returns:
--------
AIC: AIC criterion smaller is better
"""
n = X.shape[0]
yhat = modele.predict(X)
xi = X.shape[1]
e2 = np.square(y - yhat)
aic = n * (1 + np.log(2 * np.pi)) + n * np.log(e2.sum()/n) + (xi + 2) * 2
return aic
def test_index(p, indexes):
"""Test if indexes are between 0 and p-1.
Parameters:
----------
p (integer): number of columns of X
indexes (list): indexes of variables to be chosen in X
Returns:
--------
boolean: True if OK
"""
mini = p
maxi = -1
for i in indexes:
mini = min(mini, i)
maxi = max(maxi, i)
if maxi >= p or maxi < 0:
return False
if mini >= p or mini < 0:
return False
return True
if __name__ == "__main__":
print("glm_step.py is being run directly ??")