📘
Sigmoid Function
EasyClassification
Implement the sigmoid (logistic) function that maps any real number to the range (0,1).
σ(z)=1+e−z1
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<0, compute σ(z)=1+ezez.
Round each result to 4 decimal places.
Example:
Input:
z = [0, 2, -2]
Output:
[0.5, 0.8808, 0.1192]
Reasoning:
- For each value z in the input list, apply the sigmoid function: if z≥0, compute σ(z)=1+e−z1, otherwise compute σ(z)=1+ezez to avoid overflow.
- Calculate the sigmoid for each input value:
- For z=0, σ(0)=1+e01=1+11=0.5
- For z=2, σ(2)=1+e−21≈0.8808
- For z=−2, σ(−2)=1+e−2e−2≈0.1192
- Round each result to 4 decimal places.
- The final output is [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
Python 3.13.1
Test Results
0/0Run code to see test results.