Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

NumPy abs: np.abs() vs np.absolute() and np.fabs()

How np.abs works: it equals np.absolute, returns magnitude for complex input, supports out and where, and differs from np.fabs.

Written and maintained by CASRAI Editorial Board

Last updated

np.abs() returns the absolute value of every element of a NumPy array, element by element, and keeps the array’s shape. It is the shorthand for np.absolute(): the two names point to the same function. For a complex number a + ib it returns the magnitude sqrt(a² + b²) instead of a sign flip, which is the detail most tutorials skip, along with the out and where arguments covered below.

What it is used for in research

Absolute values appear wherever the sign of a difference does not matter, only its size. Typical research uses are the absolute error between observed and predicted values, np.abs(y - y_hat); the size of residuals when you inspect model fit; and the magnitude of complex output such as the amplitude spectrum from a Fourier transform (see the NumPy Fourier transform guide). A mean of absolute deviations is the basis of the measure described in mean absolute deviation, and the sum of absolute values is the L1 norm covered in numpy.linalg.norm.

Install or access

NumPy is installed from the official project. In a virtual environment, create and activate it, then install the package with pip:

  1. python -m venv my-env
  2. Activate it: source my-env/bin/activate on macOS or Linux, my-env\Scripts\activate on Windows.
  3. pip install numpy

With conda, the official install page gives conda create -n my-env, conda activate my-env and conda install numpy. Then import it and confirm the installed version:

import numpy as np
print(np.__version__)

Step-by-step: take absolute values of an array

  1. Basic call. Pass an array, a list or a scalar. The result has the same shape as the input.
    x = np.array([-1.2, 1.2])
    np.abs(x)        # array([1.2, 1.2])
    np.absolute(x)   # same function, same result
    abs(x)           # Python built-in on an ndarray, same result
  2. Complex input. For a + ib the result is the magnitude sqrt(a² + b²), returned as a real number.
    np.abs(1.2 + 1j)                  # 1.5620499351813308
    np.abs(np.array([3+4j, -1-1j]))   # array([5.        , 1.41421356])
  3. Write into an existing array with out. The documentation says out must have a shape the inputs broadcast to; if it is not given, a new array is allocated.
    a = np.array([-2.0, 5.0, -7.0])
    res = np.empty_like(a)
    np.abs(a, out=res)     # res is now [2., 5., 7.]
  4. Apply it to selected elements with where. Where the condition is True the result is stored; elsewhere out keeps its original value, so out must be initialised first. The documentation adds that if out is left as the default, the locations where the condition is False remain uninitialized.
    out = np.full(3, -1.0)
    np.abs(a, out=out, where=np.array([True, False, True]))
    out    # array([ 2., -1.,  7.])
  5. Absolute error for a research comparison.
    y     = np.array([2.0, 3.5, 1.0])
    y_hat = np.array([2.5, 3.0, 1.8])
    err = np.abs(y - y_hat)   # array([0.5, 0.5, 0.8])
    err.mean()                # 0.6

Outputs shown in these examples come from NumPy 2.5.1. To find where the largest absolute value sits, wrap the call as np.argmax(np.abs(x)); see the NumPy argmax guide.

Output and how to read it

The documentation describes the return value as an ndarray holding the absolute value of each element of x, or a scalar if x is a scalar. For real input, the output has the same shape and values with signs removed. For complex input the output is the magnitude, so a complex array becomes a real-valued array and the phase is discarded. If you need the phase as well, compute it separately; abs alone cannot recover it.

Troubleshooting

Symptom What to check
Complex array gives a real array This is expected: for a + ib the function returns sqrt(a² + b²). Use np.angle or keep the original array if you need the phase.
np.fabs fails or behaves differently on complex data The numpy.fabs documentation states that complex values are not handled and to use absolute for complex data.
Result is not the dtype you expected np.fabs always returns floats. np.abs and np.absolute do not convert integer input to float: an int64 array stayed int64 when tested with NumPy 2.5.1.
Wrong or negative-looking values from integer arrays The NumPy data type documentation warns that fixed-size numeric types can overflow when a value needs more memory than the type provides. Check the limits of your dtype with np.iinfo(your_dtype) (or np.finfo for floats) before relying on the result at the extremes.
where leaves unexpected values Elements where the condition is False keep whatever out already held. Initialise out deliberately, as in step 4.
NaN input NaN stays NaN: np.abs(np.array([1, np.nan, -2])) returned array([ 1., nan, 2.]) when tested with NumPy 2.5.1.

Free alternatives

NumPy itself is free and open source; every option below ships with NumPy or Python. The rows below follow the NumPy reference pages for numpy.absolute and numpy.fabs.

Option Handles complex Output type Notes
np.abs(x) Yes Keeps the input’s numeric kind Shorthand for np.absolute.
np.absolute(x) Yes (a + ib gives sqrt(a² + b²)) Keeps the input’s numeric kind The full ufunc name; accepts out, where, dtype.
Built-in abs(x) on an ndarray Yes Same as np.absolute The documentation says it can be used as shorthand on ndarrays; it does not take out or where.
np.fabs(x) No Always float Use only for real data when you want a float result.

FAQ

Is np.abs the same as np.absolute?

Yes. The NumPy documentation states that np.abs is a shorthand for np.absolute, so they give identical results.

What does np.abs do with complex numbers?

It returns the magnitude. For a + ib the absolute value is sqrt(a² + b²); for example np.abs(1.2 + 1j) is 1.5620499351813308.

When should I use np.fabs instead of np.abs?

Use np.fabs when the data are real and you want a float result in every case. It does not handle complex values, so use np.absolute for those.

Can I take absolute values of only some elements?

Yes, with the where argument together with an initialised out array; the other elements keep their original value in out.

Does np.abs change the shape of my array?

No. It works element by element, so the output has the same shape as the input (or a scalar for a scalar input).

Sources

Primary sources, checked on 2026-10-29.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about NumPy abs: np.abs() vs np.absolute() and np.fabs()

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →