numpy.diff() calculate the n-th discrete difference along the specified axis. It is commonly used to find differences between consecutive elements in a NumPy array, such as in time series or signal data. Example:
Python
import numpy as np
a = np.array([1, 2, 4, 7, 0])
res = np.diff(a)
print(res)
Explanation: np.diff() returns the difference between each pair of consecutive elements 2-1, 4-2, 7-4, 0-7-> 1,2,3,-7.
Syntax
numpy.diff(a, n=1, axis=-1, prepend=<no value>, append=<no value>)
Parameters:
Parameter | Description |
---|
a | Input array |
---|
n | Number of times values are differenced. Default is 1 |
---|
axis | Axis along which the difference is taken. Default is the last axis |
---|
prepend | Values to prepend to a before performing the difference |
---|
append | Values to append to a before performing the difference |
---|
Returns: A new array with the same shape as a except along the specified axis, where the dimension is reduced by n (unless prepend or append is used).
Examples
Example 1: In this example, we use the n=2 parameter to calculate the second discrete difference of the array.
Python
import numpy as np
a = np.array([1, 2, 4, 7, 0])
res = np.diff(a, n=2)
print(res)
Explanation: We compute the second-order difference by applying np.diff() twice first, np.diff(a) gives [1, 2, 3, -7] (first differences), then applying np.diff() again gives [1, 1, -10], the second-order differences.
Example 2: In this example, we use the axis=1 parameter to compute the difference between elements along each row of a 2D array.
Python
import numpy as np
a = np.array([[1, 2, 4], [3, 5, 9]])
res = np.diff(a, axis=1)
print(res)
Explanation: We compute the difference along each row using axis=1. For the first row 2−1 = 1, 4−2 = 2 -> [1, 2] and for the second row: 5−3 = 2, 9−5 = 4 → [2, 4]. The function operates independently on each row.
Example 3: In this example, we prepend a value (0) before the array to compute differences without reducing the output length.
Python
import numpy as np
a = np.array([5, 10, 15])
res = np.diff(a, prepend=0)
print(res)
Explanation: By default, np.diff() reduces the array length by one. To retain the original size, we use prepend=0, which adds a 0 at the start: [0, 5, 10, 15]. The differences then are: 5−0 = 5, 10−5 = 5, 15−10 = 5 -> [5, 5, 5].
Example 4: In this example, we append a value (0) at the end of the array to compute differences including a trailing comparison.
Python
import numpy as np
a = np.array([1, 2, 3])
res = np.diff(a, append=0)
print(res)
Explanation: Here, we use append=0 to add a value at the end, making the array [1, 2, 3, 0]. Now the differences are: 2−1 = 1, 3−2 = 1 and 0−3 = -3 -> [1, 1, -3].
Similar Reads
Python | Numpy np.ediff1d() method With the help of np.ediff1d() method, we can get the 1D array of differences between two consecutive elements by using np.ediff1d() method. Syntax : np.ediff1d(array) Return : Return 1D array having differences of consecutive elements. Example #1 : In this example we can see that by using np.ediff1d
1 min read
numpy.ma.ediff1d() function in Python numpy.ma.ediff1d() function return the differences between consecutive elements of an array. Syntax : numpy.ma.ediff1d(arr, to_end = None, to_begin = None) Parameters : arr : [array_like] Input array. to_end : [array_like, optional] Number to append at the end of the returned differences. to_begin
1 min read
numpy.not_equal() in Python The numpy.not_equal() checks whether two element are unequal or not. Syntax : numpy.not_equal(x1, x2[, out])Parameters : x1, x2 : [array_like]Input Array whose elements we want to checkout : [ndarray, optional]Output array that returns True/False. A placeholder the same shape as x1 to store the resu
2 min read
numpy.polysub() in Python numpy.polysub() :This function helps to find the difference of two polynomials and then returning the result as a polynomial. Each input polynomial must be a sequence of polynomial coefficients, from highest to lowest degree. Parameters : p1 : Input polynomial 1 : 1x + 2. p2 : Input polynomial 2 : 9
1 min read
numpy.subtract() in Python numpy.subtract() function is used when we want to compute the difference of two array.It returns the difference of arr1 and arr2, element-wise.Syntax : numpy.subtract(arr1, arr2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj], ufunc 'subtract'
3 min read
Python | Numpy np.lagsub() method np.lagub() method is used to subtract one Laguerre series to another.It returns the difference of two Laguerre series c1 - c2. Syntax : np.lagub(c1, c2) Parameters: c1, c2 :[ array_like ] 1-D arrays of Laguerre series coefficients ordered from low to high. Return : [ndarray] Laguerre series coeffici
1 min read