Skip to main content

Mapping column names to random forest feature importances

I am trying to plot feature importances for a random forest model and map each feature importance back to the original coefficient. I've managed to create a plot that shows the importances and uses the original variable names as labels but right now it's ordering the variable names in the order they were in the dataset (and not by order of importance). How do I order them in order of feature importance? Thanks!

enter image description here

My code is:

importances = brf.feature_importances_
std = np.std([tree.feature_importances_ for tree in brf.estimators_],
         axis=0)
indices = np.argsort(importances)[::-1]

# Print the feature ranking
print("Feature ranking:")

for f in range(x_dummies.shape[1]):
    print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]]))

# Plot the feature importances of the forest
plt.figure(figsize=(8,8))
plt.title("Feature importances")
plt.bar(range(x_train.shape[1]), importances[indices],
   color="r", yerr=std[indices], align="center")
feature_names = x_dummies.columns
plt.xticks(range(x_dummies.shape[1]), feature_names)
plt.xticks(rotation=90)
plt.xlim([-1, x_dummies.shape[1]])
plt.show()

Solved

A sort of generic solution would be to throw the features/importances into a dataframe and sort them before plotting:

import pandas as pd
%matplotlib inline
#do code to support model
#"data" is the X dataframe and model is the SKlearn object

feats = {} # a dict to hold feature_name: feature_importance
for feature, importance in zip(data.columns, model.feature_importances_):
    feats[feature] = importance #add the name/value pair 

importances = pd.DataFrame.from_dict(feats, orient='index').rename(columns={0: 'Gini-importance'})
importances.sort_values(by='Gini-importance').plot(kind='bar', rot=45)

I use a similar solution to Sam:

import pandas as pd
important_features = pd.Series(data=brf.feature_importances_,index=x_dummies.columns)
important_features.sort_values(ascending=False,inplace=True)

I always just print the list using print important_features but to plot you could always use Series.plot


Another simple way to get a sorted list

importances = list(zip(xgb_classifier.feature_importances_, df.columns))
importances.sort(reverse=True)

Next code adds a visualization if it's necessary

pd.DataFrame(importances, index=[x for (_,x) in importances]).plot(kind = 'bar')

Comments

Popular posts from this blog

OSError: [WinError 6] The handle is invalid, running subprocess.Popen on Python 3.6

I have the following code, I am trying to run XFoil (an airfoil analysis code, but that isn't important) from Python. import subprocess as sp ps = sp.Popen(r'C:\Users\me\XFoil\xfoil.exe',stdin=sp.PIPE,stderr=sp.PIPE,stdout=sp.PIPE) When I run, I get the following error: OSError: [WinError 6] The handle is invalid With the full list of errors as follows: File "~/XFoil_Own.py", line 12, in ps = sp.Popen(r'C:\Users\,e\XFoil\xfoil.exe' ,stdin=sp.PIPE,stderr=sp.PIPE,stdout=sp.PIPE) File "~\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 210, in __init__ super(SubprocessPopen, self).__init__(*args, **kwargs) File "~\Anaconda3\lib\subprocess.py", line 596, in __init__ _cleanup() File "~\Anaconda3\lib\subprocess.py", line 205, in _cleanup res = inst._internal_poll(_deadstate=sys.maxsize) File "~\Anaconda3\lib\subprocess.py", line 1035, in _internal_poll if _WaitForSingleObject(self._ha...

SQLCODE Error -122

I'm not used to SQL a lot, so maybe my question is so dumb, don't be hard with me guys ;) So, this is my table PEOPLE, and what i want is with a query is find the people that are not in an expecific age, but im getting an error. PEOPLE +-------+------+---------+ | NUMPOL| MCT | PRODNUM| +-------+------+---------+ | 98552 | 1054 | 9704 | +-------+------+---------+ | 89854 | 0985 | 5014 | +-------+------+---------+ | 78542 | 1054 | 9704 | +-------+------+---------+ | 98552 | 0965 | 9704 | +-------+------+---------+ | 98552 | 4222 | 9704 | +-------+------+---------+ I'm trying to run this query SELECT NUMPOL, MCT, PRODCO FROM PEOPLE WHERE MCT NOT IN (1054,0965) AND PRODNUM='9704' GROUP BY NUMPOL And this is the error that I'm getting, I tried to solve it googling but never could find an answer because I can't find why it told me that the select list is not valid: SQLCODE = -122, ERROR: COLUMN OR...