NumPy Diff Simply Explained [Tutorial + Video]

5/5 - (2 votes)

NumPy’s np.diff() function calculates the difference between subsequent values in a NumPy array. For example, np.diff([1, 2, 4]) returns the difference array [1 2].

Here is a simple example to calculate the Fibonacci number differences:

import numpy as np

# Fibonacci Sequence with first 8 numbers
fibs = np.array([0, 1, 1, 2, 3, 5, 8, 13, 21])

diff_fibs = np.diff(fibs)
# [1 0 1 1 2 3 5 8]

This code snippet shows the most simple form of the np.diff() method: how to use it on a one-dimensional NumPy array. It calculates the difference between two subsequent values of a NumPy array. Hence, an array with n elements results in a diff array with n-1 elements.

Formal Syntax

numpy.diff(a, n=1, axis=-1, prepend=<no value>, append=<no value>)

Calculate the n-th discrete difference along the given axis.

  • First difference: out[i] = a[i+1] - a[i] along the given axis.
  • Higher differences: use np.diff() recursively.
ArgumentData TypeExplanation
aarray-likeArray or list for which the differences should be calculated.
nintOptional, per default n=1. The order, i.e., number of repeated difference computations. If zero, returns a.
axisintOptional, per default the last axis=-1. The axis along which to calculate differences.
prependarray-likeValues to prepend to array a along axis before calculating the difference.
Scalar value or array matching dimension and shape of a.
appendndarray Values to append to array a along axis before calculating the difference.
Scalar value or array matching dimension and shape of a.
Official docs

Executing the NumPy Diff Method Multiple Times

Executing the NumPy Diff Method Multiple Times
Figure: Repeatedly calling np.diff() to calculate the i-th order differences.

We can also run the NumPy diff function multiple times by setting the second optional argument n:

import numpy as np

a = np.array([2, 4, 7, 4, 1, 8, 11, 12])
print(np.diff(a, n=1))
# [ 2  3 -3 -3  7  3  1]

print(np.diff(a, n=2))
# [ 1 -6  0 10 -4 -2]

print(np.diff(a, n=3))
# [ -7   6  10 -14   2]

print(np.diff(a, n=4))
# [ 13   4 -24  16]

print(np.diff(a, n=5))
# [ -9 -28  40]

print(np.diff(a, n=6))
# [-19  68]

print(np.diff(a, n=7))
# [87]

print(np.diff(a, n=8))
# []

By defining the argument n, you can execute the diff function multiple times on the respective output of the last execution. Hence, the call np.diff(x, n=2) results in the same output as np.diff(np.diff(x)).

>>> np.diff([1, 2, 4], 2)
>>> np.diff(np.diff([1, 2, 4]))

NumPy Diff with Two Axes

But what happens if you have a two-dimensional NumPy array? In other words, how does the diff function work with multiple axes?

Here is an example of how you can use the diff function to calculate the differences along the columns (axis=1):

import numpy as np

a = np.array([[0, 1, 1],
              [2, 3, 5],
              [8, 13, 21]])

diffs = np.diff(a, axis=1)
[[1 0]
 [1 2]
 [5 8]]

You can see that each row with three columns is collapsed into a row with only two columns (the differences).

Let’s make it even more complex and combine the axis with the n argument for multiple diff executions in a single function call:

import numpy as np

a = np.array([[0, 1, 1],
              [2, 3, 5],
              [8, 13, 21]])

diffs = np.diff(a, n=2, axis=1)
 [ 1]
 [ 3]]

In this puzzle, we use the axis argument axis=1 which means that we calculate the differences along the columns. For example, the first column results in the diff array [0 1].

When defining the parameter n, the diff function is applied n times to the output of the previous function execution. Thus, the first column undergoes the following transformations:

[0 1 1] diff--> [1 0] diff--> [-1]

Calculating Differences in a 3D NumPy Array

In NumPy, you can calculate differences along different axes in a 3D array using numpy.diff(). This function computes the difference between consecutive elements along a specified axis.

import numpy as np

arr = np.array([[[1, 3], [5, 7]], [[2, 4], [6, 8]]])
diff = np.diff(arr, axis=0)

Explanation: Here, np.diff calculates the difference along the first axis (axis=0) of a 3D array. It subtracts adjacent elements in this axis, resulting in a new array showing these differences.

In NumPy, an array can have one or more dimensions, and each dimension is referred to as an axis. Understanding axes in NumPy is crucial for performing operations like summation, multiplication, or finding differences across different dimensions of an array.

  • Axis 0 is often considered the “rows” in a 2D array, but in higher dimensions, it represents the first dimension of the array. For a 3D array, it could be thought of as the different layers or depth.
  • Axis 1 in a 2D array corresponds to the columns. In a 3D array, it’s the second dimension, which you can think of as rows across each layer.
  • Axis 2 in a 3D array is the third dimension, analogous to the columns in each of the 2D arrays or layers.

When you perform an operation along an axis, NumPy aggregates over that axis. For example, if you calculate a sum along axis 0 in a 2D array, it sums down the columns (vertically). In a 3D array, calculating the difference along axis 0, as in the previous example, means it computes the difference between corresponding elements of the layers.

Where to Go From Here?

Having a proficient Python education is critical for your success as a developer. You cannot hope to master data science if you do not even know the most basic Python and computer science concepts.

To this end, I have created a free Python email course (+ Bonus Cheat Sheet series). Subscribe if you need to refresh your basic Python knowledge! It’s fun!

2 thoughts on “NumPy Diff Simply Explained [Tutorial + Video]”

Comments are closed.