-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_test.py
343 lines (309 loc) · 13.9 KB
/
_test.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
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
from dataclasses import MISSING, dataclass, field
from typing import Dict, List, NamedTuple, Optional, Union
from numpy.lib.arraysetops import isin
import pandas as pd
try:
from darts.metrics import mase, mse, mae
except ImportError:
print("darts not installed. Will not be able to use dart metrics or calculate_metrics")
# from src.utils.ts_utils import forecast_bias, darts_metrics_adapter
from src.utils.general import intersect_list , union_list, difference_list
from sklearn.base import BaseEstimator, clone
from sklearn.preprocessing import StandardScaler
import numpy as np
import warnings
# from category_encoders import OneHotEncoder
@dataclass
class MissingValueConfig:
bfill_columns: List = field(
default_factory=list,
metadata={"help": "Column names which should be filled using strategy=`bfill`"},
)
ffill_columns: List = field(
default_factory=list,
metadata={"help": "Column names which should be filled using strategy=`ffill`"},
)
zero_fill_columns: List = field(
default_factory=list,
metadata={"help": "Column names which should be filled using 0"},
)
def impute_missing_values(self, df: pd.DataFrame):
df = df.copy()
bfill_columns = intersect_list(df.columns, self.bfill_columns)
df[bfill_columns] = df[bfill_columns].fillna(method="bfill")
ffill_columns = intersect_list(df.columns, self.ffill_columns)
df[ffill_columns] = df[ffill_columns].fillna(method="ffill")
zero_fill_columns = intersect_list(df.columns, self.zero_fill_columns)
df[zero_fill_columns] = df[zero_fill_columns].fillna(0)
# Filling with mean as default fillna strategy
return df.fillna(df.mean())
@dataclass
class FeatureConfig:
date: List = field(
default=MISSING,
metadata={"help": "Column name of the date column"},
)
target: str = field(
default=MISSING,
metadata={"help": "Column name of the target column"},
)
original_target: str = field(
default=None,
metadata={
"help": "Column name of the original target column in acse of transformed target. If None, it will be assigned same value as target"
},
)
continuous_features: List[str] = field(
default_factory=list,
metadata={"help": "Column names of the numeric fields. Defaults to []"},
)
categorical_features: List[str] = field(
default_factory=list,
metadata={"help": "Column names of the categorical fields. Defaults to []"},
)
boolean_features: List[str] = field(
default_factory=list,
metadata={"help": "Column names of the boolean fields. Defaults to []"},
)
index_cols: str = field(
default_factory=list,
metadata={
"help": "Column names which needs to be set as index in the X and Y dataframes."
},
)
exogenous_features: List[str] = field(
default_factory=list,
metadata={
"help": "Column names of the exogenous features. Must be a subset of categorical and continuous features"
},
)
feature_list: List[str] = field(init=False)
def __post_init__(self):
assert (
len(self.categorical_features) + len(self.continuous_features) > 0
), "There should be at-least one feature defined in categorical or continuous columns"
self.feature_list = (
self.categorical_features + self.continuous_features + self.boolean_features
)
assert (
self.target not in self.feature_list
), f"`target`({self.target}) should not be present in either categorical, continuous or boolean feature list"
assert (
self.date not in self.feature_list
), f"`date`({self.target}) should not be present in either categorical, continuous or boolean feature list"
extra_exog = set(self.exogenous_features) - set(self.feature_list)
assert (
len(extra_exog) == 0
), f"These exogenous features are not present in feature list: {extra_exog}"
if self.original_target is None:
self.original_target = self.target
def get_X_y(
self, df: pd.DataFrame, categorical: bool = False, exogenous: bool = False
):
feature_list = self.continuous_features
if categorical:
feature_list += self.categorical_features + self.boolean_features
if not exogenous:
feature_list = list(set(feature_list) - set(self.exogenous_features))
(X, y, y_orig) = (
df.loc[:, feature_list + self.index_cols].set_index(self.index_cols),
df.loc[:, [self.target] + self.index_cols].set_index(self.index_cols) if self.target in df.columns else None,
df.loc[:, [self.original_target] + self.index_cols].set_index(
self.index_cols
) if self.original_target in df.columns else None,
)
return X, y, y_orig
@dataclass
class ModelConfig:
model: BaseEstimator = field(
default=MISSING, metadata={"help": "Sci-kit Learn Compatible model instance"}
)
name: str = field(
default=None,
metadata={
"help": "Name or identifier for the model. If left None, will use the string representation of the model"
},
)
normalize: bool = field(
default=False,
metadata={"help": "Flag whether to normalize the input or not"},
)
fill_missing: bool = field(
default=True,
metadata={"help": "Flag whether to fill missing values before fitting"},
)
encode_categorical: bool = field(
default=False,
metadata={"help": "Flag whether to encode categorical values before fitting"},
)
categorical_encoder: BaseEstimator = field(
default=None,
metadata={"help": "Categorical Encoder to be used"},
)
def __post_init__(self):
assert (
not(self.encode_categorical and self.categorical_encoder is None)
), "`categorical_encoder` cannot be None if `encode_categorical` is True"
def clone(self):
self.model = clone(self.model)
return self
class MLForecast:
def __init__(
self,
model_config: ModelConfig,
feature_config: FeatureConfig,
missing_config: MissingValueConfig = None,
target_transformer: object = None,
) -> None:
"""Convenient wrapper around scikit-learn style estimators
Args:
model_config (ModelConfig): Instance of the ModelConfig object defining the model
feature_config (FeatureConfig): Instance of the FeatureConfig object defining the features
missing_config (MissingValueConfig, optional): Instance of the MissingValueConfig object
defining how to fill missing values. Defaults to None.
target_transformer (object, optional): Instance of target transformers from src.transforms.
Should support `fit`, `transform`, and `inverse_transform`. It should also
return `pd.Series` with datetime index to work without an error. Defaults to None.
"""
self.model_config = model_config
self.feature_config = feature_config
self.missing_config = missing_config
self.target_transformer = target_transformer
self._model = clone(model_config.model)
if self.model_config.normalize:
self._scaler = StandardScaler()
if self.model_config.encode_categorical:
self._cat_encoder = self.model_config.categorical_encoder
self._encoded_categorical_features = (
self.feature_config.categorical_features
)
def fit(
self,
X: pd.DataFrame,
y: Union[pd.Series, np.ndarray],
is_transformed: bool=False,
fit_kwargs: Dict={},
):
"""Handles standardization, missing value handling, and training the model
Args:
X (pd.DataFrame): The dataframe with the features as columns
y (Union[pd.Series, np.ndarray]): Dataframe, Series, or np.ndarray with the targets
is_transformed (bool, optional): Whether the target is already transformed.
If `True`, fit wont be transforming the target using the target_transformer
if provided. Defaults to False.
fit_kwargs (Dict, optional): The dictionary with keyword args to be passed to the
fit funciton of the model. Defaults to {}.
"""
missing_feats = difference_list(X.columns, self.feature_config.feature_list)
if len(missing_feats)>0:
warnings.warn(f"Some features in defined in FeatureConfig is not present in the dataframe. Ignoring these features: {missing_feats}")
self._continuous_feats = intersect_list(self.feature_config.continuous_features, X.columns)
self._categorical_feats = intersect_list(self.feature_config.categorical_features, X.columns)
self._boolean_feats = intersect_list(self.feature_config.boolean_features, X.columns)
if self.model_config.fill_missing:
X = self.missing_config.impute_missing_values(X)
if self.model_config.encode_categorical:
missing_cat_cols = difference_list(intersect_list(X.columns.tolist(), self.feature_config.categorical_features), self.model_config.categorical_encoder.cols)
assert len(missing_cat_cols)==0, f"These categorical features are not handled by the categorical_encoder : {missing_cat_cols}"
X = self._cat_encoder.fit_transform(X, y)
self._encoded_categorical_features = (
self.model_config.categorical_encoder.get_feature_names()
)
else:
self._encoded_categorical_features = []
if self.model_config.normalize:
X[
self._continuous_feats
+ self._encoded_categorical_features
] = self._scaler.fit_transform(
X[
self._continuous_feats
+ self._encoded_categorical_features
]
)
self._train_features = X.columns.tolist()
if not is_transformed and self.target_transformer is not None:
y = self.target_transformer.fit_transform(y)
self._model.fit(X, y, **fit_kwargs)
return self
def predict(self, X: pd.DataFrame) -> pd.Series:
"""Predicts on the given dataframe using the trained model
Args:
X (pd.DataFrame): The dataframe with the features as columns. The index is passed on to the prediction series
Returns:
pd.Series: predictions using the model as a pandas Series with datetime index
"""
assert len(intersect_list(self._train_features, X.columns))==len(self._train_features), f"All the features during training is not available while predicting: {difference_list(self._train_features, X.columns)}"
if self.model_config.fill_missing:
X = self.missing_config.impute_missing_values(X)
if self.model_config.encode_categorical:
X = self._cat_encoder.transform(X)
if self.model_config.normalize:
X[
self._continuous_feats
+ self._encoded_categorical_features
] = self._scaler.transform(
X[
self._continuous_feats
+ self._encoded_categorical_features
]
)
y_pred = pd.Series(
self._model.predict(X).ravel(),
index=X.index,
name=f"{self.model_config.name}",
)
if self.target_transformer is not None:
y_pred = self.target_transformer.inverse_transform(y_pred)
y_pred.name = f"{self.model_config.name}"
return y_pred
def feature_importance(self) -> pd.DataFrame:
"""Generates the feature importance dataframe, if available. For linear
models the coefficients are used and tree based models use the inbuilt
feature importance. For the rest of the models, it returns an empty dataframe.
Returns:
pd.DataFrame: Feature Importance dataframe, sorted in descending order of its importances.
"""
if hasattr(self._model, "coef_") or hasattr(
self._model, "feature_importances_"
):
feat_df = pd.DataFrame(
{
"feature": self._train_features,
"importance": self._model.coef_.ravel()
if hasattr(self._model, "coef_")
else self._model.feature_importances_.ravel(),
}
)
feat_df["_abs_imp"] = np.abs(feat_df.importance)
feat_df = feat_df.sort_values("_abs_imp", ascending=False).drop(
columns="_abs_imp"
)
else:
feat_df = pd.DataFrame()
return feat_df
# def calculate_metrics(
# y: pd.Series, y_pred: pd.Series, name: str, y_train: pd.Series = None
# ):
# """Method to calculate the metrics given the actual and predicted series
# Args:
# y (pd.Series): Actual target with datetime index
# y_pred (pd.Series): Predictions with datetime index
# name (str): Name or identification for the model
# y_train (pd.Series, optional): Actual train target to calculate MASE with datetime index. Defaults to None.
# Returns:
# Dict: Dictionary with MAE, MSE, MASE, and Forecast Bias
# """
# return {
# "Algorithm": name,
# "MAE": darts_metrics_adapter(mae, actual_series=y, pred_series=y_pred),
# "MSE": darts_metrics_adapter(mse, actual_series=y, pred_series=y_pred),
# "MASE": darts_metrics_adapter(
# mase, actual_series=y, pred_series=y_pred, insample=y_train
# )
# if y_train is not None
# else None,
# "Forecast Bias": darts_metrics_adapter(
# forecast_bias, actual_series=y, pred_series=y_pred
# ),
# }