#! /usr/bin/env python3 # def logistic ( l, b, m, x ): #*****************************************************************************80 # ## logistic evaluates the sigmoid or logistic function. # # Discussion: # # An R8 is a double precision real value. # # The sigmoid function is useful for classification problems in # machine learning. Its value is always between 0 and 1. # # 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. # import numpy as np value = l / ( 1.0 + np.exp ( - m * ( x - b ) ) ) return value def logistic_test ( ): #*****************************************************************************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.' ) l = 1.0 b = 2.0 m = 3.0 x_test = np.linspace ( -2.0, 8.0, 21 ) print ( '' ) print ( ' X logistic(',l,',',b,',',m,',x)' ) print ( '' ) for i in range ( 0, 21 ): x = x_test[i] value = logistic ( l, b, m, x ) print ( ' %10.6g %10.6g' % ( x, value ) ) # # Terminate. # print ( '' ) print ( 'logistic_test' ) print ( ' Normal end of execution.' ) return def logistic_plot ( l, b, m, x1, x2 ): #*****************************************************************************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. # # real x1, x2, the x range of the plot. # import matplotlib.pyplot as plt import numpy as np print ( '' ) print ( 'logistic_plot:' ) print ( ' Plot the function logistic(',l,',',b,',',m,').' ) # # Read the pairs "Year, Population" from the file. # x = np.linspace ( x1, x2, 101 ) y = logistic ( l, b, m, x ) # # Plot the data. # plt.plot ( x, y, linewidth = 3, color = 'b' ) plt.plot ( [ x1, x2 ], [ 0, 0 ], linewidth = 3, color = 'k' ) plt.plot ( [ b, b ], [ 0.0, 1.0 ], '--', linewidth = 3, color = 'r' ) plt.grid ( True ) plt.xlabel ( '<--- X --->', fontsize = 16 ) plt.ylabel ( '<--- Logistic(l,b,m) --->', fontsize = 16 ) s = 'Logistic(' + str ( l ) + ',' + str ( b ) + ',' + str ( m ) + ')' plt.title ( s, fontsize = 16 ) filename = 'logistic_plot.png' plt.savefig ( filename ) print ( '' ) print ( ' Graphics saved as "%s"' % ( filename ) ) plt.show ( ) plt.clf ( ) # # Terminate. # print ( '' ) print ( 'logistic_plot' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): logistic_test ( ) logistic_plot ( 1.0, 2.0, 3.0, -2.0, 8.0 )