This examples shows the use of forests of trees to evaluate the importance of features on an artificial classification task. The red bars are the feature importances of the forest, along with their inter-trees variability.
As expected, the plot suggests that 3 features are informative, while the remaining are not.
In [1]:
%matplotlib inline
print(__doc__)
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import make_classification
#from sklearn.ensemble import ExtraTreesClassifier
In [2]:
# import train data and randomize
ALL_data = pd.read_csv("iris.csv")
ALL_data = ALL_data.sample(frac=1)
In [3]:
ALL_data['NameInt'] = pd.Categorical(ALL_data['Name']).codes
In [4]:
train=ALL_data.values[0:100,]
test=ALL_data.values[100:,]
test.shape
Out[4]:
In [5]:
ALL_data.head()
Out[5]:
In [6]:
# X, y = make_classification(n_samples=1000,
# n_features=10,
# n_informative=3,
# n_redundant=0,
# n_repeated=0,
# n_classes=2,
# random_state=0,
# shuffle=False)
X=train[:,0:4]
y=train[:,5]
# Build a forest and compute the feature importances
forest = RandomForestClassifier(n_estimators=2500,
random_state=0)
In [7]:
y=np.asarray(train[:,5], dtype="|S6")
forest.fit(X, y)
Out[7]:
In [8]:
importances = forest.feature_importances_
std = np.std([tree.feature_importances_ for tree in forest.estimators_],
axis=0)
indices = np.argsort(importances)[::-1]
# Print the feature ranking
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()
In [9]:
X_test=test[:,0:4]
y_test=np.asarray(test[:,5], dtype="|S6")
results = forest.predict(X_test)
In [23]:
print(y_test)
print(results)
true_pred_count=np.sum(y_test==results)
print("True predictions %d out of %d : Accuracy %.4f " % (true_pred_count,len(y_test),true_pred_count/len(y_test)*100))