Random Forest Classifier Demo¶
In [2]:
from sklearn.datasets import load_digits
In [3]:
digits_data = load_digits()
X = digits_data['data']
Y = digits_data['target']
In [4]:
X.shape
Out[4]:
In [5]:
Y
Y.shape
Out[5]:
In [6]:
import pylab as pl
pl.gray()
pl.matshow(digits_data.images[0])
pl.show()
In [7]:
X[0]
Out[7]:
In [8]:
Y[0]
Out[8]:
In [9]:
pl.matshow(digits_data.images[1])
pl.show()
In [10]:
X[1]
Out[10]:
In [11]:
Y[1]
Out[11]:
From the corpus let us create Train and Test Dataset
In [12]:
from sklearn.cross_validation import train_test_split
In [13]:
x_train,x_test,y_train,y_test = train_test_split(X,Y,test_size=0.2,random_state=42)
In [14]:
x_train.shape
Out[14]:
In [15]:
x_test.shape
Out[15]:
In [24]:
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
clf = RandomForestClassifier(n_estimators=1,criterion="entropy")
clf.fit(x_train,y_train)
predictions = clf.predict(x_train)
In [25]:
predictions
Out[25]:
In [26]:
print "Train Accuracy = %f "%(accuracy_score(y_train,predictions)*100)
predictions_test = clf.predict(x_test)
print "Test Accuracy = %f "%(accuracy_score(y_test,predictions_test)*100)
In [22]:
In []:
No comments:
Post a Comment