kNN_vs_Random_Forest



In [3]:
from sklearn import datasets as dts
import numpy as np
from matplotlib import pyplot as plt

In [4]:
%matplotlib inline

In [6]:
data = dts.load_digits()
x = data['data']
y = data['target']

In [9]:
x.shape[0] * 0.75


Out[9]:
1347.75

In [10]:
x_train, x_test, y_train, y_test = x[:1348], x[1348:], y[:1348], y[1348:]

In [11]:
from sklearn.neighbors import KNeighborsClassifier
estimator = KNeighborsClassifier(n_neighbors=1, n_jobs=-1)
estimator.fit(x_train, y_train)
y_pred = estimator.predict(x_test)
print(np.sum(y_pred != y_test) / y_test.shape[0])


0.0378619153675

In [12]:
from sklearn.ensemble import RandomForestClassifier
estimator = RandomForestClassifier(n_estimators=1000)
estimator.fit(x_train, y_train)
y_pred = estimator.predict(x_test)
print(np.sum(y_pred != y_test) / y_test.shape[0])


0.0690423162584

In [ ]: