w3resource

NumPy: Calculate the difference between neighboring elements, element-wise, and prepend [0, 0] and append [200] to a given array

NumPy Mathematics: Exercise-30 with Solution

Write a NumPy program to calculate the difference between neighboring elements, element-wise, and prepend [0, 0] and append[200] to a given array.

Sample Solution:

Python Code:

# Importing the NumPy library
import numpy as np

# Creating an array
x = np.array([1, 3, 5, 7, 0])

# Displaying the original array
print("Original array: ")
print(x)

# Calculating differences between neighboring elements, element-wise, and appending [200] to the end and [0, 0] to the beginning
r1 = np.ediff1d(x, to_begin=[0, 0], to_end=[200])

# Calculating differences between neighboring elements, appending [200] to the end, and inserting [0, 0] at the beginning
r2 = np.insert(np.append(np.diff(x), 200), 0, [0, 0])

# Checking if the resulting arrays r1 and r2 are equivalent
assert np.array_equiv(r1, r2)

# Displaying the resulting array with differences and appended/prepended values
print("Difference between neighboring elements, element-wise, and prepend [0, 0] and append[200] to the said array:")
print(r2) 

Sample Output:

Original array: 
[1 3 5 7 0]
Difference between neighboring elements, element-wise, and prepend [0, 0] and append[200] to the said array:
[  0   0   2   2   2  -7 200]

Explanation:

x = np.array([1, 3, 5, 7, 0]) - Create an input array x with 5 elements.

r1 = np.ediff1d(x, to_begin=[0, 0], to_end=[200]) – The np.ediff1d() function computes the differences between consecutive elements of an array. The to_begin and to_end arguments specify the values to use at the beginning and end of the flattened array. In this case, to_begin=[0, 0] adds two zeros to the beginning of the flattened array, and to_end=[200] adds 200 to the end. The resulting flattened array is [0, 0, 1, 3, 5, 7, 0, 200] and the differences between consecutive elements are [0, 1, 2, 2, 2, -7, 200], which represent the "extended differences" of the input array.

r2 = np.insert(np.append(np.diff(x), 200), 0, [0, 0]) – This line of code first calculates the first-order difference of the input array x using np.diff(x) which results in an array of length one less than the original array. Then, it appends the value 200 to the end of this difference array using np.append(np.diff(x), 200). Finally, it inserts two 0s to the beginning of the array using np.insert(..., 0, [0, 0]). So the resulting array r2 will have the value 0 inserted as the first two elements, followed by the difference values between adjacent elements of the original array x, and finally, the value 200 added as the last element of the array. So the resulting array is [ 0 0 2 2 2 -7 200].

Python-Numpy Code Editor:

Previous: Write a NumPy program to calculate the difference between neighboring elements, element-wise of a given array.

Next: Write a NumPy program to compute ex, element-wise of a given array

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.