Random Forest Classifier


In [1]:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_digits
from sklearn.preprocessing import scale
%matplotlib inline

In [2]:
digits = load_digits()

In [3]:
data = scale(digits.data)
data


Out[3]:
array([[ 0.        , -0.33501649, -0.04308102, ..., -1.14664746,
        -0.5056698 , -0.19600752],
       [ 0.        , -0.33501649, -1.09493684, ...,  0.54856067,
        -0.5056698 , -0.19600752],
       [ 0.        , -0.33501649, -1.09493684, ...,  1.56568555,
         1.6951369 , -0.19600752],
       ..., 
       [ 0.        , -0.33501649, -0.88456568, ..., -0.12952258,
        -0.5056698 , -0.19600752],
       [ 0.        , -0.33501649, -0.67419451, ...,  0.8876023 ,
        -0.5056698 , -0.19600752],
       [ 0.        , -0.33501649,  1.00877481, ...,  0.8876023 ,
        -0.26113572, -0.19600752]])

In [4]:
data.shape


Out[4]:
(1797, 64)

In [5]:
n_digits = len(np.unique(digits.target))
labels = digits.target

print(n_digits)
print(labels)


10
[0 1 2 ..., 8 9 8]

In [6]:
clf = RandomForestClassifier(n_estimators=10, 
                             max_depth=5,
                             criterion='entropy')

In [7]:
clf.fit(data, labels)


Out[7]:
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='entropy',
            max_depth=5, max_features='auto', max_leaf_nodes=None,
            min_impurity_decrease=0.0, min_impurity_split=None,
            min_samples_leaf=1, min_samples_split=2,
            min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1,
            oob_score=False, random_state=None, verbose=0,
            warm_start=False)

In [8]:
scores = clf.score(data, labels)
print(scores)


0.933778519755

In [9]:
importances = clf.feature_importances_
indexes = np.argsort(importances)
print(indexes)


[ 0 56 48 47 40 39 32 24 23 16 15 12 31  7  8 57 49 11  6 14 55 63 59 44  4
  1  3 62 27 17 25 52 35 51 41 60 45 37 22  2 18  9 50 58 29 46 19 42 34  5
 53 20 13 36 61 10 38 54 28 30 26 33 43 21]

In [10]:
ind = []
for index in indexes:
    ind.append(labels[index])

In [11]:
plt.figure(1)
plt.title('Importância dos Atributos')
plt.barh(range(len(indexes)), importances[indexes], color='b', align='center')
plt.yticks(range(len(indexes)), ind)
plt.xlabel('Importância Relativa')
plt.show()



In [ ]: