问题描述
我有几个numpy数组,比如说a
,b
和c
,并创建了一个mask
应用于所有数组.
I have a few numpy arrays, lets say a
, b
, and c
, and have created a mask
to apply to all of them.
我正试图掩盖他们
a = a[mask]
其中mask
是bool
数组.值得注意的是,我已经证实了这一点
where mask
is a bool
array. It is worth noting that I have verified that
len(a) = len(b) = len(c) = len(mask)
我得到一个相当神秘的声音警告:
And I am getting a rather cryptic sounding warning:
FutureWarning: in the future, boolean array-likes will be handled as a boolean array index
推荐答案
False == 0,并且True ==1.如果掩码是列表而不是ndarray,则可能会出现一些意外行为:
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])
该数组由a [1],a [0]和a [1]组成,就像
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])
另一方面:
>>> mask_array = np.array(mask_list)
>>> mask_array
array([ True, False, True], dtype=bool)
>>> a[mask_array]
array([1, 3])
警告告诉您,最终a[mask_list]
将为您提供与a[mask_array]
相同的功能(这可能是您希望它首先为您提供的功能.)
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.)
这篇关于NumPy布尔数组警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!