Categories
Python Answers

How to replace all elements of Python NumPy Array that are greater than some value?

Spread the love

Sometimes, we want to replace all elements of Python NumPy Array that are greater than some value.

In this article, we’ll look at how to replace all elements of Python NumPy Array that are greater than some value.

How to replace all elements of Python NumPy Array that are greater than some value?

To replace all elements of Python NumPy Array that are greater than some value, we can get the values with the given condition and assign them to new values.

For instance, we write

import numpy as np

A = np.random.rand(500, 500)
A[A > 0.5] = 5

to create a NumPy array A with some random values.

Then we get all the values that are bigger than 0.5 and set them to 5 with

A[A > 0.5] = 5

Conclusion

To replace all elements of Python NumPy Array that are greater than some value, we can get the values with the given condition and assign them to new values.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *