In [82]:
from sklearn import datasets, neighbors, linear_model
digits = datasets.load_digits() # Retrieves digits dataset from scikit-learn
In [29]:
print(digits['DESCR'])
In [39]:
digits['images'][0]
Out[39]:
In [75]:
import matplotlib.pyplot as plt
plt.gray()
plt.matshow(digits.images[0])
plt.matshow(digits.images[10])
plt.show()
In [69]:
for i in range(0,10):
plt.matshow(digits.images[i])
plt.show()
In [48]:
X_digits = digits.data
X_digits
Out[48]:
In [49]:
y_digits = digits.target
y_digits
Out[49]:
In [54]:
n_samples = len(X_digits)
n_samples
Out[54]:
In [46]:
X_train = X_digits[:int(.9 * n_samples)]
X_train
Out[46]:
In [47]:
y_train = y_digits[:int(.9 * n_samples)]
y_train
Out[47]:
In [76]:
X_test = X_digits[int(.9 * n_samples):]
y_test = y_digits[int(.9 * n_samples):]
X_test
Out[76]:
In [62]:
knn = neighbors.KNeighborsClassifier() # Retrieve the default K-Neighbours Classification algorithm
knn
Out[62]:
In [63]:
fitting = knn.fit(X_train, y_train) # Train the algorithm on 90% of the samples
fitting
Out[63]:
In [64]:
knn_score = fitting.score(X_test, y_test) # Score the algorithm on how well it fits the 10% of the data that was left out
print('KNN score: %f' % knn_score)
In [78]:
logistic = linear_model.LogisticRegression()
logistic
Out[78]:
In [77]:
log_regression_fitting = logistic.fit(X_train, y_train)
log_regression_fitting
Out[77]:
In [71]:
log_regression_score = log_regression_fitting.score(X_test, y_test)
print('LogisticRegression score: %f' % log_regression_score)
In [74]:
print('KNN score: %f' % knn_score)
print('LGR score: %f' % log_regression_score)
In [ ]:
In [ ]:
In [ ]: