[英]Cannot cast array data from dtype('float64') to dtype('int32') according to the rule 'safe'
I have a numpy array like 我有一个像numpy数组
result = np.array([[[289, 354, 331],
[291, 206, 66],
[242, 70, 256]],
[[210, 389, 342],
[273, 454, 218],
[255, 87, 256]],
[[127, 342, 173],
[450, 395, 147],
[223, 228, 401]]])
and i'm trying to mask the array if an element is greater than 255. ie i assume it to be of range 0-1024 and divide my value by 4 我试图掩盖数组,如果一个元素大于255.即我假设它是0-1024范围并将我的值除以4
result = np.putmask(result, result > 255, result/4)
NOTE: result is the previous 3D array.And i'm getting this error 注意:结果是以前的3D数组。我收到此错误
TypeError: Cannot cast array data from dtype('float64') to dtype('int32') according to the rule 'safe'
What am i doing wrong? 我究竟做错了什么? Thanks in advance
提前致谢
Explanation of Error: 错误说明:
This is illustrative of an interesting property of numpy arrays: all elements of a numpy array must be of the same type 这说明了numpy数组的一个有趣属性:numpy数组的所有元素必须是相同的类型
For instance if you have the following array: 例如,如果您有以下数组:
>>> array1 = np.array([[23, 632, 634],[23.5, 67, 123.6]])
>>> array1
array([[ 23. , 632. , 634. ],
[ 23.5, 67. , 123.6]])
>>> type(array1[0][0])
<class 'numpy.float64'>
We notice that even though all the elements in the list [23, 632, 634] were all of the type int (specifically 'numpy.int64' ), all of the elements in array1 were converted to floats because of the element 123.6 in the second row (notice the decimal points in the array print out). 我们注意到,即使列表[ 23,632,634 ]中的所有元素都是int类型(特别是'numpy.int64' ), array1中的所有元素都被转换为浮点数,因为元素123.6在第二行(注意数组中的小数点打印出来)。
Similarily, if we include even one string anywhere in the array, all the elements of the array are cast to strings: 类似地,如果我们在数组中的任何位置包含一个字符串,则数组的所有元素都将转换为字符串:
>>> array2 = np.array([[23, 632, 'foo'],[23.5, 67, 123.6]])
>>> type(array2[0][0])
<class 'numpy.str_'>
Conclusion: 结论:
Your original result array contains elements of type 'numpy.int64' , but the result/4
operation returns an array of elements of type 'numpy.float64' (since 82 / 4 = 20.5 , etc.). 原始结果数组包含'numpy.int64'类型的元素,但
result/4
操作返回类型为'numpy.float64'的元素数组(因为82/4 = 20.5等)。 Thus when you try and replace the values in result , it is not 'safe' as you are inadvertently trying to place floats into an array of ints. 因此,当您尝试并替换结果中的值时,它不是“安全”,因为您无意中尝试将浮动放入一个int数组中。
The problem is that when you divide by 4, you are creating float values, which don't want to go into the array of int
s. 问题是当你除以4时,你正在创建浮点值,它们不想进入
int
数组。
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
: 如果你想使用
putmask
,并避免尝试转换为float的问题,那么你可以使用floor division( //
)来将你的值更改为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]]])
Convert your result
array to a float dtype
, and use your original putmask
: 将
result
数组转换为float dtype
,并使用原始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: 如果需要,您甚至可以转换回int:
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]]])
Do away with putmask
altogether, and you can get your desired results like this: 完全
putmask
,你可以得到你想要的结果:
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]]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.