Find Difference Between Numbers In Array Using np.diff — pythonpip.com

Python np.diff()

Syntax

Arguments

  • a: This is the source array for which the difference is found using the np.diff() function.
  • n: The number of times the array is differenced is passed as the argument to n. This is by default set to 1.
  • axis: It is used to calculate the difference. It signifies that left to right is from right to left. The value is set to -1 by default. The axis can, however, be set to 0.
  • prepend: The values appended at the beginning before performing the diff() function.
  • append: The values that are appended at the ending before performing the diff() function.

Return Value

Finding the difference in a single array using np.diff()

Output:

[ -2 35 20 -62] ** Process exited - Return Code: 0 ** Press Enter to exit terminal
-2 = 10-12 35 = 45-10 20 = 65-45 -62 = 3-65

Finding the difference in a Multiple array using np.diff()

# Importing numpy as np import numpy as np # array arr = np.array([[12, 10, 45, 65, 3], [45, 16, 35, 25, 83]]) # creating a new array diff_arr = np.diff(arr, n=1, axis=0) print(diff_arr)
[[ 33 6 -10 -40 80]] ** Process exited - Return Code: 0 ** Press Enter to exit terminal
diff_arr[0][i] = arr[1][i]-arr[0][i]
33 = 45-12 ... ...

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store