0% found this document useful (0 votes)
11 views

Assignment 4

Artificial intelligence Assignment

Uploaded by

rp7895798
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Assignment 4

Artificial intelligence Assignment

Uploaded by

rp7895798
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

assignment4

October 31, 2024

[12]: import pandas as pd


import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score,␣
↪recall_score

# Step 1: Load the dataset


data = pd.read_csv('diabetes.csv')
print(data)

# Step 2: Preprocessing
X = data.drop('Outcome', axis=1)
y = data['Outcome']

# Step 3: Split the dataset into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,␣
↪random_state=42)

# Step 4: Create and fit the KNN model


k = 5
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)

# Step 5: Make predictions


y_pred = knn.predict(X_test)

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
.. … … … … … …
763 10 101 76 48 180 32.9
764 2 122 70 27 0 36.8
765 5 121 72 23 112 26.2
766 1 126 60 0 0 30.1

1
767 1 93 70 31 0 30.4

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
.. … … …
763 0.171 63 0
764 0.340 27 0
765 0.245 30 0
766 0.349 47 1
767 0.315 23 0

[768 rows x 9 columns]

[13]: # Step 6: Compute metrics


# Compute confusion matrix
cm = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:\n", cm)

# Compute accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

# Compute error rate


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

# Compute precision
precision = precision_score(y_test, y_pred)
print("Precision:", precision)

# Compute recall
recall = recall_score(y_test, y_pred)
print("Recall:", recall)

Confusion Matrix:
[[70 29]
[23 32]]
Accuracy: 0.6623376623376623
Error Rate: 0.33766233766233766
Precision: 0.5245901639344263
Recall: 0.5818181818181818

You might also like