#! /usr/bin/env python3 # def logistic ( l, b, m, x ): #*****************************************************************************80 # ## logistic evaluates the sigmoid or logistic function. # # Discussion: # # The sigmoid function is useful for classification problems in # machine learning. Its value is always between 0 and l. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 11 October 2019 # # Author: # # John Burkardt # # Input: # # real l, the maximum value of the function. This is often 1. # # real b, the cutoff value, where the function equals l/2. # This is often 0. # # real m, the slope, which determines the steepness of the curve # and the width of the uncertainty interval. This is often 1. # # real x, the argument. # # Output: # # real value, the value of the logistic function at x. # import numpy as np value = l / ( 1.0 + np.exp ( - m * ( x - b ) ) ) return value def logistic_test ( l, b, m ): #*****************************************************************************80 # ## logistic_test() tests logistic(). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 11 October 2019 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'logistic_test():' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' logistic() evaluates the logistic function.' ) x = np.linspace ( b - 2.0, b + 2.0, 21 ) y = logistic ( l, b, m, x ) print ( '' ) print ( ' X Logistic(l=',l,'b=',b,'m=',m,',X)' ) print ( '' ) print ( np.c_ [ x, y ] ) def logistic_plot ( l, b, m ): #*****************************************************************************80 # ## logistic_plot() plots the logistic function. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 11 October 2019 # # Author: # # John Burkardt # # Input: # # real l, the maximum value of the function. This is often 1. # # real b, the cutoff value, where the function equals l/2. # This is often 0. # # real m, the slope, which determines the steepness of the curve # and the width of the uncertainty interval. This is often 1. # import matplotlib.pyplot as plt import numpy as np print ( '' ) print ( 'logistic_plot():' ) print ( ' Plot logistic(l=',l,',b=',b,',m=',m,').' ) # # Sample the function y(x) over [x1,x2]. # x = np.linspace ( b - 2.0, b + 2.0, 101 ) y = logistic ( l, b, m, x ) # # Plot the data. # plt.clf ( ) plt.plot ( x, y, linewidth = 3, color = 'b' ) plt.plot ( [ b-2.0, b+2.0 ], [ 0, 0 ], linewidth = 3, color = 'k' ) plt.plot ( [ b, b ], [ 0.0, l ], '--', linewidth = 3, color = 'r' ) plt.grid ( True ) plt.xlabel ( '<--- X --->', fontsize = 16 ) plt.ylabel ( '<--- Y = Logistic(l,b,m;x) --->', fontsize = 16 ) s = 'Logistic(l=' + str ( l ) + ',b=' + str ( b ) + ',m=' + str ( m ) + ')' plt.title ( s, fontsize = 16 ) filename = 'logistic_l' + str(l) + '_b' + str ( b ) + '_m' + str ( m ) + '.png' plt.savefig ( filename ) print ( ' Graphics saved as "%s"' % ( filename ) ) plt.show ( ) plt.close ( ) return if ( __name__ == '__main__' ): logistic_test ( 1.0, 2.0, 3.0 ) logistic_plot ( 1.0, 2.0, 3.0 ) logistic_plot ( 10.0, 2.0, 3.0 ) logistic_plot ( 1.0, 10.0, 3.0 ) logistic_plot ( 1.0, 2.0, 10.0 )