1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| import matplotlib.pyplot as plt import numpy as np from matplotlib.colors import ListedColormap from sklearn.datasets import load_iris
iris = load_iris() X = iris.data[:, (1, 3)] y = iris.target
cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF']) cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF'])
x_min, x_max = X[:, 0].min() - 0.1, X[:, 0].max() + 0.1 y_min, y_max = X[:, 1].min() - 0.1, X[:, 1].max() + 0.1 xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02), np.arange(y_min, y_max, 0.02))
def knn_code(loc, k=5, order=2 ): diff_loc = X - loc dis_loc = np.linalg.norm(diff_loc, ord=order, axis=1) knn = y[dis_loc.argsort()[:k]] counts = np.bincount(knn) return np.argmax(counts)
line_loc = np.array(list(zip(xx.ravel(), yy.ravel())))
plt.figure(figsize=(15, 12))
pos = 1
for k in [2, 6]: for order in [1, 2]: Z = np.array([knn_code(ii, k, order) for ii in line_loc]).reshape(xx.shape) ax = plt.subplot(220 + pos) ax.pcolormesh(xx, yy, Z, cmap=cmap_light, shading='auto') ax.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold) ax.set_title(f'k: {k}, distance order: {order}') pos += 1
plt.suptitle('I am a tuner!') plt.show()
|