#! /usr/bin/env python3 # def wave_regression_ols ( ): #*****************************************************************************80 # ## wave_regression_ols() uses ordinary least squares regression on wave data. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 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 platform import sklearn print ( '' ) print ( 'wave_regression_ols():' ) print ( ' Python version: ' + platform.python_version ( ) ) print ( ' scikit-learn version: '+ sklearn.__version__ ) # # Generate the dataset. # print ( '' ) print ( ' Generate the wave dataset, (X, y).' ) X, y = mglearn.datasets.make_wave ( n_samples = 60 ) print ( " X.shape:", X.shape ) # # Plot the dataset. # print ( ' Plot the dataset.' ) plt.clf ( ) plt.plot ( X, y, 'o' ) plt.ylim ( -3.0, +3.0 ) plt.grid ( True ) plt.xlabel ( "Feature" ) plt.ylabel ( "Target" ) filename = "wave_regression_data.png" plt.savefig ( filename ) print ( " Graphics saved as '" + filename + "'" ) # # Split the dataset. # from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split ( X, y, random_state = 42 ) # # Create the model. # # Note that the model "lr" will include many components or members # that can be accessed using the "lr." prefix. # In particular, the coefficients and intercept of the linear model # are then created by calling "lr.fit()", and are then accessible # as lr.coef_ and lr.intercept_. # # from sklearn.linear_model import LinearRegression lr = LinearRegression ( ) lr.fit ( X_train, y_train ) a = lr.coef_ b = lr.intercept_ print ( ) print ( ' Produce linear regression coefficients y = a * x + b' ) print ( ' a = ', a, ' b = ', b ) print ( '' ) print ( ' Training score:' ) print ( lr.score ( X_train, y_train ) ) print ( ' Test score:' ) print ( lr.score ( X_test, y_test ) ) # # Compare data to least squares line. # print ( '' ) print ( 'Compare least squares line to data.' ) plt.plot ( X, y, 'bo' ) Xmin = -3.0 Ymin = a * Xmin + b Xmax = +3.0 Ymax = a * Xmax + b plt.plot ( [Xmin,Xmax], [Ymin,Ymax], 'r-' ) plt.ylim ( Xmin, Xmax ) plt.grid ( True ) plt.xlabel ( "Feature" ) plt.ylabel ( "Target" ) filename = "wave_regression_ols.png" plt.savefig ( filename ) print ( " Graphics saved as '" + filename + "'" ) # # Terminate. # print ( '' ) print ( 'wave_regression_ols():' ) 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 ( ) wave_regression_ols ( ) timestamp ( )