Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I lower down values to a specific number in a numpy array

Let’s say I have an array like this:

[1,5, 2, 6, 6.7, 8, 10]

I want to lower down the numbers that are larger than n.
So for example if n is 6, the array will look like this:

[1,5, 2, 6, 6, 6, 6]

I have tried a solution using numpy.vectorize:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

lower_down = lambda x : min(6,x)
lower_down = numpy.vectorize(lower_down)

It works but it’s too slow. How can I make this faster? Is there a numpy function for achieving the same result?

>Solution :

Numpy already has a minimum function, no need to create your own.

>>> np.minimum(6, [1,5, 2, 6, 6.7, 8, 10])
array([1., 5., 2., 6., 6., 6., 6.])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading