How to calculate a logistic sigmoid function in Python?

后端 未结 15 1671
情话喂你
情话喂你 2020-11-29 16:14

This is a logistic sigmoid function:

\"enter

I know x. How can I calculate F(x

相关标签:
15条回答
  • 2020-11-29 16:52

    I feel many might be interested in free parameters to alter the shape of the sigmoid function. Second for many applications you want to use a mirrored sigmoid function. Third you might want to do a simple normalization for example the output values are between 0 and 1.

    Try:

    def normalized_sigmoid_fkt(a, b, x):
       '''
       Returns array of a horizontal mirrored normalized sigmoid function
       output between 0 and 1
       Function parameters a = center; b = width
       '''
       s= 1/(1+np.exp(b*(x-a)))
       return 1*(s-min(s))/(max(s)-min(s)) # normalize function to 0-1
    

    And to draw and compare:

    def draw_function_on_2x2_grid(x): 
        fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2)
        plt.subplots_adjust(wspace=.5)
        plt.subplots_adjust(hspace=.5)
    
        ax1.plot(x, normalized_sigmoid_fkt( .5, 18, x))
        ax1.set_title('1')
    
        ax2.plot(x, normalized_sigmoid_fkt(0.518, 10.549, x))
        ax2.set_title('2')
    
        ax3.plot(x, normalized_sigmoid_fkt( .7, 11, x))
        ax3.set_title('3')
    
        ax4.plot(x, normalized_sigmoid_fkt( .2, 14, x))
        ax4.set_title('4')
        plt.suptitle('Different normalized (sigmoid) function',size=10 )
    
        return fig
    

    Finally:

    x = np.linspace(0,1,100)
    Travel_function = draw_function_on_2x2_grid(x)
    

    0 讨论(0)
  • 2020-11-29 16:54

    This should do it:

    import math
    
    def sigmoid(x):
      return 1 / (1 + math.exp(-x))
    

    And now you can test it by calling:

    >>> sigmoid(0.458)
    0.61253961344091512
    

    Update: Note that the above was mainly intended as a straight one-to-one translation of the given expression into Python code. It is not tested or known to be a numerically sound implementation. If you know you need a very robust implementation, I'm sure there are others where people have actually given this problem some thought.

    0 讨论(0)
  • 2020-11-29 16:54

    Use the numpy package to allow your sigmoid function to parse vectors.

    In conformity with Deeplearning, I use the following code:

    import numpy as np
    def sigmoid(x):
        s = 1/(1+np.exp(-x))
        return s
    
    0 讨论(0)
  • 2020-11-29 16:54

    A one liner...

    In[1]: import numpy as np
    
    In[2]: sigmoid=lambda x: 1 / (1 + np.exp(-x))
    
    In[3]: sigmoid(3)
    Out[3]: 0.9525741268224334
    
    0 讨论(0)
  • 2020-11-29 16:54

    Vectorized method when using pandas DataFrame/Series or numpy array:

    The top answers are optimized methods for single point calculation, but when you want to apply these methods to a pandas series or numpy array, it requires apply, which is basically for loop in the background and will iterate over every row and apply the method. This is quite inefficient.

    To speed up our code, we can make use of vectorization and numpy broadcasting:

    x = np.arange(-5,5)
    np.divide(1, 1+np.exp(-x))
    
    0    0.006693
    1    0.017986
    2    0.047426
    3    0.119203
    4    0.268941
    5    0.500000
    6    0.731059
    7    0.880797
    8    0.952574
    9    0.982014
    dtype: float64
    

    Or with a pandas Series:

    x = pd.Series(np.arange(-5,5))
    np.divide(1, 1+np.exp(-x))
    
    0 讨论(0)
  • 2020-11-29 16:55

    you can calculate it as :

    import math
    def sigmoid(x):
      return 1 / (1 + math.exp(-x))
    

    or conceptual, deeper and without any imports:

    def sigmoid(x):
      return 1 / (1 + 2.718281828 ** -x)
    

    or you can use numpy for matrices:

    import numpy as np #make sure numpy is already installed
    def sigmoid(x):
      return 1 / (1 + np.exp(-x))
    
    0 讨论(0)
提交回复
热议问题