Last active
December 11, 2017 06:26
-
-
Save henrych4/29dec59bb177fa931fd8b2dbb0bc9fc8 to your computer and use it in GitHub Desktop.
codes for assignment3 of ENGG5103
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Reference: http://scikit-learn.org/stable/auto_examples/neighbors/plot_classification.html#sphx-glr-auto-examples-neighbors-plot-classification-py | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from matplotlib.colors import ListedColormap | |
from sklearn import neighbors | |
# Set to 1 for 3a, set to 3 for 3b | |
n_neighbors = 3 | |
X = [[1, 1], [1, 2], [1, 0], [2, 0], [0.5, 0.5], [0, 0], [4, 0], [1, 3], [2, 2], [3, 1]] | |
X = np.asarray(X) | |
y = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] | |
y = np.asarray(y) | |
h = .02 # step size in the mesh | |
# Create color maps | |
cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA', '#AAAAFF']) | |
cmap_bold = ListedColormap(['#FF0000', '#00FF00', '#0000FF']) | |
# we create an instance of Neighbours Classifier and fit the data. | |
clf = neighbors.KNeighborsClassifier(n_neighbors) | |
clf.fit(X, y) | |
# Plot the decision boundary. For that, we will assign a color to each | |
# point in the mesh [x_min, x_max]x[y_min, y_max]. | |
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 | |
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 | |
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), | |
np.arange(y_min, y_max, h)) | |
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) | |
# Put the result into a color plot | |
Z = Z.reshape(xx.shape) | |
plt.figure() | |
plt.pcolormesh(xx, yy, Z, cmap=cmap_light) | |
# Plot also the training points | |
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold, | |
edgecolor='k', s=20) | |
plt.xlim(xx.min(), xx.max()) | |
plt.ylim(yy.min(), yy.max()) | |
plt.text(3.7, 3.8, 'red: Class C1', color='red') | |
plt.text(3.7, 3.6, 'blue: Class C2', color='blue') | |
plt.xlabel('X Axis') | |
plt.ylabel('Y Axis') | |
plt.show() | |
# Predict result | |
print(clf.predict([-3, 0])) | |
print(clf.predict([0, 4])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment