Importamos las librerías necesarias
import numpy as np
import sklearn
import sklearn.datasets as ds
import sklearn.model_selection as cv
import sklearn.neighbors as nb
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (12, 10)
cargamos los digitos
digits = ds.load_digits()
X = digits.data
y = digits.target
print((X.min(), X.max()))
print(X.shape)
(0.0, 16.0) (1797, 64)
http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_digits.html
nrows, ncols = 2, 5
plt.figure(figsize=(6,3));
plt.gray()
for i in range(ncols * nrows):
ax = plt.subplot(nrows, ncols, i + 1)
ax.matshow(digits.images[i,...])
plt.xticks([]); plt.yticks([]);
plt.title(digits.target[i]);
Separamos nuestra data de entrenamiento y nuestra data de prueba:
(X_train, X_test,
y_train, y_test) = cv.train_test_split(X, y, test_size=.25)
Usamos el clasificador de KNN
knc = nb.KNeighborsClassifier()
knc.fit(X_train, y_train);
Nos da un margen de acierto de un 98.2%
knc.score(X_test, y_test)
0.98666666666666669
# Dibujamos un 1
one = np.zeros((8, 8))
one[1:-1, 4] = 16 # The image values are in [0, 16].
one[2, 3] = 16
one
array([[ 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 16., 0., 0., 0.], [ 0., 0., 0., 16., 16., 0., 0., 0.], [ 0., 0., 0., 0., 16., 0., 0., 0.], [ 0., 0., 0., 0., 16., 0., 0., 0.], [ 0., 0., 0., 0., 16., 0., 0., 0.], [ 0., 0., 0., 0., 16., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0.]])
plt.figure(figsize=(2,2));
plt.imshow(one, interpolation='none');
plt.grid(False);
plt.xticks(); plt.yticks();
plt.title("One");
knc.predict(one.ravel())
/home/gerson/anaconda3/envs/py3/lib/python3.6/site-packages/sklearn/utils/validation.py:395: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample. DeprecationWarning)
array([1])
# Dibujamos un 7
seven = np.zeros((8, 8))
seven[1:-1, 4] = 16 # The image values are in [0, 16].
seven[1, 2:5] = 16
seven[4, 3:6] = 16
seven
array([[ 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 16., 16., 16., 0., 0., 0.], [ 0., 0., 0., 0., 16., 0., 0., 0.], [ 0., 0., 0., 0., 16., 0., 0., 0.], [ 0., 0., 0., 16., 16., 16., 0., 0.], [ 0., 0., 0., 0., 16., 0., 0., 0.], [ 0., 0., 0., 0., 16., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0.]])
plt.figure(figsize=(2,2));
plt.imshow(seven, interpolation='none');
plt.grid(False);
plt.xticks(); plt.yticks();
plt.title("Seven")
<matplotlib.text.Text at 0x7f6d11bd9710>
knc.predict(seven.ravel())
/home/gerson/anaconda3/envs/py3/lib/python3.6/site-packages/sklearn/utils/validation.py:395: DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if your data has a single feature or X.reshape(1, -1) if it contains a single sample. DeprecationWarning)
array([1])
# Esta celda da el estilo al notebook
from IPython.core.display import HTML
css_file = '../styles/StyleCursoPython.css'
HTML(open(css_file, "r").read())