AppDividend
Latest Code Tutorials

np.diff: How to Find Difference Between Numbers in Array

The np.diff() is a numpy array function that finds the difference numbers in an array. The np.diff() function can be applied to a single array and multiple arrays. If a single array is passed then the difference is found by res[i] = arr[i+1] – arr[i].

Syntax

numpy.diff(a, n = 1, axis= -1, prepend = < no value >, append = < no value >)

Arguments

The np.diff() function takes five arguments as a parameter:

  1. a: It is the array for which the difference is found using the np.diff() function.
  2. n: The number of times the array is differenced is passed as the argument to n. This is by default set to 1. 
  3. axis: It is the axis in which the difference is calculated. It means that from right to left are from left to right. By default, the value is set to -1. However, we can also have the axis as 0. 
  4. prepend: The values appended at the beginning before performing the diff() function.
  5. append: The values that are appended at the ending before performing the diff() function.

Return value

The np.diff() function returns an array. This array consists of values that are the difference between the two numbers in an array.

Python program for finding the difference in a single array using np.diff()

# Importing numpy as np
import numpy as np

# Creating an array arr
arr = np.array([5, 10, 7, 11, 15])

# creating a new array by using the diff function
diff_arr = np.diff(arr)
print(diff_arr)

Output

[ 5 -3 4 4]

In this program, we imported numpy library and created a numpy array using the np.array() function. Then, we passed this array into the np.diff() function. Hence, we passed the value for a. The function performs the operations like diff_arr[i] = arr[i+1] – arr[i]. 

Hence, the first index will store the difference between the first two elements, which is 10 – 5; therefore, the first element is 5. Then in the second index of the diff_arr, the value is stored as 7 – 10 is -3. Similarly, this function calculates for all the elements in the array.

Program for finding the difference in multiple arrays inside the single array using the np.diff() function

# Importing numpy as np
import numpy as np

# Creating an array arr
arr = np.array([[5, 10, 7, 11, 15], [5, 20, 5, 15, 10]])

# creating a new array by using the diff function
diff_arr = np.diff(arr, n=1, axis=0)
print(diff_arr)

Output

[[ 0 10 -2 4 -5]]

In this program, we performed the np.diff() function in axis 0. Hence, it does the difference function like subtracting the first array elements from the second array elements. The internal formula is: diff_arr[0][i] = arr[1][i]-arr[0][i]. Hence, the first element of the diff_arr is equal to 5 – 5 = 0. Similarly, the others are calculated.

That’s it for the numpy diff() function in Python.

Related posts

np.unique

np.sin

np.hstack

np.concatenate

np.linspace

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.