TeamHG-Memex / eli5

A library for debugging/inspecting machine learning classifiers and explaining their predictions
http://eli5.readthedocs.io
MIT License
2.76k stars 334 forks source link

pandas.core.indexes.base.Index not supported for feature_names params in show_weights #408

Open TremaMiguel opened 3 years ago

TremaMiguel commented 3 years ago

If argument feature_names in function eli5.show_weights() is pandas.core.indexes.base.Index raises an TypeError: Unexpected feature_names type.

To reproduce

import eli5

from eli5.sklearn import PermutationImportance
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_boston

data = load_boston()
X, y, feats  = data['data'], data['target'], data['feature_names']
X = pd.DataFrame(X, columns=feats)

# Split in train-test
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

# Fit model
reg = RandomForestRegressor()
reg.fit(X_train, y_train)

# Run permutation importance
perm = PermutationImportance(reg, random_state=1).fit(X_test, y_test)

# Show weights with pandas.Index
eli5.show_weights(perm, top=None, feature_names=X_test.columns)
>>> TypeError: Unexpected feature_names type

Discussion

Reading the functionality of the FeatureNames class, noticed that some functionalities that feature_names param should satisfy are

cols = X_test.columns

# size
len(cols) 

# indexing
t[5]

# iterable
enumerate(cols)

so my question if it is possible to add support for pandas.core.indexes.base.Index as argument in show_weights function.