-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknn_example.py
More file actions
39 lines (30 loc) · 1.06 KB
/
knn_example.py
File metadata and controls
39 lines (30 loc) · 1.06 KB
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
import numpy as np
import pandas as pd
from sklearn import neighbors
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
data = pd.read_csv("car.data")
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)
# create a model
knn = neighbors.KNeighborsClassifier(n_neighbors=25, weights="uniform")
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.5)
knn.fit(X_train, y_train)
prediction = knn.predict(X_test)
score = accuracy_score(y_test, prediction)
print("Prediction: ", prediction)
print("Score: ", score)
print("------------------------------------------------------------------------------")
a = int(input("Enter a Value: "))
print("actual value: ", y[a])
print("prediction value", knn.predict(X)[a])