Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.sign

Returns the sign of each input element. Real values become -1, 0, or 1; complex values are divided by their magnitude, and NaN inputs remain NaN.

How it works

As a universal function, np.sign processes each input element independently. Real values map to -1, 0, or 1 according to whether they are negative, zero, or positive; complex values are normalized by their magnitude, while NaN inputs produce NaN.

Syntax

numpy.sign(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True [, signature])

Parameters

NameTypeDescription
xarray_likeInput value or array whose signs are evaluated.
outndarray or NoneOptional output array receiving the result.
wherearray_like of boolCondition selecting where computation occurs.
casting{‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optionalCasting rule applied to inputs.
order{‘K’, ‘A’, ‘C’, ‘F’}, optionalMemory layout order for the output.
dtypedtype, optionalOutput data type.
subokbool, optionalWhether subclasses are passed through.
signaturestr or tuple of ints, optionalOptional generalized-ufunc signature.

Returns

An ndarray containing the sign of x, or a scalar when x is scalar.

Key points

  • The function applies sign classification independently to every element.
  • Real negative, zero, and positive values map to -1, 0, and 1 respectively.
  • NaN input elements result in NaN output elements.
  • Complex values are normalized by their absolute magnitude.
  • The output shape follows the input: scalar in, scalar out; array in, array out.

Common mistakes

  • For complex inputs, the result is not merely -1, 0, or 1: it is the complex value divided by its magnitude.
  • A scalar input produces a scalar result; array-like input produces an ndarray.

Example: Signs of real and complex values

import numpy as np

x = np.array([-5.0, 0.0, 4.5, 3 - 4j, 8j])
print(np.sign(x))
# [-1. +0.j  0. +0.j  1. +0.j  0.6-0.8j  0. +1.j ]

Computes the sign of real and complex values element by element.

When to use it

Classify real values by sign

Convert numeric arrays into directional indicators for thresholding or categorical logic.

Normalize complex values

Normalize complex values to unit magnitude while preserving their phase.

Preserve NaN markers

Propagate undefined numeric inputs through sign-based calculations.

Interview questions

QWhat does np.sign return for real values?

np.sign is an element-wise universal function. For real x, it returns -1 when x is below zero, 0 when x equals zero, and 1 when x is above zero.

Follow-up: How does it handle complex numbers? It returns x divided by abs(x), with zero remaining zero.

QHow does np.sign handle NaN?

NaN inputs produce NaN. This preserves the undefined sign of NaN rather than classifying it as positive, negative, or zero.

Follow-up: What happens for a scalar NaN? The result is a scalar NaN because scalar inputs yield scalar outputs.

QHow does np.sign behave for complex inputs?

For complex inputs, np.sign returns the input normalized by its magnitude, x / abs(x), and returns zero when x is zero. For example, 3-4j becomes 0.6-0.8j.

Follow-up: Why does 8j become 1j? Its magnitude is 8, so dividing 8j by 8 yields 1j.

  • numpy.absolute
  • numpy.copysign
  • numpy.heaviside
  • numpy.signbit

Source: numpy.sign. Checked 2026-10-15.