If you want the absolute element-wise difference between both matrices, you can easily subtract them with NumPy and use numpy.absolute on the resulting matrix.

import numpy as np

X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]

Y = [[5,8,1],
[6,7,3],
[4,5,9]]

result = np.absolute(np.array(X) - np.array(Y))

Outputs:

[[7 1 2]
 [2 2 3]
 [3 3 0]]

Alternatively (although unnecessary), if you were required to do so in native Python you could zip the dimensions together in a nested list comprehension.

result = [[abs(a-b) for a, b in zip(xrow, yrow)]
          for xrow, yrow in zip(X,Y)]

Outputs:

[[7, 1, 2], [2, 2, 3], [3, 3, 0]]
Answer from miradulo on Stack Overflow
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.absolute.html
numpy.absolute — NumPy v2.1 Manual
An ndarray containing the absolute value of each element in x. For complex input, a + ib, the absolute value is \(\sqrt{ a^2 + b^2 }\). This is a scalar if x is a scalar. ... >>> import numpy as np >>> x = np.array([-1.2, 1.2]) >>> np.absolute(x) array([ 1.2, 1.2]) >>> np.absolute(1.2 + 1j) ...
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Calculate the absolute value element-wise (np.abs, np.fabs) | note.nkmk.me
January 14, 2024 - You can calculate the absolute value element-wise in a NumPy array (ndarray) using np.abs(), np.absolute(), or np.fabs(). Note that np.abs() is simply an alias for np.absolute(). Additionally, the bui ...
🌐
Codecademy
codecademy.com › docs › python:numpy › math methods › .abs()
Python:NumPy | Math Methods | .abs() | Codecademy
June 12, 2025 - NumPy’s .abs() function is widely used in data science applications for cleaning datasets with negative values, calculating distances in machine learning algorithms, processing signal data, financial analysis for computing returns and losses, and scientific computing where absolute differences are required.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.absolute.html
numpy.absolute — NumPy v2.4 Manual
An ndarray containing the absolute value of each element in x. For complex input, a + ib, the absolute value is \(\sqrt{ a^2 + b^2 }\). This is a scalar if x is a scalar. Examples · Try it in your browser! >>> import numpy as np >>> x = np.array([-1.2, 1.2]) >>> np.absolute(x) array([ 1.2, ...
🌐
Python Guides
pythonguides.com › python-numpy-absolute-value
NumPy's Np.abs() Function In Python
May 16, 2025 - import numpy as np # User's location (latitude, longitude) user_location = np.array([37.7749, -122.4194]) # San Francisco # Points of interest (latitude, longitude) poi_locations = np.array([ [37.8044, -122.2711], # Oakland [37.3382, -121.8863], # San Jose [38.5816, -121.4944] # Sacramento ]) # Calculate the absolute differences abs_diff = np.abs(poi_locations - user_location) # Simple distance metric (not actual geographic distance) distances = np.sum(abs_diff, axis=1) print(distances) # Output: [0.3792 0.9698 1.3817]
Top answer
1 of 3
106

It's likely because there a built-in functions with the same name, abs. The same is true for np.amax, np.amin and np.round_.

The aliases for the NumPy functions abs, min, max and round are only defined in the top-level package.

So np.abs and np.absolute are completely identical. It doesn't matter which one you use.

There are several advantages to the short names: They are shorter and they are known to Python programmers because the names are identical to the built-in Python functions. So end-users have it easier (less to type, less to remember).

But there are reasons to have different names too: NumPy (or more generally 3rd party packages) sometimes need the Python functions abs, min, etc. So inside the package they define functions with a different name so you can still access the Python functions - and just in the top-level of the package you expose the "shortcuts". Note: Different names are not the only available option in that case: One could work around that with the Python module builtins to access the built-in functions if one shadowed a built-in name.

It might also be the case (but that's pure speculation on my part) that they originally only included the long-named functions absolute (and so on) and only added the short aliases later. Being a large and well-used library the NumPy developers don't remove or deprecate stuff lightly. So they may just keep the long names around because it could break old code/scripts if they would remove them.

2 of 3
23

There also is Python's built-in abs(), but really all those functions are doing the same thing. They're even exactly equally fast! (This is not the case for other functions, like max().)

Code to reproduce the plot:

import numpy as np
import perfplot


def np_absolute(x):
    return np.absolute(x)


def np_abs(x):
    return np.abs(x)


def builtin_abs(x):
    return abs(x)


b = perfplot.bench(
    setup=np.random.rand,
    kernels=[np_abs, np_absolute, builtin_abs],
    n_range=[2 ** k for k in range(25)],
    xlabel="len(data)",
)
b.save("out.png")
b.show()
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › absolute
Python Numpy absolute() - Calculate Absolute Value | Vultr Docs
November 6, 2024 - The numpy.absolute() function in Python, part of the NumPy library, is designed to calculate the absolute values of the elements in arrays.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.allclose.html
numpy.allclose — NumPy v2.1 Manual
The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(b)) and the absolute difference atol are added together to compare against the absolute difference between a and b.
Find elsewhere
🌐
Sharp Sight
sharpsight.ai › blog › numpy-absolute-value
Numpy Absolute Value, Explained - Sharp Sight
July 24, 2021 - Here, we’ll compute the absolute values of an array of values. To do this, we first need to create an array, and then we can use np.abs() on that array. First, we’ll create a 1-dimensional array that contains the values from -2 to 2. To do this, we’ll use the Numpy arange function.
🌐
Programiz
programiz.com › python-programming › numpy › methods › absolute
NumPy absolute() (With Examples)
The absolute value of -1 is 1, 2 is 2, -3.5 is 3.5 and so on. import numpy as np # create an array array1 = np.array([-12, 23, -25, -41, -52]) # create an empty array with the same shape as array1 result = np.zeros_like(array1)
🌐
GeeksforGeeks
geeksforgeeks.org › numpy-absolute-python
numpy.absolute() in Python - GeeksforGeeks
November 29, 2018 - numpy.allclose() function is used to find if two arrays are element-wise equal within a tolerance. The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(arr2)) and the absolute difference atol are added together to compare against the absolute differenc
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.isclose.html
numpy.isclose — NumPy v2.4 Manual
The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(b)) and the absolute difference atol are added together to compare against the absolute difference between a and b.
🌐
GeeksforGeeks
geeksforgeeks.org › machine learning › absolute-deviation-and-absolute-mean-deviation-using-numpy-python
Absolute Deviation and Absolute Mean Deviation using NumPy | Python - GeeksforGeeks
November 13, 2025 - Absolute Mean Deviation is the average of all absolute deviations in a dataset. It summarizes the overall spread with a single representative value. A higher Absolute Mean Deviation indicates greater variability, while a lower value shows more ...
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.isclose.html
numpy.isclose — NumPy v2.5.dev0 Manual
The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(b)) and the absolute difference atol are added together to compare against the absolute difference between a and b.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-calculate-the-element-wise-absolute-value-of-numpy-array
How to calculate the element-wise absolute value of NumPy array? | GeeksforGeeks
August 29, 2020 - We just have to get the sum of corresponding array elements and then divide that sum with the total number of arrays. Let's see an example: Example 1: Calculate average values of two given NumPy 1d-arrays Python3 #
🌐
Medium
mr-amit.medium.com › what-is-numpy-absolute-and-when-to-use-it-0a2c48189200
What is numpy.absolute() and When to Use It? | by It's Amit | Medium
March 6, 2025 - Okay, jokes aside, here’s the deal: numpy.absolute() is your go-to method for finding the absolute value of numbers in an array.
Top answer
1 of 4
2

The answer by Derek Roberts with numpy.minimum is almost correct. However, since the input values can be greater than 100, they should first be rescaled to 0-100 with %100 (mod). I'm adding an extra pair of values to demonstrate this:

a = np.array([101,105,90,102,90,10,50,1001])
b = np.array([99,110,85,110,85,90,60,2])

x = abs(a%100-b%100)
np.minimum(x, 100-x)

Generic computation:

M = 100
x = abs(a%M-b%M)
np.minimum(x, M-x)

Output:

array([ 2,  5,  5,  8,  5, 20, 10,  1])
2 of 4
1

This is a classic example of what Modulus Arithmetic is used for Let us consider an example

Morris provides everything you need to know about modulus here

My source: maths

"Definition Let m > 0 be a positive integer called the modulus. We say that two integers a and b are congruent modulo m if b − a is divisible by m. In other words, a ≡ b(modm) ⇐⇒ a − b = m · k for some integers "

Let us take 10 and 90 for example:

In other words, moving directly from 10 to 90 gives a difference of 80, but if you consider the wrap-around (going backward from 10 to 0 and then forward from 100 to 90), you get a smaller difference of 20.

I have provide a code which helps

For example:

import numpy as np

a = np.array([101, 105, 90, 102, 90, 10, 50])
b = np.array([99, 110, 85, 110, 85, 90, 60])

# Start by computing absolute difference
absolute_of_two_nos = np.abs(a % 100 - b % 100)

# Then consider the difference- wrap-around using modulus of 100
diff_mod_using_100 = 100 - np.abs(a % 100 - b % 100)

# Take the minimum of the direct diff and the modulus diff
result = np.minimum(absolute_of_two_nos, diff_mod_using_100)

print(result)
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-math-exercise-7.php
NumPy: Calculate the absolute value element-wise - w3resource
August 29, 2025 - # Importing the NumPy library import numpy as np # Creating an array with floating-point numbers x = np.array([-10.2, 122.2, .20]) # Displaying the original array print("Original array:") print(x) # Calculating the element-wise absolute value and displaying the result print("Element-wise absolute value:") print(np.absolute(x))
🌐
SciPy
docs.scipy.org › doc › numpy-1.11.0 › reference › generated › numpy.absolute.html
numpy.absolute — NumPy v1.11 Manual
May 29, 2016 - numpy.absolute(x[, out]) = <ufunc 'absolute'>¶ · Calculate the absolute value element-wise. Examples · >>> x = np.array([-1.2, 1.2]) >>> np.absolute(x) array([ 1.2, 1.2]) >>> np.absolute(1.2 + 1j) 1.5620499351813308 · Plot the function over [-10, 10]: >>> import matplotlib.pyplot as plt ·