NumPy diff() in python

Share this article

In Python’s NumPy library, numpy.diff() is a function that calculates the difference between consecutive elements of an array. It’s useful when you need to find changes, rates, or increments in data, especially in numerical and scientific computing.

Example

import numpy as np

arr = np.array([5, 10, 15, 20])
result = np.diff(arr)
print(result)  # Output: [5 5 5]

What it does

numpy.diff() takes an input array and returns a new array containing the differences between consecutive elements. By default, it computes the first-order difference (i.e., the difference between every pair of adjacent elements).

  • Single difference: If you don’t specify the number of differences, it defaults to calculating the first-order difference.
  • Higher-order differences: You can specify the number of times to repeat the differencing.

Examples

Example 1: Basic usage with a 1D array

import numpy as np

arr = np.array([2, 4, 6, 8])
result = np.diff(arr)
print(result)  # Output: [2 2 2]

This calculates the difference between each consecutive element in the array, resulting in a new array [2, 2, 2]. Here, 4 - 2 = 2, 6 - 4 = 2, and 8 - 6 = 2.

Example 2: Using with a 2D array

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
result = np.diff(arr)
print(result)  
# Output: [[1 1]
#          [1 1]]

When using numpy.diff() with a 2D array, it calculates the difference along the last axis (columns) by default. You can also specify an axis using the axis parameter if needed.

Example 3: Higher-order differences

import numpy as np

arr = np.array([1, 3, 6, 10])
result = np.diff(arr, n=2)
print(result)  # Output: [1 1]

By specifying n=2, this calculates the second-order difference, which means it finds the difference of differences. Here, 3 - 1 = 2 and 6 - 3 = 3, resulting in first differences [2, 3, 4]. Then, the difference between these values is [1, 1].

Example 4: Finding changes in stock prices

import numpy as np

stock_prices = np.array([100, 105, 103, 110])
price_changes = np.diff(stock_prices)
print(price_changes)  # Output: [ 5 -2  7]

This shows the changes in stock prices day by day. The output [5, -2, 7] indicates how the price increased by 5, then decreased by 2, and finally increased by 7.

By using numpy.diff(), you can quickly understand how data changes between consecutive points in a sequence.