Cannot cast array data from dtype('float64') to dtype('int32') according to the rule 'safe'

前端 未结 2 887
忘掉有多难
忘掉有多难 2021-01-22 07:00

I have a numpy array like

result = np.array([[[289, 354, 331],
                    [291, 206,  66],
                    [242,  70, 256]],

                   [[         


        
2条回答
  •  故里飘歌
    2021-01-22 07:36

    The problem is that when you divide by 4, you are creating float values, which don't want to go into the array of ints.

    If you want to use putmask, and avoid the problem of trying to convert to float, then you can use floor division (//) in order to change your values to int:

    np.putmask(result, result>255, result//4)
    
    >>> result
    array([[[ 72,  88,  82],
            [ 72, 206,  66],
            [242,  70,  64]],
    
           [[210,  97,  85],
            [ 68, 113, 218],
            [255,  87,  64]],
    
           [[127,  85, 173],
            [112,  98, 147],
            [223, 228, 100]]])
    

    Alternative #1:

    Convert your result array to a float dtype, and use your original putmask:

    result = result.astype(float)
    
    np.putmask(result, result > 255, result/4)
    
    >>> result
    array([[[ 72.25,  88.5 ,  82.75],
            [ 72.75, 206.  ,  66.  ],
            [242.  ,  70.  ,  64.  ]],
    
           [[210.  ,  97.25,  85.5 ],
            [ 68.25, 113.5 , 218.  ],
            [255.  ,  87.  ,  64.  ]],
    
           [[127.  ,  85.5 , 173.  ],
            [112.5 ,  98.75, 147.  ],
            [223.  , 228.  , 100.25]]])
    

    You can even convert back to int after if desired:

    result = result.astype(int)
    
    array([[[ 72,  88,  82],
            [ 72, 206,  66],
            [242,  70,  64]],
    
           [[210,  97,  85],
            [ 68, 113, 218],
            [255,  87,  64]],
    
           [[127,  85, 173],
            [112,  98, 147],
            [223, 228, 100]]])
    

    Alternative #2:

    Do away with putmask altogether, and you can get your desired results like this:

    result[result > 255] = result[result > 255] / 4
    
    >>> result
    array([[[ 72,  88,  82],
            [ 72, 206,  66],
            [242,  70,  64]],
    
           [[210,  97,  85],
            [ 68, 113, 218],
            [255,  87,  64]],
    
           [[127,  85, 173],
            [112,  98, 147],
            [223, 228, 100]]])
    

提交回复
热议问题