Osman Rafi
Osman Rafi

Reputation: 1046

Plot feature importance in RandomForestRegressor sklearn

I Am new in Data Science. I am trying to find out the feature importance ranking for my dataset. I already applied Random forest and got the output.

Here is my code:

# importing libraries

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

# importing dataset

dataset=pd.read_csv('Churn_Modelling.csv')
X = dataset.iloc[:,3:12].values
Y = dataset.iloc[:,13].values

#encoding catagorical data

from sklearn.preprocessing import LabelEncoder, OneHotEncoder

#country
labelencoder_X_1= LabelEncoder()
X[:,1]=labelencoder_X_1.fit_transform(X[:,1])

#gender
labelencoder_X_2= LabelEncoder()
X[:,2]=labelencoder_X_2.fit_transform(X[:,2])

onehotencoder = OneHotEncoder(categorical_features=[0])
X = onehotencoder.fit_transform(X).toarray()


#spliting dataset into test set and train set

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.20)

from sklearn.ensemble import RandomForestRegressor

regressor = RandomForestRegressor(n_estimators=20, random_state=0)  
regressor.fit(X_train, y_train) 

In the importance part i almost copied the example shown in : https://scikit-learn.org/stable/auto_examples/ensemble/plot_forest_importances.html

Here is the code:

#feature importance
from sklearn.ensemble import ExtraTreesClassifier

importances = regressor.feature_importances_
std = np.std([tree.feature_importances_ for tree in regressor.estimators_],
             axis=0)
indices = np.argsort(importances)[::-1]
print("Feature ranking:")

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

# Plot the feature importances of the forest
plt.figure()
plt.title("Feature importances")
plt.bar(range(X.shape[1]), importances[indices],
       color="r", yerr=std[indices], align="center")
plt.xticks(range(X.shape[1]), indices)
plt.xlim([-1, X.shape[1]])
plt.show()

I am expecting the output shown in the documentation. Can Anyone Help me please ? Thanks in Advance.

My dataset is here: enter image description here

Upvotes: 1

Views: 4940

Answers (1)

seralouk
seralouk

Reputation: 33197

You have a lot of features and cannot been seen in a single plot. Just plot some of them.

Here I plot the first 20 most important:

# Plot the feature importances of the forest
plt.figure(figsize=(18,9))
plt.title("Feature importances")
n=20
_ = plt.bar(range(n), importances[indices][:n], color="r", yerr=std[indices][:n])
plt.xticks(range(n), indices)
plt.xlim([-1, n])
plt.show()

My code in case you need it: https://filebin.net/be4h27swglqf3ci3

Output:

enter image description here

Upvotes: 3

Related Questions