-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
202 lines (153 loc) · 5.57 KB
/
model.py
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
"""
GLMNet module.
**Available routines:**
- class ``GLMNet``: Builds GLMnet model using cross validation.
Credits
-------
::
Authors:
- Diptesh
Date: Sep 06, 2021
"""
# pylint: disable=invalid-name
# pylint: disable=R0902,R0903,R0913,C0413,W0511
from typing import List, Dict
import warnings
import re
import sys
from inspect import getsourcefile
from os.path import abspath
import pandas as pd
import numpy as np
from sklearn.linear_model import ElasticNetCV
from sklearn.model_selection import train_test_split as split
path = abspath(getsourcefile(lambda: 0))
path = re.sub(r"(.+\/)(.+.py)", "\\1", path)
sys.path.insert(0, path)
import metrics # noqa: F841
# =============================================================================
# --- DO NOT CHANGE ANYTHING FROM HERE
# =============================================================================
def ignore_warnings(test_func): # pragma: no cover
"""Suppress warnings."""
def do_test(self, *args, **kwargs):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
test_func(self, *args, **kwargs)
return do_test
class GLMNet():
"""GLMNet module.
Objective:
- Build
`GLMNet <https://web.stanford.edu/~hastie/Papers/glmnet.pdf>`_
model using optimal alpha and lambda
Parameters
----------
df : pd.DataFrame
Pandas dataframe containing `y_var` and `x_var` variables.
y_var : str
Dependant variable.
x_var : List[str]
Independant variables.
strata : pd.DataFrame, optional
A pandas dataframe column defining the strata (the default is None).
param : Dict, optional
GLMNet parameters (the default is None).
In case of None, the parameters will default to::
seed: 1
a_inc: 0.05
test_perc: 0.25
n_jobs: -1
k_fold: 10
Returns
-------
opt : Dict
Summary of the model built along with best paramameters
and estimators.
model : object
Final optimal model.
model_summary : Dict
Model summary containing key metrics like R-squared, RMSE, MSE, MAE,
MAPE
Methods
-------
predict
Example
-------
>>> mod = GLMNet(df=df_ip, y_var=["y"], x_var=["x1", "x2", "x3"])
>>> df_op = mod.predict(df_predict)
"""
def __init__(self,
df: pd.DataFrame,
y_var: str,
x_var: List[str],
strata: str = None,
param: Dict = None):
"""Initialize variables for module ``GLMNet``."""
self.df = df[[y_var] + x_var]
self.y_var = y_var
self.x_var = x_var
self.strata = strata
self.model_summary = None
self.opt = None
if param is None:
param = {"seed": 1,
"a_inc": 0.05,
"test_perc": 0.25,
"n_jobs": -1,
"k_fold": 10}
self.param = param
self.param["l1_range"] = list(np.round(np.arange(self.param["a_inc"],
1.01,
self.param["a_inc"]),
2))
self._fit()
self._compute_metrics()
def _fit(self) -> None:
"""Fit the best GLMNet model."""
train_x, test_x, \
train_y, test_y = split(self.df[self.x_var],
self.df[[self.y_var]],
test_size=self.param["test_perc"],
random_state=self.param["seed"],
stratify=self.strata)
mod = ElasticNetCV(l1_ratio=self.param["l1_range"],
fit_intercept=True,
alphas=[1e-5, 1e-4, 1e-3, 1e-2, 1e-1,
1.0, 10.0, 100.0],
cv=self.param["k_fold"],
n_jobs=self.param["n_jobs"],
random_state=self.param["seed"])
mod.fit(train_x, train_y.values.ravel())
opt = {"alpha": mod.l1_ratio_,
"lambda": mod.alpha_,
"intercept": mod.intercept_,
"coef": mod.coef_,
"train_v": mod.score(train_x, train_y),
"test_v": mod.score(test_x, test_y)}
self.model = mod
self.opt = opt
def _compute_metrics(self):
"""Compute commonly used metrics to evaluate the model."""
y = self.df[[self.y_var]].iloc[:, 0].values.tolist()
y_hat = list(self.predict(self.df[self.x_var])["y"].values)
model_summary = {"rsq": np.round(metrics.rsq(y, y_hat), 3),
"mae": np.round(metrics.mae(y, y_hat), 3),
"mape": np.round(metrics.mape(y, y_hat), 3),
"rmse": np.round(metrics.rmse(y, y_hat), 3)}
model_summary["mse"] = np.round(model_summary["rmse"] ** 2, 3)
self.model_summary = model_summary
def predict(self, df_predict: pd.DataFrame) -> pd.DataFrame:
"""Predict y_var/target variable.
Parameters
----------
df_predict : pd.DataFrame
Pandas dataframe containing `x_var`.
Returns
-------
pd.DataFrame
Pandas dataframe containing predicted `y_var` and `x_var`.
"""
y_hat = self.model.predict(df_predict)
df_predict.insert(loc=0, column=self.y_var, value=y_hat)
return df_predict