Regularised Linear model

Let’s try ElasticNet model. 



from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler, RobustScaler
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.metrics import make_scorer

from sklearn.linear_model import ElasticNet

steps = [
  ('scaler', RobustScaler()),
  ('enet', ElasticNet()),
]
reg_enet = Pipeline(steps=steps)
reg_enet.get_params()

param_grid = {
  'enet__alpha': [0.2, 0.4, 0.6, 0.8, 1.0],
  'enet__l1_ratio': [0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0],
  'enet__max_iter': [100000,]
}

np.random.seed(0)
metric = make_scorer(custom_loss, greater_is_better=False)
cv = [(slice(None), slice(None))]
gs_enet = GridSearchCV(reg_enet, param_grid=param_grid, scoring=metric, cv=cv, n_jobs=-1)

gs_enet.fit(x_train, y_train)
print(gs_enet.best_params_)

coef = gs_enet.best_estimator_.named_steps['enet'].coef_
importance = {}
for ii, name in enumerate(x_train.columns):
  if coef[ii] == 0.: continue
  importance[name] = coef[ii]

for k in sorted(importance, key=importance.get, reverse=True):
    print('{:10s}'.format(k), importance[k])
      Linear    ElasticNet
================================
MSE   1197.8    1167.3
R2    0.68183   0.68992
score inf       inf