NumPy diff()

The diff() function calculates the difference of consecutive elements along a specified axis of an array.

Example

import numpy as np

array1 = np.array([1, 3, 6, 10, 15])

# use diff() to calculate difference of consecutive elements of array1 result = np.diff(array1)
print(result) # Output: [2 3 4 5]

diff() Syntax

The syntax of diff() is:

numpy.diff(array, n=1, axis=-1)

diff() Arguments

The diff() function takes following arguments:

  • array - the input array
  • n (optional) - the number of times the differences are taken consecutively
  • axis (optional) - the axis along which the differences are calculated

diff() Return Value

The diff() function returns an array that contains the differences of consecutive elements along the specified axis.


Example 1: diff() With 2-D Array

The axis argument defines how we can find the difference of consecutive elements in a 2-D array.

  • If axis = 0, the difference of consecutive elements is calculated column-wise.
  • If axis = 1, the difference of consecutive elements is calculated row-wise.
import numpy as np

array1 = np.array([[1, 3, 6],
                                [2, 4, 8]])

# compute the differences between consecutive elements column-wise (along axis 0) result1 = np.diff(array1, axis=0)
print("Differences along axis 0 (column-wise):") print(result1)
# compute the differences between consecutive elements row-wise (along axis 1) result2 = np.diff(array1, axis=1)
print("\nDifferences along axis 1 (row-wise):") print(result2)

Output

Differences along axis 0 (column-wise):
[[1 1 2]]

Differences along axis 1 (row-wise):
[[2 3]
 [2 4]]

Here,

  • The resulting array result1 contains the differences of consecutive elements for each column of array1.
  • The resulting array result2 contains the differences of consecutive elements for each row of array1.

Example 2: Use of n Argument in diff()

The n argument in diff() allows us to specify the number of times the differences are taken consecutively.

By default, n is set to 1, which calculates the differences between consecutive elements once.

import numpy as np

# create a 1D NumPy array
array1 = np.array([1, 4, 9, 16, 25])

# compute the first-order differences by setting n=1 result1 = np.diff(array1, n=1)
print("First-order differences:") print(result1)
# compute the second-order differences by setting n=2 result2 = np.diff(array1, n=2)
print("\nSecond-order differences:") print(result2)

Output

First-order differences:
[3 5 7 9]

Second-order differences:
[2 2 2]

In this example,

  • The resulting array result1 contains the differences between consecutive elements of array1. It is calculated as [4-1, 9-4, 16-9, 25-16], resulting in [3, 5, 7, 9].
  • The resulting array result2 contains the differences between consecutive elements of result1. It is calculated as [5-3, 7-5, 9-7], resulting in [2, 2, 2].

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges