A numpy array is said to be monotonically increasing 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 increasing

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

**📚 Discover Online Data Science Courses & Programs (Enroll for Free)**

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

- Check if the array is sorted in ascending order – by comparing the array with a sorted copy of the array.
- Iterate through the array and check if each value is greater 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 greater 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 ascending order, we can say that the array is monotonically increasing.

Now, we can use the `numpy.sort()`

function to get a sorted copy of the original array and compare it to the original array and check if all the corresponding values are the same or not in both arrays.

**Upskill your career right now →**

import numpy as np # create numpy array ar1 = np.array([1, 2, 2, 3, 5, 7]) ar2 = np.array([1, 5, 3, 2, 7]) # check if array is monotonically increasing print((ar1 == np.sort(ar1)).all()) print((ar2 == np.sort(ar2)).all())

Output:

True False

We get `True`

for the first array which is monotonically increasing 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 greater than or equal to the previous values.

Let’s take the same example as above.

import numpy as np # create numpy array ar1 = np.array([1, 2, 2, 3, 5, 7]) ar2 = np.array([1, 5, 3, 2, 7]) # function to check if array is monotonically increasing def is_monotonically_increasing(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 increasing print(is_monotonically_increasing(ar1)) print(is_monotonically_increasing(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 greater than or equal to zero or not.

**Upskill your career right now →**

Let’s take the same example as above.

import numpy as np # create numpy array ar1 = np.array([1, 2, 2, 3, 5, 7]) ar2 = np.array([1, 5, 3, 2, 7]) # check if array is monotonically increasing 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.**