numpy.diff() in Python (original) (raw)
Last Updated : 22 Jul, 2021
numpy.diff(arr[, n[, axis]]) function is used when we calculate the n-th order discrete difference along the given axis. The first order difference is given by out[i] = arr[i+1] – arr[i] along the given axis. If we have to calculate higher differences, we are using diff recursively.
Syntax: numpy.diff()
Parameters:
arr : [array_like] Input array.
n : [int, optional] The number of times values are differenced.
axis : [int, optional] The axis along which the difference is taken, default is the last axis.
Returns : [ndarray]The n-th discrete difference. The output is the same as a except along axis where the dimension is smaller by n.
Code #1 :
Python3
import
numpy as geek
arr
=
geek.array([
1
,
3
,
4
,
7
,
9
])
print
(
"Input array : "
, arr)
print
(
"First order difference : "
, geek.diff(arr))
print
(
"Second order difference : "
, geek.diff(arr, n
=
2
))
print
(
"Third order difference : "
, geek.diff(arr, n
=
3
))
Output:
Input array : [1 3 4 7 9] First order difference : [2 1 3 2] Second order difference : [-1 2 -1] Third order difference : [ 3 -3]
Code #2 :
Python3
import
numpy as geek
arr
=
geek.array([[
1
,
2
,
3
,
5
], [
4
,
6
,
7
,
9
]])
print
(
"Input array : "
, arr)
print
(
"Difference when axis is 0 : "
, geek.diff(arr, axis
=
0
))
print
(
"Difference when axis is 1 : "
, geek.diff(arr, axis
=
1
))
Output:
Input array : [[1 2 3 5] [4 6 7 9]] Difference with axis 0 : [[3 4 4 4]] Difference with axis 1 : [[1 1 2] [2 1 2]]
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.less() in Python The numpy.less() : checks whether x1 is lesser than x2 or not. Syntax : numpy.less(x1, x2[, out]) Parameters : x1, x2 : [array_like]Input arrays. If x1.shape != x2.shape, they must be broadcastable to a common shape out : [ndarray, boolean]Array of bools, or a single bool if x1 and x2 are scalars. R 2 min read
- numpy.greater() in Python The numpy.greater() checks whether x1 is greater than x2 or not. Syntax : numpy.greater(x1, x2[, out]) Parameters : x1, x2 : [array_like]Input arrays. If x1.shape != x2.shape, they must be broadcastable to a common shape out : [ndarray, boolean]Array of bools, or a single bool if x1 and x2 are scala 2 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.less_equal() in Python The numpy.less_equal() function checks whether x1 is <= x2 or not. Syntax : numpy.less_equal(x1, x2[, out]) Parameters : x1, x2 : [array_like]Input arrays. If x1.shape != x2.shape, they must be broadcastable to a common shape out : [ndarray, boolean]Array of bools, or a single bool if x1 and x2 a 2 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 4 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
- numpy.greater_equal() in Python The numpy.greater_equal() checks whether x1 >= x2 or not. Syntax : numpy.greater_equal(x1, x2[, out]) Parameters : x1, x2 : [array_like]Input arrays. If x1.shape != x2.shape, they must be broadcastable to a common shape out : [ndarray, boolean]Array of bools, or a single bool if x1 and x2 are sca 2 min read