codingstreets
Search
Close this search box.
python-numpy-differences

Introduction to Python NumPy Differences

In This Article, You Will Learn About Python Numpy Differences.

Python Numpy Differences – Before moving ahead, let’s know a bit of Python NumPy Products

Differences

Difference means subtracting two distinct elements.

E.g. for [1, 2, 3], the discrete difference would be [2-1, 3-2] = [1, 1]

To find out discrete difference, use the diff() function.

Example – Finding discrete difference of the array.

import numpy as np

array = np.array([5, 10, 15, 20])

updated_array = np.diff(array)

print(updated_array)
python-numpy-differences

As a result, it returned an array after subtracting elements in itself. 20-15=5, 15-10=5, 10-5=5, finally it returned [5 5 5].

This process can be continued by specifying parameter n.

E.g. for [1, 2, 3], the discrete difference with parameter n = 5 would be [5-1, 5-2, 5-3] = [4, 3, 2], then, since n=5, we will do it again, with the new value: [5-5, 5-5] = [0, 0]

Example – Finding discrete difference of the array twice.

import numpy as np

array = np.array([5, 15, 25, 30])

updated_array = np.diff(array, n=2)

print(updated_array)
Output - 

[ 0 -5]

As shown above, it returns an array [0 -5] because 15-5=10, 25-15=10, 30-25=5 AND 10-10=0 and 5-10=-5. Finally it returned [0 5]

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on