-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml_knn.py
executable file
·52 lines (36 loc) · 1.06 KB
/
ml_knn.py
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
47
48
49
50
51
52
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 16 00:08:45 2018
@author: BHANU
"""
import pandas as pd
import sklearn
from sklearn.neighbors import KNeighborsClassifier
headers = ['age','year','nodes']
raw = open('haberman.data.txt')
sur_stat = []
features = []
#Preparing the data
for line in raw:
i=0
var = []
for y in line.split(','):
if (i==3):
sur_stat.append([int(y)])
else:
var.append(int(y))
i+=1
features.append(var)
df_x = pd.DataFrame.from_records(features, columns = headers)
df_y = pd.DataFrame.from_records(sur_stat, columns = ['label'])
X_train, X_test, Y_train, Y_test = sklearn.model_selection.train_test_split(
df_x,df_y,test_size=0.2,random_state = 5)
knn = KNeighborsClassifier()
knn.fit(X_train,Y_train['label'].tolist())
y_pred = knn.predict(X_test)
correct = 0
for i in range(len(y_pred)):
if(y_pred[i]==Y_test['label'].tolist()[i]):
correct+=1
accuracy = correct/len(y_pred)
print('Accuracy = %s' % accuracy)