NumPy diff() Function:
Diff() function python: The diff() function of the numpy module is used for calculating the nth discrete difference along the specified axis.
The first difference is given by out[i] = a[i+1] – a[i] along the specified axis, higher differences are calculated recursively by this function.
Syntax:
numpy.diff(a, n=1, axis=-1, prepend=<no value>, append=<no value>)
Parameters
a: This is required. It is an array (array-like) given as input.
n: This is optional. It indicates the number of times values are differenced. If the value is 0, the input is returned unchanged.
axis: This is optional. It indicates the axis along which the difference is calculated; the last axis is the default.
prepend, append: This is optional. Before conducting the difference, give the values to prepend or append to along the axis. Scalar values are expanded to arrays of length 1 in the axis direction and the shape of the input array in all other axes. Otherwise, the dimension and shape must be identical with “a” except along the axis.
Return Value:
Numpy diff: An array with n-th discrete difference along the specified axis of “a” is returned. The output has the same shape as a, except along the axis where the dimension is smaller by n.
- Python NumPy hstack() Function
- In Python, How do you append an Array?
- Python: Count Number of True Elements in Numpy Array in Python
NumPy diff() Function in Python
Example1
Approach:
- Import NumPy module using the import keyword.
- Pass some random list as an argument to the array() function to create an array.
- Store it in a variable.
- Print the above-given array.
- Get the first difference array by passing the given array and 1 as arguments the diff() function.
- To get the second difference array we apply the diff() function for the above result and pass second argument as 1.
- We can directly get the second difference array by passing 2 as the second arguments to the diff() function.
- Print the first difference array.
- Print the second difference array from the first difference array.
- Print the second difference array directly.
- The Exit of the Program
Below is the implementation:
# Import NumPy module using the import keyword. import numpy as np # Pass some random list as an argument to the array() function to # create an array. # Store it in a variable. gvn_arry = np.array([5, 10, 12, 4, 25]) # Print the above given array. print("The above given array is:") print(gvn_arry) # Get the first difference array by passing the given array and 1 as # argument the diff() function. fst_diffrnce = np.diff(gvn_arry, 1) # To get the second difference array we apply the diff() function for the # above result and pass second argument as 1 scnd_diffrnce = np.diff(fst_diffrnce, 1) # We can directly get the second difference array by passing 2 as second argument to # the diff() function direct_scnddiff = np.diff(gvn_arry, 2) # Print the first difference array print("The first difference array:", fst_diffrnce) # Print the second difference array from first difference array print("The second difference array from first difference array:", scnd_diffrnce) # Print the second difference array directly print("The second difference array directly :", direct_scnddiff)
Output:
The above given array is: [ 5 10 12 4 25] The first difference array: [ 5 2 -8 21] The second difference array from first difference array: [ -3 -10 29] The second difference array directly : [ -3 -10 29]
Example2
Approach:
- Import NumPy module using the import keyword.
- Pass some random list as an argument to the array() function to create an array.
- Store it in a variable.
- Print the above-given array.
- Get the first difference array along axis=0 by passing the given array, 1 and axis=0 as arguments the diff() function.
- To get the first difference array along axis=1, apply the diff() function by # passing the given array, 1 and axis=1 as the arguments to it.
- Print the first difference array along axis= 0.
- Print the first difference array along axis= 1.
- The Exit of the Program.
Below is the implementation:
# Import NumPy module using the import keyword. import numpy as np # Pass some random list as an argument to the array() function to # create an array. # Store it in a variable. gvn_arry = np.array([[2, 5, 8], [1, 3, 2], [4, 6, 5]]) # Print the above given array. print("The above given array is:") print(gvn_arry) # Get the first difference array along axis=0 by passing the given array, 1 and axis=0 as # arguments the diff() function. fst_diffrnce_0 = np.diff(gvn_arry, 1, axis=0) # To get the first difference array along axis=1, apply the diff() function by # passing the given array, 1 and axis=1 as the arguments to it. fst_diffrnce_1 = np.diff(gvn_arry, 1, axis=1) # Print the first difference array along axis= 0 print("The first difference array along axis= 0:\n", fst_diffrnce_0) # Print the first difference array along axis= 1 print("The first difference array along axis= 1:\n", fst_diffrnce_1)
Output:
The above given array is: [[2 5 8] [1 3 2] [4 6 5]] The first difference array along axis= 0: [[-1 -2 -6] [ 3 3 3]] The first difference array along axis= 1: [[ 3 3] [ 2 -1] [ 2 -1]]