PIXELBANKv8.2.1
Menu

Sigmoid Function

Implement the sigmoid (logistic) function that maps any real number to the range (0,1)(0, 1).

σ(z)=11+ez\sigma(z) = \frac{1}{1 + e^{-z}}

Given a list of values, apply the sigmoid function to each and return the results.

To avoid overflow for large negative values, use the identity: for z<0z < 0, compute σ(z)=ez1+ez\sigma(z) = \frac{e^z}{1 + e^z}.

Round each result to 4 decimal places.

Example:

Input:
z = [0, 2, -2]
Output:
[0.5, 0.8808, 0.1192]
Reasoning:
  • For each value zz in the input list, apply the sigmoid function: if z0z \geq 0, compute σ(z)=11+ez\sigma(z) = \frac{1}{1 + e^{-z}}, otherwise compute σ(z)=ez1+ez\sigma(z) = \frac{e^z}{1 + e^z} to avoid overflow.
  • Calculate the sigmoid for each input value:
    • For z=0z = 0, σ(0)=11+e0=11+1=0.5\sigma(0) = \frac{1}{1 + e^{0}} = \frac{1}{1 + 1} = 0.5
    • For z=2z = 2, σ(2)=11+e20.8808\sigma(2) = \frac{1}{1 + e^{-2}} \approx 0.8808
    • For z=2z = -2, σ(2)=e21+e20.1192\sigma(-2) = \frac{e^{-2}}{1 + e^{-2}} \approx 0.1192
  • Round each result to 4 decimal places.
  • The final output is [0.5,0.8808,0.1192][0.5, 0.8808, 0.1192]

Constraints:

  • Input is a list of floats (can be negative, zero, or positive)
  • Return a list of sigmoid values, each rounded to 4 decimal places
  • Handle large positive/negative inputs without overflow
Editor

Test Results

0/0
Run code to see test results.