-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn.txt
41 lines (29 loc) · 1.21 KB
/
knn.txt
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
#Implement K-Nearest Neighbors algorithm on diabetes.csv dataset
import numpy as np
import pandas as pd
data = pd.read_csv('./diabetes.csv')
data.head()
#Check for null or missing values
data.isnull().sum()
#Replace zero values with mean values
for column in data.columns[1:-3]:
data[column].replace(0, np.NaN, inplace = True)
data[column].fillna(round(data[column].mean(skipna=True)), inplace = True)
data.head(10)
X = data.iloc[:, :8] #Features
Y = data.iloc[:, 8:] #Predictor
#Perform Spliting
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
#KNN
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier()
knn_fit = knn.fit(X_train, Y_train.values.ravel())
knn_pred = knn_fit.predict(X_test)
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score, accuracy_score
print("Confusion Matrix")
print(confusion_matrix(Y_test, knn_pred))
print("Accuracy Score:", accuracy_score(Y_test, knn_pred)*100)
print("Reacal Score:", recall_score(Y_test, knn_pred)*100)
print("F1 Score:", f1_score(Y_test, knn_pred)*100)
print("Precision Score:",precision_score(Y_test, knn_pred)*100)