Problems
Loading...
1 / 1

Implement Sigmoid in NumPy

Activation Functions
Easy

Implement the sigmoid activation function:

σ(x)=11+ex\sigma(x) = \frac{1}{1 + e^{-x}}
Loading visualization...

Examples

Input: x = [0, 2, -2]

Output: [0.5, 0.88079708, 0.11920292]

Input: x = 0

Output: 0.5

Input: x = [[-1, 0], [1, 2]]

Output: [[0.26894142, 0.5], [0.73105858, 0.88079708]]

Hint 1

Convert the input into a NumPy array so that operations like -x and np.exp(-x) work correctly for scalars, lists, and arrays.

Hint 2

Use np.asarray(x, dtype=float)

Requirements

  • Must work on scalars, Python lists, and NumPy arrays
  • Must return a NumPy array of floats
  • Should be vectorized (no Python loops)

Constraints

  • Vectorized implementation only
  • Time limit: 200 ms; Memory: 64 MB
  • Allowed library: NumPy only
Try Similar Problems