#! /usr/bin/env python3 # def diabetes ( ): #*****************************************************************************80 # ## diabetes() does logistic regression on diabetes diagnosis data. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 14 February 2022 # # Author: # # John Burkardt # from sklearn.linear_model import LogisticRegression import matplotlib.pyplot as plt import numpy as np print ( '' ) print ( 'diabetes():' ) print ( ' Logistic regression for diabetes diagnosis data.' ) # # Read the data: # data = np.loadtxt ( 'diabetes_data.txt' ) prg = data[:,0] glu = data[:,1] dbp = data[:,2] tri = data[:,3] ins = data[:,4] bmi = data[:,5] ped = data[:,6] age = data[:,7] dia = data[:,8] n = len ( prg ) X = np.zeros ( [ n, 9 ] ) X[:,0] = 1 X[:,1] = prg[:] X[:,2] = glu[:] X[:,3] = dbp[:] X[:,4] = tri[:] X[:,5] = ins[:] X[:,6] = bmi[:] X[:,7] = ped[:] X[:,8] = age[:] y = dia[:] # # Scale the data. # from sklearn import preprocessing scaler = preprocessing.StandardScaler().fit ( X ) X_scaled = scaler.transform ( X ) # # Fit the logistic regression model to the data. # classifier = LogisticRegression(random_state=0).fit ( X_scaled, y ) # # Evaluate the model on the original data. # yp = classifier.predict ( X_scaled ) # # Compute MSE # mse = ( 1.0 / n ) * sum ( ( yp - y )**2 ) print ( ' MSE = ', mse ) # # Construct the coefficient array W. # w = classifier.coef_[0].copy() w[0] = classifier.intercept_ print ( '' ) print ( ' Computed weights W = ', w ) # # Evaluate the admissions formula for new data. # # Raw values: # 1 46 81 # 2 26 75 # 3 30 66 # return X2 = np.array ( [ \ [ 1.0, 46, 81 ], \ [ 1.0, 26, 75 ], \ [ 1.0, 30, 66 ] ] ) print ( ' New medical data:' ) print ( X2 ) y2 = classifier.predict ( X2 ) print ( ' diabetes new student? (0/1)' ) print ( y2 ) p2 = classifier.predict_proba ( X2 ) print ( ' Strength of decision (0/1)' ) print ( p2 ) # # Terminate. # print ( '' ) print ( 'diabetes():' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): diabetes ( )