#! /usr/bin/env python3 # def cancer_classify_mlp ( ): #*****************************************************************************80 # ## cancer_classify_mlp() uses a multilayer perceptron to classify cancer data. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 August 2023 # # Author: # # Andreas Mueller, Sarah Guido. # This version by John Burkardt. # # Reference: # # Andreas Mueller, Sarah Guido, # Introduction to Machine Learning with Python, # OReilly, 2017, # ISBN: 978-1-449-36941-5 # from sklearn.datasets import load_breast_cancer from sklearn.model_selection import train_test_split from sklearn.neural_network import MLPClassifier import matplotlib.pyplot as plt import mglearn import numpy as np import platform import sklearn print ( '' ) print ( 'cancer_classify_mlp():' ) print ( ' Python version: ' + platform.python_version ( ) ) print ( ' scikit-learn version: '+ sklearn.__version__ ) print ( ' Use a multilayer perceptron to classify cancer data.' ) # # Generate the dataset. # print ( '' ) print ( ' Retrieve the cancer dataset, (X, y).' ) cancer = load_breast_cancer ( ) # # Split the data. # X_train, X_test, y_train, y_test = train_test_split ( \ cancer.data, cancer.target, stratify = cancer.target, random_state = 66 ) # # Fit the classifier. # mlp = MLPClassifier ( random_state = 42 ) mlp.fit ( X_train, y_train ) # # Compute training and testing accuracy for varying number of neighbors. # print ( ' Training accuracy = ', mlp.score ( X_train, y_train ) ) print ( ' Testing accuracy = ', mlp.score ( X_test, y_test ) ) print ( '' ) print ( 'Compute mean and standard deviation, and rescale the data.' ) # # Compute mean and standard deviation. # mean_on_training = X_train.mean ( axis = 0 ) std_on_training = ( X_train - mean_on_training ).std ( axis = 0 ) # # Rescale the data. # X_train_scaled = ( X_train - mean_on_training ) / std_on_training X_test_scaled = ( X_test - mean_on_training ) / std_on_training # # Fit the classifier to the rescaled data. # mlp = MLPClassifier ( random_state = 0 ) mlp.fit ( X_train_scaled, y_train ) # # Compute training and testing accuracy for varying number of neighbors. # print ( ' Training accuracy = ', mlp.score ( X_train_scaled, y_train ) ) print ( ' Testing accuracy = ', mlp.score ( X_test_scaled, y_test ) ) # # Increase the number of iterations, and now # fit the classifier to the rescaled data. # print ( '' ) print ( 'Increase the number of iterations, and repeat classification.' ) mlp = MLPClassifier ( random_state = 0, max_iter = 1000 ) mlp.fit ( X_train_scaled, y_train ) # # Increase alpha, and now # fit the classifier to the rescaled data. # print ( '' ) print ( 'Increase alpha, and repeat classification.' ) mlp = MLPClassifier ( random_state = 0, max_iter = 1000, alpha = 1.0 ) mlp.fit ( X_train_scaled, y_train ) # # Compute training and testing accuracy for varying number of neighbors. # print ( ' Training accuracy = ', mlp.score ( X_train_scaled, y_train ) ) print ( ' Testing accuracy = ', mlp.score ( X_test_scaled, y_test ) ) # # Display the weights that were learned for the first hidden layer. # print ( '' ) print ( 'Plot the weight learned for the first hidden layer.' ) plt.clf ( ) plt.figure ( figsize = ( 20, 5 ) ) plt.imshow ( mlp.coefs_[0], interpolation = 'none', cmap = 'viridis' ) plt.yticks ( range ( 30 ), cancer.feature_names ) plt.xlabel ( 'Columns in weight matrix' ) plt.ylabel ( 'Input features' ) plt.colorbar ( ) filename = 'cancer_classify_mlp.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) plt.close ( ) # # Terminate. # print ( '' ) print ( 'cancer_classify_mlp():' ) 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_mlp ( ) timestamp ( )