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 › stable › reference › generated › numpy.absolute.html
numpy.absolute — NumPy v2.4 Manual
>>> import numpy as np >>> x = np.array([-1.2, 1.2]) >>> np.absolute(x) array([ 1.2, 1.2]) >>> np.absolute(1.2 + 1j) 1.5620499351813308
🌐
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 ... -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) ...
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.absolute.html
numpy.absolute — NumPy v2.1 Manual
>>> import numpy as np >>> x = np.array([-1.2, 1.2]) >>> np.absolute(x) array([ 1.2, 1.2]) >>> np.absolute(1.2 + 1j) 1.5620499351813308
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()
🌐
Programiz
programiz.com › python-programming › numpy › methods › absolute
NumPy absolute() (With Examples)
The absolute() function is used to compute the absolute value of each element in an array. The absolute() function is used to compute the absolute value of each element in an array. Example import numpy as np array1 = np.array([-1, -2, 3, -4, 5]) # use absolute() to find absolute values of ...
🌐
Vultr Docs
docs.vultr.com › python › third-party › numpy › absolute
Python Numpy absolute() - Calculate Absolute Value | Vultr Docs
November 6, 2024 - For instance, 3+4j has a magnitude calculated as sqrt(3^2 + 4^2). The numpy.absolute() function is a versatile tool for computing the absolute values of elements within numpy arrays, including handling complex data types.
🌐
Codecademy
codecademy.com › docs › python:numpy › math methods › .abs()
Python:NumPy | Math Methods | .abs() | Codecademy
June 12, 2025 - An array containing the absolute value of each element in the input. For complex numbers, returns the magnitude calculated as √(real² + imaginary²). This example demonstrates the fundamental usage of numpy.abs() with different numeric data types:
🌐
GeeksforGeeks
geeksforgeeks.org › absolute-deviation-and-absolute-mean-deviation-using-numpy-python
Absolute Deviation and Absolute Mean Deviation using NumPy | Python | GeeksforGeeks
July 31, 2024 - Code #1 : Python3 1== # Python ... non-negative value of x without regard to its sign. For example absolute value of 7 is 7 and the absolute value of -7 is also 7. Deviation: Deviation is a measure of the difference between the ...
Find elsewhere
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))
🌐
Sharp Sight
sharpsight.ai › blog › numpy-absolute-value
Numpy Absolute Value, Explained - Sharp Sight
July 24, 2021 - This tutorial will show you how to use the Numpy absolute value function. You'll learn how np.abs works and we'll show you clear examples.
🌐
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 - Passing an ndarray to np.abs() returns a new ndarray, with each element converted to its absolute value. a = np.array([-2, -1, 0, 1, 2]) print(np.abs(a)) # [2 1 0 1 2] print(type(np.abs(a))) # <class 'numpy.ndarray'>
🌐
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 - Syntax: numpy.absolute(arr, out = None, ufunc ‘absolute’) Return: An array with absolute value of each element. ... Example 1: Element-wise absolute value of 1d-array.
🌐
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.
Top answer
1 of 4
16

Here's one approach that is significantly faster than V2: take img1-img2, and multiply by 1 or -1 depending on img1>img2. Here's how it is implemented:

Copydef differenceImageV6(img1, img2):
  a = img1-img2
  b = np.uint8(img1<img2) * 254 + 1
  return a * b

A test harness for testing performance:

Copyimport numpy as np

img1=np.uint8(np.random.randint(0, 255, (480, 640)))
img2=np.uint8(np.random.randint(0, 255, (480, 640)))

def differenceImageV1(img1, img2):
  diff=np.empty_like(img1)
  h, w=img1.shape
  for y in range(h):
    for x in range(w):
      if img1[y, x]<img2[y, x]: diff[y, x]=img2[y, x]-img1[y, x]
      else:                     diff[y, x]=img1[y, x]-img2[y, x]
  return(diff)

def differenceImageV2(img1, img2):
  return(np.uint8(np.abs(np.int16(img1)-img2)))

def differenceImageV3(img1, img2):  # fast - but wrong result
  return(img1-img2)

def differenceImageV4(img1, img2):
  return np.where(img1>img2, img1-img2, img2-img1)

def differenceImageV5(img1, img2):
  a = img1-img2
  b = img2-img1
  c = img1>img2
  return a*c + b*(~c)

def differenceImageV6(img1, img2):
  a = img1-img2
  b = np.uint8(img1<img2) * 254 + 1
  return a * b

import timeit
def testit():
  for fn in [differenceImageV2, differenceImageV3, differenceImageV4, differenceImageV5, differenceImageV6]:
    print fn.__name__, np.sum(fn(img1, img2).astype('int64')),
    print timeit.timeit("%s(img1, img2)" % fn.__name__, "from test import img1, img2, %s" % fn.__name__, number=1000)

if __name__ == '__main__':
    testit()

and resulting performance numbers:

CopydifferenceImageV2 26071358 0.982538938522
differenceImageV3 39207702 0.0261280536652
differenceImageV4 26071358 1.36270809174
differenceImageV5 26071358 0.220561981201
differenceImageV6 26071358 0.154536962509

differenceImageV6 is about 6x slower than the incorrect differenceImageV3, but still about 6x faster than the previous best differenceImageV2. differenceImageV1 isn't tested because it's easily a few orders of magnitude slower than the rest.

Note: I included an np.where approach for comparison; I thought it might have good performance but it turns out to be fairly poor. It seems that performing slicing by a boolean array is quite slow in NumPy.

2 of 4
11

If you have opencv available, you can also use:

Copydef differenceImageV4(img1, img2):
  return cv2.absdiff(img1, img2)

which is almost the same speed as differenceImageV3.

🌐
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
🌐
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 ·
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.absolute.html
numpy.absolute — NumPy v2.0 Manual
>>> x = np.array([-1.2, 1.2]) >>> np.absolute(x) array([ 1.2, 1.2]) >>> np.absolute(1.2 + 1j) 1.5620499351813308