# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
# %matplotlib inline
# Necessary Packages
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Model and data processing packages
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import GridSearchCV
# -

np.random.seed(234)

df = pd.read_csv('diabetes.csv')
df.head(5)

df.shape

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

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

neighbors = np.arange(1,20)
train_accuracy = np.empty(len(neighbors))
test_accuracy = np.empty(len(neighbors))

print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)

for i,k in enumerate(neighbors):
    
    knn = KNeighborsClassifier(n_neighbors=k)
    
    knn.fit(X_train, y_train)
    
    train_accuracy[i] = knn.score(X_train, y_train)
    
    test_accuracy[i] = knn.score(X_test, y_test)

plt.plot(neighbors, test_accuracy, label='Testing Accuracy')
plt.plot(neighbors, train_accuracy, label='Training Accuracy')
plt.legend()
plt.xlabel('Number of Neighbors')
plt.ylabel('Accuracy')
plt.title('KNN Varying Number of Neighbors')
plt.show()

n_neighbors=18
knn = KNeighborsClassifier(n_neighbors)

knn.fit(X_train, y_train)

knn.score(X_test, y_test)

y_pred = knn.predict(X_test)

confusion_matrix(y_test, y_pred)

pd.crosstab(y_test, y_pred, rownames=['True'], colnames=['Predicted'], margins=True)

print(classification_report(y_test, y_pred))

y_pred_proba = knn.predict_proba(X_test)[:,1]

# FPR = False Positive Rate, TPR = True Positive Rate
fpr, tpr, thresholds = roc_curve(y_test, y_pred_proba)

plt.plot([0,1],[0,1], label='k--')
plt.plot(fpr, tpr, label='KNN')
plt.legend()
plt.xlabel('fpr')
plt.ylabel('tpr')
plt.title(f'KNN (n_neighbors={n_neighbors}) ROC Curve')
plt.show()

roc_auc_score(y_test, y_pred_proba)

param_grid = {'n_neighbors':np.arange(1,100)}

knn = KNeighborsClassifier()
knn_cv = GridSearchCV(knn, param_grid, cv=5)
knn_cv.fit(X, y)

knn_cv.best_score_

knn_cv.best_params_

n_neighbors=14
knn = KNeighborsClassifier(n_neighbors)

knn.fit(X_train, y_train)

knn.score(X_test, y_test)

y_pred = knn.predict(X_test)

confusion_matrix(y_test, y_pred)

pd.crosstab(y_test, y_pred, rownames=['True'], colnames=['Predicted'], margins=True)

print(classification_report(y_test, y_pred))

y_pred_proba = knn.predict_proba(X_test)[:,1]

y_pred_proba = knn.predict_proba(X_test)[:,1]
fpr, tpr, thresholds = roc_curve(y_test, y_pred_proba)

plt.plot([0,1],[0,1], 'k--')
plt.plot(fpr, tpr, label='KNN')
plt.xlabel('fpr')
plt.ylabel('tpr')
plt.title(f'KNN (n_neighbors={n_neighbors}) ROC Curve')
plt.show()

roc_auc_score(y_test, y_pred_proba)



StarcoderdataJupyterScriptsDedupFiltered