Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sklearn import neighbors
- from sklearn import datasets
- import numpy as np
- import matplotlib.pyplot as plt
- X = [ [2.0,1.5], [1.0,1.0], [1.0,3.0], [0.8,2.2], [3.0,1.0], [0.4,2.0], [1.2,1.2] ]
- Y = [ 0, 0, 1, 1, 0, 1, 1 ]
- X = np.array(X)
- Y = np.array(Y)
- clf = neighbors.KNeighborsClassifier(n_neighbors=1)
- clf = clf.fit(X, Y)
- #****************
- # Plot
- # baseado em https://scikit-learn.org/stable/auto_examples/neighbors/plot_classification.html
- #****************
- step_size = 0.01
- # encontrando os limites
- x_min, x_max = X[:, 0].min(), X[:, 0].max()
- y_min, y_max = X[:, 1].min(), X[:, 1].max()
- x_min, x_max = x_min-abs(0.1*x_min), x_max+abs(0.1*x_max)
- y_min, y_max = y_min-abs(0.1*y_min), y_max+abs(0.1*y_max)
- xx, yy = np.meshgrid(np.arange(x_min, x_max, step_size), np.arange(y_min, y_max, step_size))
- Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
- Z = Z.reshape(xx.shape)
- plt.figure()
- plt.pcolormesh(xx, yy, Z, cmap='Set3')
- # plot training points
- plt.scatter(X[:, 0], X[:, 1], c=Y, edgecolor='k', s=20, cmap='Set3')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement