Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
- import sklearn
- # Load data
- knndata = pd.read_csv("./knndata.txt")
- print("knndata = ")
- print(knndata)
- print()
- X = knndata.loc[:, ["X1", "X2"]]
- y = knndata.loc[:, "Y"]
- # Show data
- plt.scatter(X[y == 1].X1, X[y == 1].X2, c="blue", marker="o", label="1")
- plt.scatter(X[y == 2].X1, X[y == 2].X2, c="red", marker="+", label="2")
- plt.title("Points")
- plt.xlabel("X1")
- plt.ylabel("X2")
- plt.legend()
- plt.show()
- # Classifier kNN
- from sklearn.neighbors import KNeighborsClassifier
- k = 1
- clf = KNeighborsClassifier(n_neighbors=k)
- clf = clf.fit(X, y)
- # Predict
- new_X1 = 0.7
- new_X2 = 0.4
- new_X = [new_X1, new_X2]
- print("k = " + str(k))
- print(clf.predict([new_X])) # [new], where new = [..., ...]
- print(clf.predict_proba([new_X])) # [new], where new = [..., ...]
- print()
- k = 5
- clf = KNeighborsClassifier(n_neighbors=k)
- clf = clf.fit(X, y)
- # Predict
- new_X1 = 0.7
- new_X2 = 0.4
- new_X = [new_X1, new_X2]
- print("k = " + str(k))
- print(clf.predict([new_X])) # [new], where new = [..., ...]
- print(clf.predict_proba([new_X])) # [new], where new = [..., ...]
- print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement