K-Nearest Neighbours: Effect of K#
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets, neighbors
from matplotlib.colors import ListedColormap
def knn_comparison(data, n_neighbors = 15):
'''
This function finds k-NN and plots the data.
'''
X = data[:, :2]
y = data[:,2]
# grid cell size
h = .02
cmap_light = ListedColormap(['#FFAAAA', '#AAAAFF'])
cmap_bold = ListedColormap(['#FF0000', '#0000FF'])
# the core classifier: k-NN
clf = neighbors.KNeighborsClassifier(n_neighbors)
clf.fit(X, y)
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
# we create a mesh grid (x_min,y_min) to (x_max y_max) with 0.02 grid spaces
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
# we predict the value (either 0 or 1) of each element in the grid
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
# xx.ravel() will give a flatten array
# np.c_ : Translates slice objects to concatenation along the second axis.
# > np.c_[np.array([1,2,3]), np.array([4,5,6])]
# > array([[1, 4],
# [2, 5],
# [3, 6]]) (source: np.c_ documentation)
# convert the out back to the xx shape (we need it to plot the decission boundry)
Z = Z.reshape(xx.shape)
# pcolormesh will plot the (xx,yy) grid with colors according to the values of Z
# it looks like decision boundry
plt.figure()
plt.pcolormesh(xx, yy, Z, cmap=cmap_light)
# scatter plot of with given points
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold)
#defining scale on both axises
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
# set the title
plt.title('K value = '+str(n_neighbors))
plt.show()
Meshgrid explanation#
please check this link stackoverflow meshgrid explanation
data = np.genfromtxt('data/6.overlap.csv', delimiter=',')
knn_comparison(data, 1)
knn_comparison(data, 5)
knn_comparison(data,15)
knn_comparison(data, 30)
knn_comparison(data, 50)
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
Cell In[3], line 1
----> 1 data = np.genfromtxt('data/6.overlap.csv', delimiter=',')
2 knn_comparison(data, 1)
3 knn_comparison(data, 5)
4 knn_comparison(data,15)
File ~/work/omniverse/omniverse/.venv/lib/python3.14/site-packages/numpy/lib/_npyio_impl.py:1982, in genfromtxt(fname, dtype, comments, delimiter, skip_header, skip_footer, converters, missing_values, filling_values, usecols, names, excludelist, deletechars, replace_space, autostrip, case_sensitive, defaultfmt, unpack, usemask, loose, invalid_raise, max_rows, encoding, ndmin, like)
1980 fname = os.fspath(fname)
1981 if isinstance(fname, str):
-> 1982 fid = np.lib._datasource.open(fname, 'rt', encoding=encoding)
1983 fid_ctx = contextlib.closing(fid)
1984 else:
File ~/work/omniverse/omniverse/.venv/lib/python3.14/site-packages/numpy/lib/_datasource.py:192, in open(path, mode, destpath, encoding, newline)
155 """
156 Open `path` with `mode` and return the file object.
157
(...) 188
189 """
191 ds = DataSource(destpath)
--> 192 return ds.open(path, mode, encoding=encoding, newline=newline)
File ~/work/omniverse/omniverse/.venv/lib/python3.14/site-packages/numpy/lib/_datasource.py:529, in DataSource.open(self, path, mode, encoding, newline)
526 return _file_openers[ext](found, mode=mode,
527 encoding=encoding, newline=newline)
528 else:
--> 529 raise FileNotFoundError(f"{path} not found.")
FileNotFoundError: data/6.overlap.csv not found.
data = np.genfromtxt('data/1.ushape.csv', delimiter=',')
knn_comparison(data, 1)
knn_comparison(data, 5)
knn_comparison(data,15)
knn_comparison(data,30)
data = np.genfromtxt('data/2.concerticcir1.csv', delimiter=',')
knn_comparison(data, 1)
knn_comparison(data, 5)
knn_comparison(data,15)
knn_comparison(data,30)
data = np.genfromtxt('data/3.concertriccir2.csv', delimiter=',')
knn_comparison(data, 1)
knn_comparison(data, 5)
knn_comparison(data, 15)
data = np.genfromtxt('data/4.linearsep.csv', delimiter=',')
knn_comparison(data, 1)
knn_comparison(data, 5)
knn_comparison(data)
data = np.genfromtxt('data/5.outlier.csv', delimiter=',')
knn_comparison(data,1)
knn_comparison(data,5)
knn_comparison(data)
data = np.genfromtxt('data/7.xor.csv', delimiter=',')
knn_comparison(data, 1)
knn_comparison(data, 5)
knn_comparison(data)
data = np.genfromtxt('data/8.twospirals.csv', delimiter=',')
knn_comparison(data, 1)
knn_comparison(data, 5)
knn_comparison(data)
data = np.genfromtxt('data/9.random.csv', delimiter=',')
knn_comparison(data, 1)
knn_comparison(data, 5)
knn_comparison(data)