Numpy ediff1d – The differences between consecutive elements of an array

Numpy Ediff1D Cover

In this article, we understand and implement numpy.ediff1d() which is a NumPy function, as NumPy is an array processing package numpy.ediff1d() is used to calculate the difference between elements of the array.

What is Numpy.ediff1d()?

The numpy.ediff1d() is used in python to compute the consecutive difference between the elements of a given array.

We can mention the prepend value through to_begin which prepends a value at the start of an array similarly we can append a value at the end of an array via to_end .

These values can contain more than one element to be prepended or appended.

Syntax of ediff1d()

numpy.ediff1d(ary, to_end=None, to_begin=None)

Parameter

Parameter DescriptionRequired/Optional
ary: array_likeInput arrayrequired
to_end: array_likeThese are the numbers to be appended at the end of the array containing the difference .optional
to_begin: array_likeThese are the numbers to be prepended at the start of the array containing the difference .optional
Numpy -ediff1d syntax paramter

Return Value

An array is returned which consists of:

  • Prepend and append value ( if given any ) in the array.
  • The consecutive difference between the elements of the array.

Examples of the ediff1d() method

Importing numpy library and declaring and printing an array.

import numpy as np
arr=np.array([10,20,30,40,50])
print("Input array: \n",arr)
Input array:
 [10 20 30 40 50]

Example 1: Implementing numpy.ediff1d()

The code below uses the np.ediff1d() function to calculate the difference between each element in the given array ‘arr’.

x=np.ediff1d(arr)
print ("Output array: \n",x)

The output is an array containing the difference between each element of the given array. In this case, the output array contains four elements with each value equal to 10.

Output :

Output array:
 [10 10 10 10]

Example 2: Prepend and append elements to output.

This code creates an output array with an additional beginning and end value. It does this using the NumPy ediff1d function, which takes an array (in this case, arr) and adds the to_begin and to_end values (in this case, 1 and 100).

y=np.ediff1d(arr,to_begin=[1],to_end=[100])
print ("Output array with begin and end value: \n",y)

Output :

Output array with begin and end value:
 [  1  10  10  10  10 100]

Example 3: to prepend and append more than one element to output

The code below uses the numpy.ediff1d() function to calculate the difference between elements in an array (arr). The to_begin and to_end parameters are used to prepend and append elements to the output array.

z=np.ediff1d(arr, to_begin=[0,1], to_end=[90,100])
print ("Output array with multiple begin and end value: \n",z)

In this case, the output array will start with the value 1 and end with the value 100. The result is stored in the variable y and printed to the console.

Output :

Output array with multiple begin and end value:
 [  0   1  10  10  10  10  90 100]

Example 4: the returned array is always 1D.

This code uses the NumPy library to calculate the difference between each element of a two-dimensional array. The two-dimensional array is stored in a variable called ‘w’. The NumPy ediff1d() function is then used to calculate the difference between each element of the array.

w = [[1, 2, 4], [1, 6, 24]]
print ("Input array: \n",w)
print("Output array: \n",np.ediff1d(w))

The output of the ediff1d() function is a one-dimensional array containing the differences between each element of the array. In this example, the output array contains the differences 1, 2, -3, 5, and 18.

The output is calculated as:

  • 2 – 1 = 1
  • 4 – 2 = 2
  • 1 – 4 = -3
  • 6 – 1 = 5
  • 24 – 6 = 18

Output:

Input array:
 [[1, 2, 4], [1, 6, 24]]
Output array:
[ 1  2 -3  5 18]

Conclusion

We have understood and implemented the numpy.ediff1d() function with and without optional parameters ie to_end and to_begin having single or multiple elements as their value. numpy.ediff1d() is utilized to return an array consisting of consecutive difference value of the elements of the input array.

Reference

https://het.as.utexas.edu/HET/Software/Numpy/reference/generated/numpy.ediff1d.html