from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix
# Assuming 'Outcome' is the target variable in your dataset
X_train, X_test, y_train, y_test = train_test_split(diabetes.loc[:, diabetes.columns != 'Outcome'], diabetes['Outcome'], stratify=diabetes['Outcome'], random_state=66)
# Create and train the KNN classifier
knn = KNeighborsClassifier(n_neighbors=9)
knn.fit(X_train, y_train)
# Print accuracy on training and test sets
print('Accuracy of K-NN classifier on training set: {:.2f}'.format(knn.score(X_train, y_train)))
print('Accuracy of K-NN classifier on test set: {:.2f}'.format(knn.score(X_test, y_test)))
# Generate and print confusion matrix
y_pred = knn.predict(X_test)
conf_matrix = confusion_matrix(y_test, y_pred)
print('\nConfusion Matrix:\n', conf_matrix)
No comments:
Post a Comment