11/10/2024, ml4.
ipynb -
23:20 Colab
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv('diabetes.csv')
df.head()
Pregnancies Glucose BloodPressure SkinThickness Insulin BMI Pedigree Age Outcom
e
0 6 148 72 35 0 33.6 0.627 50 1
1 1 85 66 29 0 26.6 0.351 31 0
2 8 183 64 0 0 23.3 0.672 32 1
3 1 89 66 23 94 28.1 0.167 21 0
4 0 137 40 35 168 43.1 2.288 33 1
df
Pregnancies Glucose BloodPressure SkinThickness Insulin BMI Pedigree Age Outcome
0 6 148 72 35 0 33.6 0.627 50 1
1 1 85 66 29 0 26.6 0.351 31 0
2 8 183 64 0 0 23.3 0.672 32 1
3 1 89 66 23 94 28.1 0.167 21 0
4 0 137 40 35 168 43.1 2.288 33 1
... ... ... ... ... ... ... ... ... ...
763 10 101 76 48 180 32.9 0.171 63 0
764 2 122 70 27 0 36.8 0.340 27 0
765 5 121 72 23 112 26.2 0.245 30 0
766 1 126 60 0 0 30.1 0.349 47 1
767 1 93 70 31 0 30.4 0.315 23 0
768 rows × 9 columns
df.dtypes
Pregnancies int64
Glucose
int64 BloodPressure
int64
SkinThickness int64
Insulin int64
BMI float64
Pedigree float64
Age int64
Outcome int64
sns.heatmap(df.corr(),annot=True)
https://colab.research.google.com/drive/ 1/
1kw0teWnkZVDNbX2zdJoibQzSerRDrbD9#printMode=true 3
11/10/2024, ml4.ipynb -
23:20 Colab
<Axes: >
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.2,random_state=42)
from sklearn.neighbors import KNeighborsClassifier
knn=KNeighborsClassifier()
knn.fit(x_train,y_train)
▾ KNeighborsClassifier i ?
KNeighborsClassifier()
y_pred=knn.predict(x_test)
from sklearn.metrics import accuracy_score,confusion_matrix,classification_report
accuracy_score(y_test,y_pred
) 0.6623376623376623
cm =
confusion_matrix(y_test,y_pred)
cm
array([[70, 29],
[23, 32]])
print(classification_report(y_test,y_pred))
precision recall f1-score support
0 0.75 0.71 0.73 99
1 0.52 0.58 0.55 55
accuracy 0.66 154
macro avg 0.64 0.64 0.64 154
weighted 0.67 0.66 0.67 154
avg
https://colab.research.google.com/drive/ 2/
1kw0teWnkZVDNbX2zdJoibQzSerRDrbD9#printMode=true 3
11/10/2024, ml4.ipynb -
23:20 Colab
https://colab.research.google.com/drive/ 3/
1kw0teWnkZVDNbX2zdJoibQzSerRDrbD9#printMode=true 3