NumPy boolean array warning?

有些话、适合烂在心里 提交于 2019-12-05 02:12:21

False == 0, and True == 1. If your mask is a list, and not an ndarray, you can get some unexpected behaviour:

>>> a = np.array([1,2,3])
>>> mask_list = [True, False, True]
>>> a[mask_list]
__main__:1: FutureWarning: in the future, boolean array-likes will be handled as a boolean array index
array([2, 1, 2])

where this array is made up of a[1], a[0], and a[1], just like

>>> a[np.array([1,0,1])]
array([2, 1, 2])

On the other hand:

>>> mask_array = np.array(mask_list)
>>> mask_array
array([ True, False,  True], dtype=bool)
>>> a[mask_array]
array([1, 3])

The warning is telling you that eventually a[mask_list] will give you the same as a[mask_array] (which is probably what you wanted it to give you in the first place.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!