Created
April 8, 2020 21:56
-
-
Save smazzanti/65402883456e830e75150a8124b698e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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