Vectorized Sigmoid Function in NumPy

Quick Overview

Implement an elementwise sigmoid in NumPy that returns a new float64 array of the same shape for any numeric input without changing the caller's array. Tests vectorized NumPy, dtype handling for integer inputs, exact behavior at zero, and overflow for large-magnitude scores.

Vectorized Sigmoid Function in NumPy

Role: Data Scientist

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `sigmoid(z)`: given a 1-D NumPy array `z` of real-valued scores, return a new `np.ndarray` where element `i` equals `1 / (1 + exp(-z[i]))`. Requirements: - Return a new array of the same shape as `z` with dtype `float64`, even for integer input (for example `np.array([-2, -1, 0, 1, 2])`). - Do not mutate the input. - Compute it with vectorized NumPy operations, not a Python loop. - A score of 0 must map to exactly 0.5. Example: ```python z = np.array([-2.0, -1.0, 0.0, 1.0, 2.0]) sigmoid(z) # array([0.11920292, 0.26894142, 0.5, 0.73105858, 0.88079708]) ``` ```hint Where the dtype is set Decide at which point the input becomes `float64`, so that every later operation, including the negation, already works on floating-point values. ``` ### Constraints & Assumptions - `z` is a 1-D NumPy array of any real numeric dtype, integers included. - The output has the same shape as `z` and dtype `float64`. - The caller keeps using `z` afterward, so it must come back unchanged. ### What a Strong Answer Covers - An elementwise vectorized expression that preserves the shape. - How the `float64` output is guaranteed for integer and lower-precision inputs. - How the function avoids modifying the caller's array. - Why a score of 0 gives exactly 0.5. - What happens for large-magnitude scores. ### Follow-up Questions - How would you keep `sigmoid` numerically stable for large-magnitude inputs? - Why does `np.where(z >= 0, 1 / (1 + np.exp(-z)), np.exp(z) / (1 + np.exp(z)))` still emit overflow warnings? - How is the derivative of the sigmoid written in terms of its output, and where does that show up when training a logistic regression?

Overview: Implement an elementwise sigmoid in NumPy that returns a new float64 array of the same shape for any numeric input without changing the caller's array. Tests vectorized NumPy, dtype handling for integer inputs, exact behavior at zero, and overflow for large-magnitude scores.

General logo
General
Nov 24, 2026
easyData ScientistOtherMachine Learning
0
0

Implement sigmoid(z): given a 1-D NumPy array z of real-valued scores, return a new np.ndarray where element i equals 1 / (1 + exp(-z[i])).

Requirements:

  • Return a new array of the same shape as z with dtype float64 , even for integer input (for example np.array([-2, -1, 0, 1, 2]) ).
  • Do not mutate the input.
  • Compute it with vectorized NumPy operations, not a Python loop.
  • A score of 0 must map to exactly 0.5.

Example:

z = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
sigmoid(z)
# array([0.11920292, 0.26894142, 0.5, 0.73105858, 0.88079708])

Constraints & Assumptions

  • z is a 1-D NumPy array of any real numeric dtype, integers included.
  • The output has the same shape as z and dtype float64 .
  • The caller keeps using z afterward, so it must come back unchanged.

What a Strong Answer Covers Guidance

  • An elementwise vectorized expression that preserves the shape.
  • How the float64 output is guaranteed for integer and lower-precision inputs.
  • How the function avoids modifying the caller's array.
  • Why a score of 0 gives exactly 0.5.
  • What happens for large-magnitude scores.

Follow-up Questions Guidance

  • How would you keep sigmoid numerically stable for large-magnitude inputs?
  • Why does np.where(z >= 0, 1 / (1 + np.exp(-z)), np.exp(z) / (1 + np.exp(z))) still emit overflow warnings?
  • How is the derivative of the sigmoid written in terms of its output, and where does that show up when training a logistic regression?