#! /usr/bin/env python3 # def exercise2 ( ): #*****************************************************************************80 # ## exercise2() classifies gold coins as fake(0) or legitimate(1). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 11 February 2022 # # Author: # # John Burkardt # import matplotlib.pyplot as plt import numpy as np from logistic_regression import logistic_regression print ( '' ) print ( 'exercise2():' ) print ( ' Seek parameters for logistic regression example.' ) print ( ' We have M samples of gold coins.' ) print ( ' Data includes g, the weight in grams, and' ) print ( ' y, a label of 0 for fake coins, 1 for real coins.' ) print ( ' We seek a logistic regression model' ) print ( ' y = 1 / ( 1 + exp ( - ( w(0) + w(1) * g(:) ) )' ) print ( ' for which we need to estimate the weights w.' ) print ( '' ) print ( ' Use gradient descent.' ) # # Read the data file. # data = np.loadtxt ( 'gold_data.txt' ) # # First column is weight in grams. # Second column is Y (genuine = 1, counterfeit = 0). # g = data[:,0] y = data[:,1] m = len ( g ) # # Normalize the G data. # gmin = np.min ( g ) gmax = np.max ( g ) gn = ( g - np.min ( g ) ) / ( np.max ( g ) - np.min ( g ) ) # # Create a data array X = [ 1 | g ]. # X = np.zeros ( [ m, 2 ] ) X[:,0] = 1 X[:,1] = gn # # Set the learning rate ALPHA # alpha = 0.02 # # Set the number of iterations KMAX. # kmax = 100000 # # Compute logistic weights W. # w = logistic_regression ( X, y, alpha, kmax ) print ( '' ) print ( ' Estimated weights W = (%g,%g)' % ( w[0], w[1] ) ) # # Display the data. # gp = np.linspace ( -1.0, 2.0, 101 ) gpu = gmin + ( gmax - gmin ) * gp cutoff = - w[0] / w[1] cutoff2 = gmin + ( gmax - gmin ) * cutoff print ( ' Cutoff value for normalized data is ', cutoff ) print ( ' Cutoff value is ', cutoff2 ) yp = 1.0 / ( 1.0 + np.exp ( - ( w[0] + w[1] * gp ) ) ) plt.plot ( g, y, 'ro' ) plt.plot ( gpu, yp, linewidth = 3 ) plt.plot ( [cutoff2,cutoff2], [0,1], '--', color = 'r' ) plt.grid ( True ) plt.xlabel ( '<-- G: Weight in grams -->' ) plt.ylabel ( '<-- Y(G): Fake = 0, Real = 1 -->' ) plt.title ( 'Logistic regression for gold coin weights' ) filename = 'exercise2.png' print ( ' Graphics saved as "%s"' % ( filename ) ) plt.savefig ( filename ) plt.show ( ) plt.close ( ) # # Terminate. # print ( '' ) print ( 'exercise2():' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): exercise2 ( )