Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computing Feature Importance with OneHotEncoded Features

Tags:

scikit-learn

Is it possible to compute feature importance (with Random Forest) in scikit learn when features have been onehotencoded?

like image 251
gbhrea Avatar asked Jul 14 '26 13:07

gbhrea


1 Answers

Here's an example of how to combine feature names with their importances:

from sklearn.feature_extraction import DictVectorizer
from sklearn.preprocessing import FunctionTransformer
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import make_pipeline


# some example data
X = pd.DataFrame({'feature': ['value1', 'value2', 'value2', 'value1', 'value2']})
y = [1, 0, 0, 1, 1]

# translate rows to dicts
def row_to_dict(X, y=None):
    return X.apply(dict, axis=1)

# define prediction model
ft = FunctionTransformer(row_to_dict, validate=False)
dv = DictVectorizer()
rf = RandomForestClassifier()

# glue steps together
model = make_pipeline(ft, dv, rf)

# train
model.fit(X, y)

# get feature importances
feature_importances = zip(dv.feature_names_, rf.feature_importances_)

# have a look
print feature_importances
like image 157
Kris Avatar answered Jul 16 '26 15:07

Kris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!