0

I was trying to calculate the Permutation Importance by using Eli5, however the command, eli5.show_weights does not fetch any image or anything. (tried in python 2.7, 3.7) can you please help. The code is as below:

Code Snippet

feature_names = [c for c in train_df.columns if train_df[c].dtype in [np.int64]]

X = train_df[feature_names]

X = X.drop('state', axis = 1)

y = train_df['state']

X_train,X_test,y_train,y_test = train_test_split(X,y,random_state = 1)

my_model = RandomForestClassifier(n_estimators = 100,random_state = 1).fit(X_train,y_train)

perm = PermutationImportance(my_model, random_state = 1).fit(X_test,y_test)

**eli5.show_weights(perm, show_feature_values = X_test.columns.tolist())**#This prints nothing

Community
  • 1
  • 1

1 Answers1

0

Sci-kit learn added permutation importance recently.

This is an alternative.

In regards to your code. I think the reason you are not seeing anything is because X_test may not have columns as it is a numpy array. Try printing out that separately to see.

Another option is accessing the permutation importances as attributes of your fitted model.

You can try that at the end of your code try:

##fit the permutation
perm = PermutationImportance(my_model, random_state = 1).fit(X_test,y_test)

##view importances
print(perm.feature_importances_)

## view with xvalues
for feat, imp in zip(X.columns,perm.feature_importances_):
    print("Feature: {}, Importance: {}".format(feat,imp))

jawsem
  • 751
  • 5
  • 8