#! /usr/bin/env python3 # def exercise4 ( ): #*****************************************************************************80 # ## exercise3() applies logistic regression to caesarian delivery data. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 12 February 2022 # # Author: # # John Burkardt # from sklearn.linear_model import LogisticRegression import matplotlib.pyplot as plt import numpy as np print ( '' ) print ( 'exercise4():' ) print ( ' Seek parameters for logistic regression example.' ) print ( ' We have N examples of D-dimensional data X.' ) print ( ' For a model' ) print ( ' y(x) = 1 / ( 1 + exp ( - ( w(0) + w(1)*h + w(2)*t ) ) )' ) print ( ' determine the weights w.' ) # # Read the data file. # data = np.loadtxt ( 'caesarian_data.txt' ) # # # Third column is Y (comfortable = 1, no = 0). # age = data[:,0] num = data[:,1] tim = data[:,2] pre = data[:,3] hrt = data[:,4] cae = data[:,5] n = len ( age ) d = 6 # # Create a data array X = [ 1 | age | num | tim | pre | hrt ]. # X = np.zeros ( [ n, d ] ) X[:,0] = 1 X[:,1] = age X[:,2] = num X[:,3] = tim X[:,4] = pre X[:,5] = hrt y = cae.copy ( ) # # 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 ( ( y - yp )**2 ) print ( ' MSE = ', mse ) # # For some reason, w[0] does NOT contain the intercept. # What is in w[0] I don't know. # Luckily, the intercept is available, so let's get it and copy # it into w[0], where it belongs. # w = classifier.coef_[0].copy() w[0] = classifier.intercept_ print ( '' ) print ( ' Computed weights W = ', w ) # # To evaluate new data, ynew = classifier.predict ( Xnew ) # Xnew = np.array ( [ \ [ 1, 29, 2, 2, 0, 1 ], \ [ 1, 33, 2, 0, 2, 0 ], \ [ 1, 22, 0, 1, 1, 0 ] ] ) ynew = classifier.predict ( Xnew ) print ( ' New data:' ) print ( Xnew ) print ( ' Caesarian recommendation?' ) print ( ynew ) # # Terminate. # print ( '' ) print ( 'exercise4():' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): exercise4 ( )