#! /usr/bin/env python3 # from sklearn import * def study ( ): #*****************************************************************************80 # ## study is an artificial logistic regression example using six points. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 27 January 2020 # # Author: # # John Burkardt # import numpy as np import matplotlib.pyplot as plt import platform import sklearn from sklearn import linear_model print ( '' ) print ( 'study' ) print ( ' python version: %s' % ( platform.python_version ( ) ) ) print ( ' scikit-learn version is %s' % ( sklearn.__version__ ) ) print ( ' Use sklearn LogisticRegression() on the study-time example.' ) print ( '' ) x = np.array( [ 0.50, 0.75, 1.00, 1.25, 1.50, 1.75, 1.75, 2.00, 2.25, 2.50, 2.75, 3.00, 3.25, 3.50, 4.00, 4.25, 4.50, 4.75, 5.00, 5.00 ] ) xx = np.reshape ( x, [20,1] ) y = np.array ( [ 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1 ] ) # xx = np.reshape ( x, [6,1] ) lr = linear_model.LogisticRegression ( ) lr.fit ( xx, y ) lr_coef = np.ndarray.flatten ( lr.coef_ ) lr_intercept = np.ndarray.flatten ( lr.intercept_ ) print ( lr_coef, lr_intercept ) cutoff = - lr_intercept / lr_coef print ( "cutoff", cutoff ) print ( "score", lr.score(xx,y) ) print ( "mean", np.mean(y) ) plt.scatter ( x, y ) x_eval = np.linspace ( 0, 6, 100 ) y_eval = logistic_fun ( x_eval * lr_coef + lr_intercept ) plt.plot ( x_eval, y_eval ) plt.axvline ( x = cutoff, linestyle = '--', color = 'red' ) plt.title ( 'study logistic regression example' ) plt.grid ( True ) filename = 'study.png' plt.savefig ( filename ) print ( '' ) print ( ' Graphics saved as "%s"' % ( filename ) ) plt.show ( ) print ( '' ) print ( 'study' ) print ( ' Normal end of execution.' ) return def logistic_fun ( x ): #*****************************************************************************80 # ## logistic_fun evaluates the logistic function. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 08 January 2020 # # Author: # # John Burkardt # # Input: # # real x, the arguments. # # Output: # # real value, the logistic function evaluated at x. # import numpy as np value = 1.0 / ( 1.0 + np.exp ( - x ) ); return value if ( __name__ == '__main__' ): study ( )