KNN Classification 1
KNN Classification 1
KNN Classification 1
Table of Contents
KNN Classification ................................................................................................................................... 2
KNN Classification Algorithm .............................................................................................................. 4
Output of Nearest Neighbours Classification Model .......................................................................... 6
Let’s consider the task of classifying a green circle between class 1 and class 2.
Consider the case of KNN based on 1-nearest neighbour. It is clear that in this case, KNN
will classify the green circle in class 1. Now let’s increase the number of nearest
neighbours to 3 i.e., 3-nearest neighbour. As you can see in the figure there is ‘two’ class
2 objects and ‘one’ class 1 object inside the circle. KNN will classify a green circle in class
2 object as it forms the majority.
import numpy as np
n_neighbors = 15
iris = datasets.load_iris()
y = iris.target
clf.fit(X, y)
# Plot the decision boundary. For that, we will assign a color to each
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.figure()
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
% (n_neighbors, weights))
plt.show()