numpy check array is monotonically decreasing

Numpy – Check If Array is Monotonically Decreasing

A numpy array is said to be monotonically decreasing if the subsequent values in the array are greater than or equal to the previous values.

Methods to check if a numpy array is monotonically decreasing

numpy check array is monotonically decreasing

To check if a Numpy array is monotonically decreasing, you can use one of the following methods –

  • Check if the array is sorted in descending order – by comparing the array with a sorted copy of the array.
  • Iterate through the array and check if each value is less than or equal to the previous value.
  • Use the numpy.diff() function to calculate the difference of the (i+1)th value from the ith value and check if it’s less than or equal to zero for all the differences.

Let’s now look at some examples of using the above methods –

Method 1 – Check if the array is sorted

If the array is sorted in descending order, we can say that the array is monotonically decreasing.

Now, we can use the numpy.sort() function to get a sorted copy of the original array and reverse it using the slice operation [::-1]. We compare the resulting descending order sorted array to the original array and check if all the corresponding values are the same or not in both arrays.

import numpy as np

# create numpy array
ar1 = np.array([7, 5, 3, 3, 2, 1])
ar2 = np.array([5, 3, 2, 7, 1])

# check if array is monotonically decreasing
print((ar1 == np.sort(ar1)[::-1]).all())
print((ar2 == np.sort(ar2)[::-1]).all())

Output:

True
False

We get True for the first array which is monotonically decreasing and False for the second array which isn’t.

Method 2 – Iterate through the array

The idea here is to iterate through the array and see if all the values are less than or equal to the previous values.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Let’s take the same example as above.

import numpy as np

# create numpy array
ar1 = np.array([7, 5, 3, 3, 2, 1])
ar2 = np.array([5, 3, 2, 7, 1])

# function to check if array is monotonically decreasing
def is_monotonically_decreasing(ar):
    for i in range(1, len(ar)):
        if ar[i] <= ar[i-1]:
            continue
        else:
            return False
    return True

# check if array is monotonically decreasing
print(is_monotonically_decreasing(ar1))
print(is_monotonically_decreasing(ar2))

Output:

True
False

We get the same result as above.

Method 3 – Using the numpy.diff() function

In this method, we use the numpy.diff() function to calculate the one-step difference in the array, the difference of (i+1)th element with the ith element, and check if all such differences are less than or equal to zero or not.

Let’s take the same example as above.

import numpy as np

# create numpy array
ar1 = np.array([7, 5, 3, 3, 2, 1])
ar2 = np.array([5, 3, 2, 7, 1])

# check if array is monotonically decreasing
print((np.diff(ar1) <= 0).all())
print((np.diff(ar2) <= 0).all())

Output:

True
False

We get the same result as above.

Here, np.diff(ar1) results in an array of differences which we compare with 0 and then check if all such comparisons result in True or not with the all() function.

Refer to this for more on the numpy.diff() function.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top