Python Project
#!/usr/bin/env python # coding: utf-8 # In[36]: from sklearn.datasets import load_digits # In[37]: digits = load_digits() # In[38]: print(digits.DESCR) # In[39]: digits.target[::100] # target values of every 100th sample # In[40]: digits.images[13] # show array for sample image at index 13 # In[41]: import matplotlib.pyplot as plt # In[42]: plt.imshow(digits.images[13]) # In[43]: figure, axes = plt.subplots(nrows=4, ncols=6, figsize=(6, 4)) for item in zip(axes.ravel(), digits.images, digits.target): axes, image, target = item axes.imshow(image, cmap=plt.cm.gray_r) axes.set_xticks([]) # remove x-axis tick marks axes.set_yticks([]) # remove y-axis tick marks axes.set_title(target) plt.tight_layout() # In[44]: from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split( digits.data, digits.target, random_state=11, test_size=0.20) # random_state for reproducibility # In[45]: X_train.shape # In[46]: from sklearn.neighbors import KNeighborsClassifier knn = KNeighborsClassifier() knn.fit(X=X_train, y=y_train) # In[47]: predicted = knn.predict(X=X_test) expected = y_test predicted[:20] expected[:20] # In[48]: predicted[:20] # In[49]: expected[:20] # In[50]: wrong = [(p, e) for (p, e) in zip(predicted, expected) if p != e] # In[51]: wrong # In[59]: print("Model accuracy:%.2f%%" %(knn.score(X_test,y_test)*100)) # In[ ]: # In[ ]: