0% found this document useful (0 votes)
10 views1 page

Omml

The document outlines a Python script that implements a K-Nearest Neighbors (KNN) classifier to predict diabetes outcomes using a dataset. It includes steps for loading the data, splitting it into training and testing sets, training the model, and calculating performance metrics such as confusion matrix, accuracy, precision, and recall. Finally, it prints the computed results.

Uploaded by

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

Omml

The document outlines a Python script that implements a K-Nearest Neighbors (KNN) classifier to predict diabetes outcomes using a dataset. It includes steps for loading the data, splitting it into training and testing sets, training the model, and calculating performance metrics such as confusion matrix, accuracy, precision, and recall. Finally, it prints the computed results.

Uploaded by

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

# Import libraries

import pandas as pd
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

# Load the dataset


data = pd.read_csv('diabetes.csv')
X = data.drop('Outcome', axis=1)
y = data['Outcome']

# Split the data


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

# Train the KNN model


knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)

# Compute metrics
conf_matrix = confusion_matrix(y_test, y_pred)
accuracy = accuracy_score(y_test, y_pred)
error_rate = 1 - accuracy
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)

# Print results
print("Confusion Matrix:\n", conf_matrix)
print("Accuracy:", accuracy)
print("Error Rate:", error_rate)
print("Precision:", precision)
print("Recall:", recall)

You might also like