#! /usr/bin/env python3 # def regression_1d_data ( data_num ): #*****************************************************************************80 # ## regression_1d_data creates data for 1D regression. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 April 2019 # # Author: # # John Burkardt # # Parameters: # # Input, integer data_num, the number of data items to create. # # Output, real data(data_num,2), the regression data. # import numpy as np xmin = - 1.0 xmax = + 2.0 x = xmin + ( xmax - xmin ) * np.random.rand ( data_num ) y = 0.5 * x**2 + x + 1.5 y = y + 0.30 * np.random.randn ( data_num ) data = np.zeros ( [ data_num, 2 ] ) data[:,0] = x data[:,1] = y return data def regression_1d_data_test ( ): #*****************************************************************************80 # ## regression_1d_data_test tests regression_1d_dataE. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 25 April 2019 # # Author: # # John Burkardt # import matplotlib.pyplot as plt import numpy as np import platform print ( '' ) print ( 'regression_1d_data_test:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Create training and testing data for 1d linear regression.' ) print ( '' ) seed = 123456789 np.random.seed ( seed ) print ( ' Using random number seed %d' % ( seed ) ) train_num = 100 print ( ' Generating %d training items' % ( train_num ) ) train_data = regression_1d_data ( train_num ) filename = 'regression_1d_train.txt' r8mat_write ( filename, train_num, 2, train_data ) print ( ' Training data written to "%s".' % ( filename ) ) test_num = 10 print ( ' Generating %d testing items' % ( test_num ) ) test_data = regression_1d_data ( test_num ) filename = 'regression_1d_test.txt' r8mat_write ( filename, test_num, 2, test_data ) print ( ' Training data written to "%s".' % ( filename ) ) # # Plot the data. # plt.plot ( train_data[:,0], train_data[:,1], 'bo' ) plt.plot ( test_data[:,0], test_data[:,1], 'ro' ) plt.grid ( True ) plt.xlabel ( '<--- X --->', fontsize = 16 ) plt.ylabel ( '<--- Y --->', fontsize = 16 ) plt.title ( 'Data: train (blue), test (red)', fontsize = 16 ) filename = 'regression_1d_data.png' plt.savefig ( filename ) print ( '' ) print ( ' Graphics saved as "%s"' % ( filename ) ) plt.show ( ) # # Terminate. # print ( '' ) print ( 'regression_1d_data_test:' ) print ( ' Normal end of execution.' ) return def r8mat_write ( filename, m, n, a ): #*****************************************************************************80 # ## R8MAT_WRITE writes an R8MAT to a file. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 12 October 2014 # # Author: # # John Burkardt # # Parameters: # # Input, string FILENAME, the name of the output file. # # Input, integer M, the number of rows in A. # # Input, integer N, the number of columns in A. # # Input, real A(M,N), the matrix. # output = open ( filename, 'w' ) for i in range ( 0, m ): for j in range ( 0, n ): s = ' %g' % ( a[i,j] ) output.write ( s ) output.write ( '\n' ) output.close ( ) return def timestamp ( ): #*****************************************************************************80 # ## TIMESTAMP prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # # Parameters: # # None # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == '__main__' ): timestamp ( ) regression_1d_data_test ( ) timestamp ( )