forked from DL-Academy/MachineLearningSKLearn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn_classifier
46 lines (40 loc) · 1015 Bytes
/
knn_classifier
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
40
41
42
43
44
45
46
import numpy as np
import pandas as pd
from sklearn import neighbors, metrics, svm
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from matplotlib import pyplot as plt
data = pd.read_csv('car.data')
print(data.head())
X = data[['buying', 'maint', 'safety']].values
y = data[['class']]
X = np.array(X)
print(X)
#converting data
#X
Le = LabelEncoder()
for i in range(len(X[0])):
X[:, i] = Le.fit_transform(X[:, i])
print(X)
#y
label_mapping = {
'unacc':0,
'acc':1,
'good':2,
'vgood':3
}
y['class'] = y['class'].map(label_mapping)
y = np.array(y)
#create model
print(X.shape)
print(y.shape)
knn = svm.SVC()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
knn.fit(X_train, y_train)
prediction = knn.predict(X_test)
accuracy = metrics.accuracy_score(y_test, prediction)
print("predictions:", prediction)
print("accuracy: ", accuracy)
a = 1727
print("actual value ", y[a])
print("predicted value", knn.predict(X)[a])