0% found this document useful (0 votes)
12 views2 pages

Practical 4

Uploaded by

manasishivarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Practical 4

Uploaded by

manasishivarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import pandas as pd

import numpy as np

df=pd.read_csv("diabetes.csv")
df.head()

Pregnancies Glucose BloodPressure SkinThickness Insulin


BMI \
0 6 148 72 35 0 33.6

1 1 85 66 29 0 26.6

2 8 183 64 0 0 23.3

3 1 89 66 23 94 28.1

4 0 137 40 35 168 43.1

Pedigree Age Outcome


0 0.627 50 1
1 0.351 31 0
2 0.672 32 1
3 0.167 21 0
4 2.288 33 1

df['Outcome'].value_counts()

Outcome
0 500
1 268
Name: count, dtype: int64

x = df.drop('Outcome',axis=1)
y = df['Outcome']

from sklearn.model_selection import train_test_split


x_train, x_test, y_train, y_test = train_test_split(x, y,
test_size=0.3,random_state=53)

from sklearn.neighbors import KNeighborsClassifier


knn = KNeighborsClassifier(n_neighbors=5) # You can adjust n_neighbors
knn.fit(x_train, y_train)

KNeighborsClassifier()

y_pred=knn.predict(x_test)
y_pred

array([0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1,
1,
1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0,
0,
0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0,
1,
0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1,
0,
0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1,
0,
1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1,
0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0,
1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1,
0,
0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1,
1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1,
1,
0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1], dtype=int64)

from sklearn.metrics import confusion_matrix, accuracy_score,


precision_score,recall_score

conf_matrix = confusion_matrix(y_test, y_pred)


print("Confusion Matrix:\n", conf_matrix)

Confusion Matrix:
[[113 30]
[ 29 59]]

# Print the evaluation metrics


accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy*100,"%")

Accuracy: 74.45887445887446 %

error_rate = 1 - accuracy
print("Error Rate:", error_rate)

Error Rate: 0.2554112554112554

precision = precision_score(y_test, y_pred)


print("Precision:", precision)

Precision: 0.6629213483146067

recall = recall_score(y_test, y_pred)


print("Recall:", recall)

Recall: 0.6704545454545454

You might also like