#! /usr/bin/env python3 # def admit ( ): #*****************************************************************************80 # ## admit does logistic regression on college admission data. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 13 October 2019 # # Author: # # John Burkardt # import matplotlib.pyplot as plt import numpy as np from logistic_regression import logistic_regression print ( '' ) print ( 'admit:' ) print ( ' We have M=100 english and math test scores' ) print ( ' and the corresponding college admission decision y.' ) print ( ' Consider the model' ) print ( ' y(x) = 1 / ( 1 + exp ( - w\'x ) )' ) print ( ' Determine weights w, and apply to 3 new cases.' ) # # Read the data: (x1=english, x2=math, y=admit(0/1)) # data = np.loadtxt ( 'admit_data.txt' ) eng = data[:,0] mat = data[:,1] admit = data[:,2] m = len ( eng ) # # Normalize the input data. # eng = ( eng - np.min ( eng ) ) / ( np.max ( eng ) - np.min ( eng ) ) mat = ( mat - np.min ( mat ) ) / ( np.max ( mat ) - np.min ( mat ) ) # # Create arrays x and y. # x = np.zeros ( [ m, 3 ] ) x[:,0] = 1 x[:,1] = eng[:] x[:,2] = mat[:] y = admit[:] alpha = 0.01 kmax = 10000 w = logistic_regression ( x, y, alpha, kmax ) print ( '' ) print ( ' Using ALPHA = %g' % ( alpha ) ) print ( ' Final W = (%g,%g,%g)' % ( w[0], w[1], w[2] ) ) # # Display data. # plt.plot ( x[y==0,1], x[y==0,2], 'r.', markersize = 15 ) plt.plot ( x[y==1,1], x[y==1,2], 'b.', markersize = 15 ) plt.plot ( [-w[0]/w[1], (-w[0]-w[2])/w[1]], [0,1], 'k-', linewidth = 3 ) plt.xlabel ( '<-- English Exam -->' ) plt.ylabel ( '<-- Math Exam -->' ) plt.title ( 'Blue = Admitted, Red = Not admitted', fontsize = 16 ) plt.grid ( True ) filename = 'admit.png' plt.savefig ( filename ) plt.show ( ) print ( '' ) print ( ' Graphics saved as "%s"' % ( filename ) ) # # Evaluate the admissions formula for new data. # # Raw values: # 1 46 81 # 2 26 75 # 3 30 66 # # Normalized values: # # 1 0.66 0.88 # 2 0.22 0.74 # 3 0.31 0.55 # x1 = np.array ( [ 1.0, 0.66, 0.88 ] ) wtx = np.dot ( w, x1 ) y1 = 1.0 / ( 1.0 + np.exp ( - wtx ) ) print ( 'Student 1: wtx = ', wtx, ' Y = ', y1 ) x2 = np.array ( [ 1.0, 0.22, 0.74 ] ) wtx = np.dot ( w, x2 ) y2 = 1.0 / ( 1.0 + np.exp ( - wtx ) ) print ( 'Student 2: wtx = ', wtx, ' Y = ', y2 ) x3 = np.array ( [ 1.0, 0.31, 0.55 ] ) wtx = np.dot ( w, x3 ) y3 = 1.0 / ( 1.0 + np.exp ( - wtx ) ) print ( 'Student 3: wtx = ', wtx, ' Y = ', y3 ) # # Terminate. # print ( '' ) print ( 'admit:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): admit ( )