Skip to content

Instantly share code, notes, and snippets.

@smazzanti
Created April 8, 2020 21:56
Show Gist options
  • Save smazzanti/65402883456e830e75150a8124b698e2 to your computer and use it in GitHub Desktop.
Save smazzanti/65402883456e830e75150a8124b698e2 to your computer and use it in GitHub Desktop.
from sklearn.metrics import mean_absolute_error
from eli5.sklearn import PermutationImportance
mae = pd.DataFrame(columns = ['train', 'test'])
fi = pd.DataFrame(columns = feature_names)
for model_name in list(models.keys()):
# fit model
models[model_name].fit(X_trn, y_trn)
# compute mean absolute error of model in train and test set
mae.loc[model_name,:] = [mean_absolute_error(y_trn, models[model_name].predict(X_trn)), mean_absolute_error(y_tst, models[model_name].predict(X_tst))]
# compute feature importances of model
try:
feature_importances_ = models[model_name].feature_importances_
except:
feature_importances_ = PermutationImportance(models[model_name], cv = 'prefit', n_iter = 3).fit(X_trn, y_trn).feature_importances_
fi.loc[model_name, :] = feature_importances_ / feature_importances_.sum()
fi.fillna(0, inplace = True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment