# -*- coding: utf-8 -*-
"""
Created on Sat Apr 11 20:06:32 2020

@author: martin
"""

import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsClassifier
from  sklearn import model_selection
from sklearn.datasets import load_breast_cancer 
cancer = load_breast_cancer () 
X_train , X_test , y_train , y_test = model_selection.train_test_split( cancer . data , cancer . target , stratify = cancer . target , random_state = 66 ) 
training_accuracy = []
test_accuracy = [] 
# try n_neighbors from 1 to 10 
neighbors_settings = range ( 1 , 11 ) 
for n_neighbors in neighbors_settings : 
    # build the model 
    clf = KNeighborsClassifier ( n_neighbors = n_neighbors ) 
    clf . fit ( X_train , y_train ) 
    # record training set accuracy 
    training_accuracy . append ( clf . score ( X_train , y_train )) 
    # record generalization accuracy 
    test_accuracy . append ( clf . score ( X_test , y_test )) 
plt . plot ( neighbors_settings , training_accuracy , label = "training accuracy" ) 
plt . plot ( neighbors_settings , test_accuracy , label = "test accuracy" ) 
plt . ylabel ( "Accuracy" ) 
plt . xlabel ( "n_neighbors" ) 
plt . legend ()