Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

function
numpy
python
communitycreator

# What is the numpy.ediff1d() function in NumPy? Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

### Overview

The numpy.ediff1d() function in NumPy is used to compute the differences between consecutive elements of an input array.

### Syntax

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


### Parameters

The numpy.ediff1d() function takes the following parameters:

• ary: This is the input array.

• to_end: These are the numbers (if any) that are to be appended at the end of the array containing the resulting differences.

• to_begin: These are the numbers (if any) that are to be prepended at the beginning of the array containing the resulting differences.

### Return value

An array is returned which contains the following elements:

• The appended and prepended values (if any) at the end and start of the array respectively.

• The differences between the consecutive elements of the original array.

Note: If we want to append or prepend more than one element to the output array, we enclose those elements within a [], separated by commas. For example,numpy.ediff1d(ary, to_end=[4, 5], to_begin=[1, 2, 3]).

### Example

import numpy as np

# creating an array
a = np.array([1, 2, 3, 4, 5])

# Implementing the numpy.ediff1d() function
# taking the differences and also appending and prepending 3 and 2 respectively
myarray = np.ediff1d(a, 2, 3)

print("Input array: ", a)
print("Output array: ", myarray)
Implementing the numpy.ediff1d() function

### Explanation

• Line 1: We import the numpy module.

• Line 4: We create an array, a, using the np.array() method.

• Line 8: We implement the numpy.ediff1d() function on the array and assign it to a variable, myarray.

• Line 10: We print the input array, a.

• Line 11: We print the output array, myarray.

RELATED TAGS

function
numpy
python
communitycreator

CONTRIBUTOR Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring

Learn in-demand tech skills in half the time 