#! /usr/bin/env python3 # def exercise1(): #*****************************************************************************80 # ## exercise1() processes the hw_data dataset. # # Discussion: # # The data is stored as a text file, with spaces used as the delimiter. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 17 January 2022 # # Author: # # John Burkardt # import matplotlib.pyplot as plt import numpy as np print ( "exercise1():" ) print ( " Read the data from the datafile." ) print ( " Print the first five lines." ) print ( " Compute some statistical measurements." ) print ( " Standardize the data, and recompute the statistics." ) # # Read data. # filename = 'hw_data.txt' data = np.loadtxt ( filename ) rows, cols = np.shape ( data ) print ( '' ) print ( ' "' + filename + '" contains', rows, 'rows and', cols, 'columns.' ) # # Print the first five lines. # print ( '' ) print ( ' First five lines of data:' ) print ( '' ) print ( data[0:5,:] ) # # Statistics. # print ( '' ) print ( ' Statistics for data:' ) print ( '' ) print ( ' data.shape: ', data.shape ) print ( ' np.min(data,axis=0): ', np.min ( data, axis = 0 ) ) print ( ' np.mean(data,axis=0): ', np.mean ( data, axis = 0 ) ) print ( ' np.max(data,axis=0): ', np.max ( data, axis = 0 ) ) print ( ' np.std(data,axis=0): ', np.std ( data, axis = 0 ) ) print ( ' np.var(data,axis=0): ', np.var ( data, axis = 0 ) ) # # Standardize. # data2 = ( data - np.mean ( data, axis = 0 ) ) / np.std ( data, axis = 0 ) # # Scatter plot. # plt.scatter ( data2[:,0], data2[:,1] ) t = np.linspace ( 0, 2.0 * np.pi, 51 ) # # Add three rings. # x = np.cos ( t ) y = np.sin ( t ) plt.plot ( x, y, 'r-', linewidth = 2 ) plt.plot ( 2.0*x, 2.0*y, 'r-', linewidth = 2 ) plt.plot ( 3.0*x, 3.0*y, 'r-', linewidth = 2 ) plt.xlabel ( 'Height' ) plt.ylabel ( 'Weight' ) plt.title ( '(Height,Weight) standardized' ) plt.grid ( True ) plt.axis ( 'equal' ) plotfile = 'exercise1.png' plt.savefig ( plotfile ) print ( ' Graphics saved as "' + plotfile + '"' ) plt.show ( ) plt.close ( ) # # Terminate. # print ( "" ) print ( "exercise1():" ) print ( " Normal end of execution." ) return if ( __name__ == "__main__" ): exercise1 ( )