#! /usr/bin/env python3 # def cancer_classify_knn ( ): #*****************************************************************************80 # ## cancer_classify_knn() uses k-nearest neighbor classification on cancer data. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 15 June 2023 # # Author: # # Andreas Mueller, Sarah Guido. # Modifications by John Burkardt. # # Reference: # # Andreas Mueller, Sarah Guido, # Introduction to Machine Learning with Python, # OReilly, 2017, # ISBN: 978-1-449-36941-5 # import matplotlib.pyplot as plt import mglearn import numpy as np import pandas as pd import platform import sklearn print ( '' ) print ( 'cancer_classify_knn():' ) print ( ' Python version: ' + platform.python_version ( ) ) print ( ' scikit-learn version: '+ sklearn.__version__ ) # # Generate the dataset. # print ( '' ) print ( ' Retrieve the cancer dataset, (X, y).' ) from sklearn.datasets import load_breast_cancer cancer = load_breast_cancer ( ) from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split ( \ cancer.data, cancer.target, stratify = cancer.target, random_state = 66 ) # # Compute training and testing accuracy for varying number of neighbors. # from sklearn.neighbors import KNeighborsClassifier training_accuracy = [] test_accuracy = [] for n_neighbors in range ( 1, 11 ): clf = KNeighborsClassifier ( n_neighbors = n_neighbors ) clf.fit ( X_train, y_train ) training_accuracy.append ( clf.score ( X_train, y_train ) ) test_accuracy.append ( clf.score ( X_test, y_test ) ) plt.clf ( ) plt.plot ( range ( 1, 11 ), training_accuracy, label = 'Training accuracy' ) plt.plot ( range ( 1, 11 ), test_accuracy, label = 'Test accuracy' ) plt.ylabel ( 'Accuracy' ) plt.xlabel ( 'n_neighbors' ) plt.legend ( ) filename = 'cancer_classify_knn.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) # # Terminate. # print ( '' ) print ( 'cancer_classify_knn():' ) print ( ' Normal end of execution.' ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 August 2019 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == '__main__' ): timestamp ( ) cancer_classify_knn ( ) timestamp ( )