-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknnvis.py
More file actions
38 lines (30 loc) · 931 Bytes
/
knnvis.py
File metadata and controls
38 lines (30 loc) · 931 Bytes
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
import pandas as pd
import matplotlib as plt
import numpy as np
from sklearn import neighbors, datasets
from mlxtend.plotting import plot_decision_regions
from sklearn.preprocessing import LabelEncoder
def knvis(data, k):
X = data[["buying", "maint", "safety"]].values
y = data[["class"]]
# convertion of string to numbers
# X
le = LabelEncoder()
for i in range(len(X[0])):
X[:, i] = le.fit_transform(X[:, i])
# y
label_mapping = {"unacc": 0, "acc": 1, "good": 2, "vgood": 3}
y["class"] = y["class"].map(label_mapping)
y = np.array(y)
clf = neighbors.KNeighborsClassifier(n_neighbors=k)
clf.fit(X, y)
# Plotting decission regions
plot_decision_regions(X, y, clf=clf, legend=2)
# axes annotations
plt.xlabel("X")
plt.ylabel("Y")
plt.title("Kn Vis")
plt.show()
knn = pd.read_csv("car.data")
for i in [1, 5, 20, 30, 40, 80]:
knvis(knn, i)