Updated March 28, 2023
Introduction to numpy.diff()
numpy.diff() is a function of the numpy module which is used for depicting the divergence between the values along with the x-axis. So the divergence among each of the values in the x array will be calculated and placed as a new array. These difference values for the arrays can be calculated across up to n number of times. so this means the disparity between the given values can be effectively estimated across multiple levels of the arrays.
Syntax:
numpy.diff(a, n=1, axis=-1, prepend=<no value>, append=<no value>)
- a = The array which is keyed in for determining the difference across the elements of the array.
- n = Represents the entire number of times the differentiation process needs to be carried upon.
- append, prepend = If some value needs to be appended or prepended to the values in the x-axis then these parameters are been used. here the difference will be calculated just before the append or the prepend process.
How numpy.diff() Works?
Consider an input array Test, the occurrence of the first difference for the input array is calculated using the formula out[i]=Test[i+1]-a[i]. The diff() again on this array helps to calculate the higher difference values.
The numpy is used on top of a one-dimensional numpy array. here the one-dimensional array has only a single-dimensional set of elements to it. But there could be instances where it needs to be substituted over a two-dimensional array too. from a different perception, there are situations where the numpy element need to be substituted over a two-dimensional array also. these two-dimensional arrays are in other words termed as multiple axes.
So at the point of implying diff() function over two-dimensional axis arrays then we rely on the use of the argument called an axis. here the value specified for the axis argument will be representing the columns of the two-dimensional array. so as like mentioned before at the point of applying the diff() function execution the initial column in the array undergoes a transformation which is very similar to below.
Sample array,
[0, 1, 1], [7, 3, 15], [8, 3, 11]Difference value for the first row in the two-dimensional array will be taken forward as like below.
- [0 1 1] difference = [1 0]
- [1 0] difference = [-1]
The same process will be extended upon each and every row in the array,
Examples of numpy.diff()
Following are the examples are given below:
Example #1
Code:
import numpy as np
Date_array = np.arange('2020-09-01', '2020-09-05', dtype=np.datetime64)
output_diffrence = np.diff(Date_array)
print("The difference in date value is: " + str(len(output_diffrence)) +" days ")
Output:
Code Explanation: The above program uses numpy library for determining the difference of days within two date values in an array. the program begins with an import of the numpy library using the alias name as np. the np.arrange() method is used for creating an array element, the array element formulated in the arrange function is based on the below syntax.
numpy.arange([start_value, stop_value, n_value,dtype=None)
here the first two indexes refer to the start and stop value whereas the third type mentions the data type which is been used. In this case for determining the expanded array of dates, the arrange method is filled with the start and the end date values. Both the date values are maintained in the YYYY-MM-DD format. so all dates falling within this are determined and formulated as an array. the formulated array is passed as input to the np.diff() function. so the np.diff() function is responsible for determining the difference in date values between each and every item in the formulated array. so once the difference value is determined it is depicted as an array. we have additionally used the len() function to determine the length of the array. based on the length of the array the number of days is determined. The determined number of days is populated in the print console as output.
Example #2
Code:
import numpy as np
array_var = np.array([1,2,5])
print("Array value:",array_var)
output_diffrence = np.diff(array_var)
print("The diffrence value is: ",output_diffrence )
Output :
Code Explanation: The given program is used for determining the difference of value between a given set of the array, So the program starts with a header import of numpy module as alias name np.
Next, the array variable is passed as input to the np.diff() function. As we already know this np.diff() function is primarily responsible for evaluating the difference between the values of the array. the degree of difference can be depicted next to this parameter. based on the degree of difference mentioned the formulated array list will get hierarchal determined for its difference. the derived output is printed to the console by means of the print statement.
Example #3
Code:
import numpy as np
array_var = np.array([[1, 7, 4, 12], [4, 2, 4, 8]])
print("Array value:",array_var)
output_diffrence = np.diff(array_var,n=2)
print("The diffrence value is: ",output_diffrence )
Output :
Code Explanation: The given program is used for formative the disparity of value between a given set of two different arrays, So the program starts with a header import of numpy module as alias name np. the array function is used for creating an array with the necessary set of values and data types are handily specified. Again as like before here too the returned value of the array function is stored in a variable of name ‘array’. This particular variable is accountable for holding the array values and the value held by this array set of the variable is displayed in the console. The necessity for performing the array function conversion in these diff() function processing is because there is a need to convert a list of values passed into a-axis oriented representation.
Conclusion
The above content nicely mentions the necessity of np.diff() function in python oriented programming for axis level difference calculation. Also additionally a set of three different techniques for implementing np.diff() is also discussed.
Recommended Articles
This is a guide to numpy.diff(). Here we discuss the Introduction and working of python numpy.diff() along with different examples and its code implementation. You may also look at the following articles to learn more –