numpy.diff() in Python

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.

Synatx: 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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.diff() method
  
    
# importing numpy
import numpy as geek 
  
# input array
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))

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.diff() method
  
    
# importing numpy
import numpy as geek 
  
# input array
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))

chevron_right


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]]



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.