🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.absolute()

AI & DATA SCIENCE // np-absolute

np.absolute() (aliased as np.abs()) computes the element-wise absolute value of an array — stripping the sign from real numbers, or computing the magnitude of complex numbers.

Syntax

np.absolute(arr)
np.abs(arr)

Deep Dive Course

For an array of real numbers, absolute() simply removes the sign from each element. For an array of complex numbers, it instead computes each element's magnitude, the distance from the origin in the complex plane, the same generalization the built-in abs() function applies to a single complex number. np.abs is just a shorter alias for the exact same function; both names are equally valid and commonly used.

1Understanding np.absolute()

For an array of real numbers, absolute() simply removes the sign from each element. For an array of complex numbers, it instead computes each element's magnitude, the distance from the origin in the complex plane, the same generalization the built-in abs() function applies to a single complex number. np.abs is just a shorter alias for the exact same function; both names are equally valid and commonly used.

💡

np.abs() and np.absolute() are the exact same function under two names — use whichever reads better in context, there's no functional difference.

editor.html
import numpy as np

arr = np.array([-3, -1, 0, 2, 5])
print(np.absolute(arr))
localhost:3000

2Practical Example

Here is a real-world application of np.absolute() showing how it is used in production NumPy code.

editor.html
import numpy as np

complex_arr = np.array([3 + 4j, 1 - 1j])
print(np.abs(complex_arr))
localhost:3000

3Best Practices

Follow these guidelines when working with np.absolute():

1. Use np.abs(a - b) to compute element-wise distance/difference between two arrays instead of a manual sign check

2. Use absolute() on complex arrays when you need magnitude, understanding the result will be a real-valued array, not complex

3. Combine np.abs() with a tolerance comparison, or np.isclose(), instead of == when checking whether array values are close to a target

⚠️

Tip: np.abs() and np.absolute() are the exact same function under two names — use whichever reads better in context, there's no functional difference.

editor.html
import numpy as np

arr = np.array([-3, -1, 0, 2, 5])
print(np.absolute(arr))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([-3, -1, 0, 2, 5])
print(np.absolute(arr))
Example 02Advanced Example
import numpy as np

complex_arr = np.array([3 + 4j, 1 - 1j])
print(np.abs(complex_arr))

Best Practices

  • Use np.abs(a - b) to compute element-wise distance/difference between two arrays instead of a manual sign check
  • Use absolute() on complex arrays when you need magnitude, understanding the result will be a real-valued array, not complex
  • Combine np.abs() with a tolerance comparison, or np.isclose(), instead of == when checking whether array values are close to a target

Interview Question

Why does np.abs() return a real-valued array when applied to an array of complex numbers?

Hint: Think about what 'absolute value' generalizes to for complex numbers.

For a complex number, absolute value generalizes to its magnitude, the distance from the origin in the complex plane, computed as the square root of the sum of the squares of its real and imaginary parts. That magnitude is always a non-negative real number by definition, regardless of how complex the original number was, which is why np.abs() applied to a complex array always produces a real-valued (float) array as its result, rather than another complex array.

Exercises

MediumPractice using np.absolute() in a real scenario.
View Solution
import numpy as np

arr = np.array([-3, -1, 0, 2, 5])
print(np.absolute(arr))

Frequently Asked Questions

Why does np.abs() return a real-valued array when applied to an array of complex numbers?

For a complex number, absolute value generalizes to its magnitude, the distance from the origin in the complex plane, computed as the square root of the sum of the squares of its real and imaginary parts. That magnitude is always a non-negative real number by definition, regardless of how complex the original number was, which is why np.abs() applied to a complex array always produces a real-valued (float) array as its result, rather than another complex array.

Related Functions

np-sqrtabs()np-clip