Skip to main content

NumPy Difference


In this tutorial you will learn:

  • What is the NumPy Difference?
  • Implementation of difference in Python
  • Python Syntax

NumPy Difference

Difference is just another name for the operation of subtraction. It is a very common operation in the world of computation, mathematics and science. For taking a discrete difference it is necessary that there are 2 numbers involved, they can be equal, lesser or greater to each other.

Implementation of Difference

Function diff() is used to get the difference of each element from its subsequent element in the array, the function takes in an array having size ‘s’ as a mandatory parameter and returns an array having size ‘s-1’. Suppose if we have an array with variable [a,b,c,d,e], the result of the applying the diff() function on the array will be [b-a,c-b,d-c,e-d]. You can observe that the size of input array has been decreased by 1.
Lets take an example, in this example we will be giving an array having size 4 as a parameter to the diff() function, an d the diff() function will be returning the output array having size 3.

  1. #importing the NumPy library
  2. import numpy as np
  3. #declaring an array
  4. my_arr= np.array([4,8,12,16])
  5. print('Array of which difference is being taken:', my_arr)
  6. #finding the difference of contents of an array
  7. res = np.diff(my_arr)
  8. #printing the result
  9. print('The result of diff() function:', res)

In this next example we will input an optional parameter ‘n’, ‘n’ indicates that how many repetitions of subtraction will be carried out. Any increase in ‘n’ will result in the decrease of the size of the array.
Suppose we have an array having elements [80,90,100,120], if we call the diff function with value of n equal to 1. You will observe that there will no repetition and diff() function will have no additional effect, but when I will change the value of ‘n’ to 2 you will observe that size of array has further decreased.
For ‘n’ as 1, result of the array would be [90-80, 100-90,120-100] that is [10, 10, 20]. For ‘n’ as 2, the diff() function will again be applied and the results would be [10-10, 20-10] equals [0,10].
Lets take an example, in this example we will raise the number of repetitions ‘n’ from 1 to 3. Here you can observe the decrease in size and result of each repetition.
  1. #importing the NumPy library
  2. import numpy as np
  3. #declaring an array
  4. my_arr= np.array([4,45,93,118])
  5. print('Array of which difference is being taken:', my_arr)
  6. #finding the difference of contents of an array with n=1
  7. res = np.diff(my_arr,n=1)
  8. print('The result after applying diff() with n=1:',res)
  9. #finding the difference of contents of an array with n=2
  10. res = np.diff(my_arr,n=2)
  11. print('The result after applying diff() with n=2:',res)
  12. #finding the difference of contents of an array with n=3
  13. res = np.diff(my_arr,n=3)
  14. print('The result after applying diff() with n=3:',res)

Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.