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

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** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 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 –

- Numpy – Check If All Array Elements are Equal
- Numpy – Check If Array is Sorted
- Numpy – Check If an Array contains a NaN value
- Check If Two Numpy Arrays are Equal

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