NumPy diff()
The numpy.diff() function calculates the n-th discrete difference of an array along a specified axis.
It is commonly used to compute differences between consecutive elements.
The nth discrete difference refers to the process of computing the difference between elements of a sequence multiple times iteratively.
Given a sequence of numbers, the first discrete difference is simply the difference between consecutive elements. If we continue applying the difference operation repeatedly, we obtain the nth discrete difference.
Syntax
numpy.diff(a, n=1, axis=-1, prepend=<no value>, append=<no value>)
Parameters
| Parameter | Type | Description |
|---|---|---|
a | array_like | Input array for which differences are computed. |
n | int, optional | Number of times values are differenced. Default is 1. |
axis | int, optional | Axis along which the difference is calculated. Default is the last axis (-1). |
prepend | array_like, optional | Values to prepend before computing differences. |
append | array_like, optional | Values to append before computing differences. |
Return Value
Returns an array of differences with the same shape as a, except along the specified axis where its size is reduced by n.
The data type of the output depends on the difference calculation.
Examples
1. Calculating First-Order Differences
Computing the first-order discrete difference of a simple array.
import numpy as np
# Define an input array
arr = np.array([10, 15, 25, 40])
# Compute first-order differences
result = np.diff(arr)
# Print the result
print("First-order differences:", result)
Output:
First-order differences: [ 5 10 15]

2. Computing Higher-Order Differences
Using n=2 to compute second-order differences.
import numpy as np
# Define an input array
arr = np.array([10, 15, 25, 40])
# Compute second-order differences
result = np.diff(arr, n=2)
# Print the result
print("Second-order differences:", result)
Output:
Second-order differences: [5 5]

3. Using the axis Parameter
Computing differences along a specific axis in a 2D array.
import numpy as np
# Define a 2D array
arr = np.array([[1, 2, 4],
[6, 8, 10]])
# Compute differences along axis 0 (column-wise)
result_axis0 = np.diff(arr, axis=0)
# Compute differences along axis 1 (row-wise)
result_axis1 = np.diff(arr, axis=1)
# Print the results
print("Column-wise differences:\n", result_axis0)
print("Row-wise differences:\n", result_axis1)
Output:
Column-wise differences:
[[5 6 6]]
Row-wise differences:
[[1 2]
[2 2]]

4. Using prepend and append
Adding values before and after the input array before computing differences.
import numpy as np
# Define an input array
arr = np.array([3, 7, 15, 20])
# Compute differences with a prepended value
result_prepend = np.diff(arr, prepend=0)
# Compute differences with an appended value
result_append = np.diff(arr, append=25)
# Print the results
print("Differences with prepend:\n", result_prepend)
print("Differences with append:\n", result_append)
Output:
Differences with prepend:
[ 3 4 8 5]
Differences with append:
[ 4 8 5 5]

The prepend argument inserts a value before computing differences, while append inserts a value at the end.
