image_output_1.png
from sklearn import metrics

from sklearn.model_selection import cross_val_score, learning_curve, validation_curve

from sklearn.neighbors import KNeighborsClassifier

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

df2 = pd.read_csv('../input/diabetescsv/diabetes.csv')

X = df2.iloc[:, 0:8]
y = df2.iloc[:, 8]

val_results = []
train_results = []

k_range = range(1, 21)

for k in k_range:
    clf_2 = KNeighborsClassifier(n_neighbors=k)
    clf_2 = clf_2.fit(X_train, y_train)
    pred_train = clf_2.predict(X_train)
    train_score = metrics.accuracy_score(y_train, pred_train)
    train_results.append(train_score)
    pred_val = clf_2.predict(X_val)
    val_score = metrics.accuracy_score(y_val, pred_val)
    val_results.append(val_score)

clf_2 = KNeighborsClassifier()

def plot_validation_curve(clf, X, y, param_name, param_range):
    train_scores, test_scores = validation_curve(clf, X, y, cv=10, scoring='accuracy', param_name=param_name, param_range=param_range, n_jobs=-1)
    x_range = param_range
    train_scores_mean = np.mean(train_scores, axis=1)
    test_scores_mean = np.mean(test_scores, axis=1)

plot_validation_curve(clf_2, X_train, y_train, param_name='n_neighbors', param_range=range(1, 21))