<jupyter_start><jupyter_text>Digits Classification **KNN (K — Nearest Neighbors)** is one of many (supervised learning) algorithms used in data mining and machine learning, it’s a classifier algorithm where the learning is based “how similar” is a data (a vector) from other **How it’s working?**The KNN is pretty simple, imagine that you have a data about colored balls:Purple balls;Yellow balls;And a ball that you don’t know if it’s purple or yellow, but you has all the data about this color(except the color label).So, how are you going to know the ball’s color? imagine you like a machine that you only have the ball’s characteristics(data), but doesn’t the final label. Hou do you will to know the ball’s color(final label/your class)?**Obs:** Let’s suppose that data with number 1(and label R) are referring to the purple balls and the data with number 2 (and label A) are referring to the yellow balls, this’s just to make the explanation easier, in future articles we will work with real data.Each line refers to a ball and each column refers to a ball’s characteristic, in the last column we have the class (color) of each of the balls: The KNN’s steps are:1. Receive an unclassified data;2. Measure the distance (Euclidian, Manhattan, Minkowski or Weighted) from the new data to all others data that is already classified;3. Gets the K(K is a parameter that you difine) smaller distances;4. Check the list of classes had the shortest distance and count the amount of each class that appears;5. Takes as correct class the class that appeared the most times;6. Classifies the new data with the class that you took in step 5;![](https://lh3.googleusercontent.com/-ADmh7vPIWxA/XTrM4rNblAI/AAAAAAAAgH8/CRNZB7ycYoQFy8O8mvHQqOlXqBJbaaGUACK8BGAs/s0/2019-07-26.png)<jupyter_code>from sklearn import datasets, neighbors, linear_model
import matplotlib.pyplot as plt
digits = datasets.load_digits()
print(digits.DESCR)
digits.images[0]
plt.imshow(digits.images[0])
X_digits = digits.data / digits.data.max()
len(X_digits)
X_digits[0]
y_digits = digits.target
y_digits
n_samples = len(X_digits)
print(int(.9 * n_samples))
X_train = X_digits[:int(.9 * n_samples)]
X_train[1]
y_train = y_digits[:int(.9 * n_samples)]
X_test = X_digits[int(.9 * n_samples):]
y_test = y_digits[int(.9 * n_samples):]
knn = neighbors.KNeighborsClassifier()
logistic = linear_model.LogisticRegression(solver='lbfgs', max_iter=1000,
                                           multi_class='multinomial')
print('KNN score: %f' % knn.fit(X_train, y_train).score(X_test, y_test))
print('LogisticRegression score: %f'% logistic.fit(X_train, y_train).score(X_test, y_test))
knn.classes_
knn.predict([[0.    , 0.    , 0.    , 0.75  , 0.8125, 0.3125, 0.    , 0.    ,
       0.    , 0.    , 0.    , 0.6875, 1.    , 0.5625, 0.    , 0.    ,
       0.    , 0.    , 0.1875, 0.9375, 1.    , 0.375 , 0.    , 0.    ,
       0.    , 0.4375, 0.9375, 1.    , 1.    , 0.125 , 0.    , 0.    ,
       0.    , 0.    , 0.0625, 1.    , 1.    , 0.1875, 0.    , 0.    ,
       0.    , 0.    , 0.0625, 1.    , 1.    , 0.375 , 0.    , 0.    ,
       0.    , 0.    , 0.0625, 1.    , 1.    , 0.375 , 0.    , 0.    ,
       0.    , 0.    , 0.    , 0.6875, 1.    , 0.625 , 0.    , 0.   ]])
knn.predict_proba([[0.    , 0.    , 0.    , 0.75  , 0.8125, 0.3125, 0.    , 0.    ,
       0.    , 0.    , 0.    , 0.6875, 1.    , 0.5625, 0.    , 0.    ,
       0.    , 0.    , 0.1875, 0.9375, 1.    , 0.375 , 0.    , 0.    ,
       0.    , 0.4375, 0.9375, 1.    , 1.    , 0.125 , 0.    , 0.    ,
       0.    , 0.    , 0.0625, 1.    , 1.    , 0.1875, 0.    , 0.    ,
       0.    , 0.    , 0.0625, 1.    , 1.    , 0.375 , 0.    , 0.    ,
       0.    , 0.    , 0.0625, 1.    , 1.    , 0.375 , 0.    , 0.    ,
       0.    , 0.    , 0.    , 0.6875, 1.    , 0.625 , 0.    , 0.   ]])
logistic.classes_
New_predicate = knn.predict(X_test)
New_predicate[0:5]
knn.score(X_train,y_train)*100
knn.score(X_test,New_predicate)*100<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13130805925182024, 'fraction_numerical': 0.0785839819231735, 'mean_word_length': 2.3906382978723406, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '257953', 'license': 'MIT', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1486, 'n_words': 463, 'path': 'Classification/k-nearest neighbors/KNeighborsClassifier-DIGITS DATA.ipynb', 'repo_name': 'vivekrathi14/Machine-Learning-with-Scikit-Learn-Python-3.x', 'size': '3983'}