Python | Numpy np.ediff1d () method



With the np.ediff1d() method, we can get a one-dimensional array of differences between two consecutive elements using np.ediff1d () .

Syntax: np.ediff1d (array)
Return: Return 1D array having differences of consecutive elements.

Example # 1:
In this example we see that with np.ediff1d () we can get a 1D array of consecutive differences of array elements using this method.

# NumPy import

import numpy as np

 
# using the np.ediff1d () method

arr = np.array ([ 1 , 2 , 3 , 5 , 7 , 11 ])

gfg = np.ediff1d (arr)

 

print (gfg)

Output:

[1 1 2 2 4]

Example # 2:

# NumPy imports

import numpy as np

  
# using the np.ediff1d () method

arr = np.array ([ 1 , 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 ])

gfg = np.ediff1d (arr)

 

print (gfg)

Logout:

[1 1 2 2 4 2 4 2 4 6 2 6 4 2 4]