#! /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 # from sklearn.linear_model import LogisticRegression import matplotlib.pyplot as plt import numpy as np print ( '' ) print ( 'admit():' ) print ( ' We have M=100 english and math test scores' ) print ( ' and the corresponding college admission decision adm.' ) 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, adm=admit(0/1)) # data = np.loadtxt ( 'admit_data.txt' ) eng = data[:,0] mat = data[:,1] adm = data[:,2] n = len ( eng ) X = np.zeros ( [ n, 3 ] ) X[:,0] = 1 X[:,1] = eng[:] X[:,2] = mat[:] y = adm[:] # # Fit the logistic regression model to the data. # classifier = LogisticRegression(random_state=0).fit ( X, y ) # # Evaluate the model on the original data. # yp = classifier.predict ( X ) # # Compute MSE # mse = ( 1.0 / n ) * sum ( ( yp - adm )**2 ) print ( ' MSE = ', mse ) # # Construct the coefficient array W. # w = classifier.coef_[0].copy() w[0] = classifier.intercept_ print ( '' ) print ( ' Computed weights W = ', w ) # # Display data. # emin = np.min ( eng ) emax = np.max ( eng ) mmin = ( - w[0] - w[1] * emin ) / w[2] mmax = ( - w[0] - w[1] * emax ) / w[2] 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 ( [emin,emax], [mmin,mmax], '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 # X2 = np.array ( [ \ [ 1.0, 46, 81 ], \ [ 1.0, 26, 75 ], \ [ 1.0, 30, 66 ] ] ) print ( ' New student data:' ) print ( X2 ) y2 = classifier.predict ( X2 ) print ( ' Admit new student? (0/1)' ) print ( y2 ) p2 = classifier.predict_proba ( X2 ) print ( ' Strength of decision (0/1)' ) print ( p2 ) # # Terminate. # print ( '' ) print ( 'admit():' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): admit ( )