#! /usr/bin/env python3 # def moon_classify_mlp ( ): #*****************************************************************************80 # ## moon_classify_mlp() uses a multilayer perceptron to classify the moon data. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 05 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 make_moons from sklearn.neural_network import MLPClassifier from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt import mglearn import numpy as np import platform import sklearn print ( '' ) print ( 'moon_classify_mlp():' ) print ( ' Python version: ' + platform.python_version ( ) ) print ( ' scikit-learn version: '+ sklearn.__version__ ) print ( ' Generate 100 samples of the artificial moon dataset.' ) print ( ' Classify the data using a multilayer perceptron.' ) # # Create the data. # X, y = make_moons ( n_samples = 100, noise = 0.25, random_state = 3 ) # # Split the data into training and test sets. # X_train, X_test, y_train, y_test = train_test_split ( \ X, y, stratify = y, random_state = 42 ) # # Create an MLP classifier and fit it to the training data. # By default, 100 hidden nodes are used. # mlp = MLPClassifier ( solver = 'lbfgs', random_state = 0 ) mlp.fit ( X_train, y_train ) # # Display the data. # plt.clf ( ) mglearn.plots.plot_2d_separator ( mlp, X_train, fill = True, alpha = 0.3 ) mglearn.discrete_scatter ( X_train[:,0], X_train[:,1], y_train ) plt.grid ( True ) plt.xlabel ( "Feature 0" ) plt.ylabel ( "Feature 1" ) plt.title ( "MLP classifies moon data (100 nodes)" ) filename = 'moon_classify_mlp_100.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) # # Repeat, but use just 10 hidden nodes. # mlp = MLPClassifier ( solver = 'lbfgs', random_state = 0, hidden_layer_sizes = [10], max_iter = 500 ) mlp.fit ( X_train, y_train ) # # Display the data. # plt.clf ( ) mglearn.plots.plot_2d_separator ( mlp, X_train, fill = True, alpha = 0.3 ) mglearn.discrete_scatter ( X_train[:,0], X_train[:,1], y_train ) plt.grid ( True ) plt.xlabel ( "Feature 0" ) plt.ylabel ( "Feature 1" ) plt.title ( "MLP classifies moon data (10 nodes)" ) filename = 'moon_classify_mlp_010.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) # # Repeat, but use two layers of 10 nodes. # mlp = MLPClassifier ( solver = 'lbfgs', random_state = 0, hidden_layer_sizes = [10,10], max_iter = 500 ) mlp.fit ( X_train, y_train ) # # Display the data. # plt.clf ( ) mglearn.plots.plot_2d_separator ( mlp, X_train, fill = True, alpha = 0.3 ) mglearn.discrete_scatter ( X_train[:,0], X_train[:,1], y_train ) plt.grid ( True ) plt.xlabel ( "Feature 0" ) plt.ylabel ( "Feature 1" ) plt.title ( "MLP classifies moon data (10+10 nodes)" ) filename = 'moon_classify_mlp_010_010.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) # # Repeat, but use two layers of 10 nodes and tanh nonlinearity. # mlp = MLPClassifier ( solver = 'lbfgs', random_state = 0, hidden_layer_sizes = [10,10], activation = 'tanh', max_iter = 500 ) mlp.fit ( X_train, y_train ) # # Display the data. # plt.clf ( ) mglearn.plots.plot_2d_separator ( mlp, X_train, fill = True, alpha = 0.3 ) mglearn.discrete_scatter ( X_train[:,0], X_train[:,1], y_train ) plt.grid ( True ) plt.xlabel ( "Feature 0" ) plt.ylabel ( "Feature 1" ) plt.title ( "MLP classifies moon data (10+10 nodes + tanh)" ) filename = 'moon_classify_mlp_010_010_tanh.png' plt.savefig ( filename ) print ( ' Graphics saved as "' + filename + '"' ) # # Terminate. # print ( '' ) print ( 'moon_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 ( ) moon_classify_mlp ( ) timestamp ( )