3

I have a huge 4 dimensional array, but for this example I will use a 3-dimenisonal array, x.

The array is of type float.

I'm trying to use boolean masking to locate specific numbers and then change those numbers to another number.

However, it seems I'm unable to locate some float numbers despite them being a element in my array 'x'

x is a 32,32,3 array

Here is how a portion of my x array looks like :

>>> x

array([[[ -71.71074,  -74.05614,  -69.5538 ],
        [ -87.14036,  -89.44238,  -86.85358],
        [ -81.05044,  -88.24616,  -89.58144],
        ..., 
        [  26.94098,   -4.307  ,  -24.44078],
        [  21.60692,  -10.70848,  -30.00036],
        [  17.826  ,  -11.4736 ,  -28.89246]],

       [[-114.0993 , -115.29484, -111.36412],
        [-129.3446 , -134.45914, -130.4656 ],
        [-112.2169 , -127.1767 , -131.10234],
        ..., 
        [  -7.04254,  -47.13862,  -75.90018],
        [ -10.33038,  -51.55898,  -80.49918],
        [  -7.1568 ,  -47.38368,  -73.45104]],

       [[-104.72472, -110.64818, -109.2514 ],
        [-112.71662, -126.49748, -129.01922],
        [ -80.47348, -107.05546, -121.47586],
        ..., 
        [ -11.21066,  -49.95824,  -79.29398],
        [  -8.59094,  -49.54152,  -79.06528],
        [ -19.59134,  -60.55772,  -87.22356]],

       ..., 
       [[  81.44628,   44.0467 ,  -18.1421 ],
        [  76.41156,   29.2665 ,  -77.77894],
        [  74.07492,   38.1989 ,  -84.73544],
        ..., 
        [  35.75964,    9.99658,  -40.87088],
        [ -68.68902,  -92.7463 , -104.75116],
        [ -72.51674,  -90.86008,  -93.10554]],

       [[  52.9319 ,   12.62384,  -18.64896],
        [  47.53098,   -1.58318,  -70.6934 ],
        [  60.71512,   19.8131 ,  -82.16812],
        ..., 
        [  58.50748,   23.66242,  -18.25642],
        [ -28.58606,  -62.64844,  -78.71842],
        [ -42.9967 ,  -72.2858 ,  -79.60626]],

       [[  49.26526,   17.05222,    0.55436],
        [  41.40204,    3.34674,  -19.9712 ],
        [  52.13484,   16.24322,  -26.9406 ],
        ..., 
        [  89.22164,   58.30292,   26.16942],
        [  24.43504,   -7.61742,  -29.90624],
        [  -3.63908,  -33.8503 ,  -42.38186]]])

So I picked numbers within this array and see if I could locate them using boolean masking. However, only some numbers were located:

This located the element:

>>> x[x==60.71512]
array([ 60.71512])

However, this didn't locate the element

>>> x[x==-28.58606]
array([], dtype=float64)

#empty array result despite being an element within my array

I'm confused by this behavior.

My ultimate goal is to locate all 0 elements within my 4-dimensional arrays and convert them .1.

y is my 4-dimesional array (holding images)

I was assuming y[y==0] = .1 would work, but now do the strange behavior above, I'm not sure if the empty arrays I'm getting are due to the behavior above or due to there not actually being zeros in my array.

Also, since I"m dealing with floats, should it be y[y==0.00]?

5
  • 1
    Try np.isclose(). Commented Nov 15, 2017 at 20:20
  • There doesn't seem to be any 0 elements in this array... Do you mean exactly 0 as in 0.00000.... or 0 <= e < 1? Commented Nov 15, 2017 at 20:24
  • 1
    first use np.set_printoptions(17) to see all the decimals... an use np.isclose as already mentionned Commented Nov 15, 2017 at 21:43
  • @B.M. Yeah, np.isclose fixes the float problem. Thank you Commented Nov 15, 2017 at 22:46
  • @Divakar Yeah, np.isclose fixes the float problem. Thank you. Commented Nov 15, 2017 at 22:46

1 Answer 1

1

If you mean literally elements of exactly value 0, then yes, you can do what you suggested in the question. Note that when testing these operations for multi-dimensional arrays, it can sometimes help to simplify by working with 1d arrays:

>>> a = np.array([1, 2, 4, 0, 1, 0, 7, 0], dtype="float32")
>>> a[a==0] = 1
>>> a
array([ 1.,  2.,  4.,  1.,  1.,  1.,  7.,  1.], dtype=float32)

As first mentioned by @Divikar, you can use np.isclose to get rid of the errors caused by the floats.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, I meant exactly zero (to avoid zero division problems). Thank you.
Hi Joe, sorry about that. Jjust include Divakar's portion as well as he answered the other part of my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.