Replace all elements of Python NumPy Array that are greater than some value

NLi10Me

I have a 2D NumPy array and would like to replace all values in it greater than or equal to a threshold T with 255.0. To my knowledge, the most fundamental way would be:

shape = arr.shape
result = np.zeros(shape)
for x in range(0, shape[0]):
    for y in range(0, shape[1]):
        if arr[x, y] >= T:
            result[x, y] = 255
  1. What is the most concise and pythonic way to do this?

  2. Is there a faster (possibly less concise and/or less pythonic) way to do this?

This will be part of a window/level adjustment subroutine for MRI scans of the human head. The 2D numpy array is the image pixel data.

mdml

I think both the fastest and most concise way to do this is to use NumPy's built-in Fancy indexing. If you have an ndarray named arr, you can replace all elements >255 with a value x as follows:

arr[arr > 255] = x

I ran this on my machine with a 500 x 500 random matrix, replacing all values >0.5 with 5, and it took an average of 7.59ms.

In [1]: import numpy as np
In [2]: A = np.random.rand(500, 500)
In [3]: timeit A[A > 0.5] = 5
100 loops, best of 3: 7.59 ms per loop

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select array elements that are greater than 5% of a sum

From Dev

How to check that all elements of one array are greater than their counterparts in a parallel array (in Ruby).

From Dev

Lanczos scale not working when scaleKey greater than some value

From Dev

Remove all elements from array greater than n

From Dev

subset data.table keeping only elements greater than certain value applied to all columns

From Dev

Numpy: Replace random elements in an array

From Dev

Is it possible to select all elements with an attribute value greater than a certain number?

From Dev

Select all columns greater than some value

From Dev

How to replace only the first n elements in a numpy array that are larger than a certain value?

From Dev

Numpy array exclude some elements

From Dev

How to take a median of all the values greater than a certain number if the data is stored in a numpy array?

From Dev

Replace value in array when greater than x

From Dev

numpy.argmin for elements greater than a threshold

From Dev

Numpy: Replace every value in the array with the mean of its adjacent elements

From Dev

Write a program that return a new array that contains all values greater than the first value in the array in java

From Dev

How to get all values in an array whose added index size is greater than value?

From Dev

Is it possible to Multiply elements in the array if the elements value is greater than 10?

From Dev

Excel: Select the cell to the left that is greater than/less than some value

From Dev

php - Split an associative array with value greater than some integer

From Dev

Remove elements in a vector that are greater than value

From Dev

How to truncate a numpy array for values greater than a specified value?

From Dev

Lanczos scale not working when scaleKey greater than some value

From Dev

Quick comparison of numpy array elements, greater or less than each other

From Dev

'Greater than' some value in height()

From Dev

Is it possible to select all elements with an attribute value greater than a certain number?

From Dev

Select all columns greater than some value

From Dev

How can i change all the values of a multidimensional array which are greater than zero to 1 in python?

From Dev

How to check version is greater than some base value?

From Dev

PostgreSQL: Replace values greater than a certain limit in array elements

Related Related

  1. 1

    Select array elements that are greater than 5% of a sum

  2. 2

    How to check that all elements of one array are greater than their counterparts in a parallel array (in Ruby).

  3. 3

    Lanczos scale not working when scaleKey greater than some value

  4. 4

    Remove all elements from array greater than n

  5. 5

    subset data.table keeping only elements greater than certain value applied to all columns

  6. 6

    Numpy: Replace random elements in an array

  7. 7

    Is it possible to select all elements with an attribute value greater than a certain number?

  8. 8

    Select all columns greater than some value

  9. 9

    How to replace only the first n elements in a numpy array that are larger than a certain value?

  10. 10

    Numpy array exclude some elements

  11. 11

    How to take a median of all the values greater than a certain number if the data is stored in a numpy array?

  12. 12

    Replace value in array when greater than x

  13. 13

    numpy.argmin for elements greater than a threshold

  14. 14

    Numpy: Replace every value in the array with the mean of its adjacent elements

  15. 15

    Write a program that return a new array that contains all values greater than the first value in the array in java

  16. 16

    How to get all values in an array whose added index size is greater than value?

  17. 17

    Is it possible to Multiply elements in the array if the elements value is greater than 10?

  18. 18

    Excel: Select the cell to the left that is greater than/less than some value

  19. 19

    php - Split an associative array with value greater than some integer

  20. 20

    Remove elements in a vector that are greater than value

  21. 21

    How to truncate a numpy array for values greater than a specified value?

  22. 22

    Lanczos scale not working when scaleKey greater than some value

  23. 23

    Quick comparison of numpy array elements, greater or less than each other

  24. 24

    'Greater than' some value in height()

  25. 25

    Is it possible to select all elements with an attribute value greater than a certain number?

  26. 26

    Select all columns greater than some value

  27. 27

    How can i change all the values of a multidimensional array which are greater than zero to 1 in python?

  28. 28

    How to check version is greater than some base value?

  29. 29

    PostgreSQL: Replace values greater than a certain limit in array elements

HotTag

Archive