#! /usr/bin/env python3 # from sklearn import * def gold ( ): #*****************************************************************************80 # ## gold 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 ( 'gold:' ) print ( ' python version: %s' % ( platform.python_version ( ) ) ) print ( ' scikit-learn version is %s' % ( sklearn.__version__ ) ) print ( ' Use sklearn LogisticRegression() on the gold coin example.' ) print ( '' ) data = np.loadtxt ( 'gold_data.txt' ) x = data[:,0] y = data[:,1] m = len ( x ) xx = np.reshape ( x, [m,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 ) # # Plot the results. # plt.scatter ( x, y ) x_eval = np.linspace ( 28, 38, 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 ( 'gold logistic regression example' ) plt.grid ( True ) filename = 'gold.png' plt.savefig ( filename ) print ( '' ) print ( ' Graphics saved as "%s"' % ( filename ) ) plt.show ( ) # # Terminate. # print ( '' ) print ( 'gold' ) 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__' ): gold ( )