How to calculate a logistic sigmoid function in Python?

后端 未结 15 1903
情话喂你
情话喂你 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: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))
    

提交回复
热议问题