简体   繁体   English

为什么我的数据不被屏蔽?

[英]Why aren't my data being masked?

data = [[0, 1, 1, 5, 5, 5, 0, 2, 2, 2, 2, 2, 2, 2, 6, 6, 6, 6, 6, 6, 6, 6],
        [1, 1, 1, 0, 5, 5, 5, 0, 2, 2, 0, 0, 2, 0, 0, 6, 6, 6, 0, 0, 6, 6],
        [1, 1, 1, 0, 0, 0, 0, 0, 2, 2, 0, 2, 2, 2, 0, 0, 2, 6, 0, 0, 6, 6]]

The data object i have is a <class 'numpy.ndarray'> 我拥有的数据对象是<class 'numpy.ndarray'>

Knowing data is a numpy object i did the following: 知道数据是一个numpy对象我做了以下:

data = np.array(data)

i want to set the numbers inside a list i give as input to 0, what i tried: 我想设置列表中的数字我作为0的输入,我尝试了:

data[~np.isin(data,[2,4])] = 0

i expect all the 2 and 4 occurrences in the previous matrix to be 0 and the rest to keep their values, what i got: 我希望前一个矩阵中的所有2和4次出现都是0,剩下的就是保持它们的值,我得到了:

TypeError: only integer scalar arrays can be converted to a scalar index TypeError:只能将整数标量数组转换为标量索引

also tried to give data as a numpy array using np.array gave error as well. 还尝试使用np.array给出数据作为numpy数组np.array给出了错误。

You should not negate the mask from np.isin check if you intend to set those matching values to 0. The below code works just fine: 如果您打算将这些匹配值设置为0, np.isin应该从np.isin检查中否定掩码。以下代码可以正常工作:

Also, you should make the data a numpy array instead of list of lists. 此外,您应该使data成为一个numpy数组而不是列表列表。

In [10]: data = np.array([[0, 1, 1, 5, 5, 5, 0, 2, 2, 2, 2, 2, 2, 2, 6, 6, 6, 6, 6, 6, 6, 6],
    ...:         [1, 1, 1, 0, 5, 5, 5, 0, 2, 2, 0, 0, 2, 0, 0, 6, 6, 6, 0, 0, 6, 6],
    ...:         [1, 1, 1, 0, 0, 0, 0, 0, 2, 2, 0, 2, 2, 2, 0, 0, 2, 6, 0, 0, 6, 6]])
    ...:         

In [11]: data[np.isin(data, [2, 4])] = 0

In [12]: data
Out[12]: 
array([[0, 1, 1, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6, 6],
       [1, 1, 1, 0, 5, 5, 5, 0, 0, 0, 0, 0, 0, 0, 0, 6, 6, 6, 0, 0, 6, 6],
       [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 6, 6]])

Just to reproduce your error: 只是为了重现你的错误:

In [13]: data = [[0, 1, 1, 5, 5, 5, 0, 2, 2, 2, 2, 2, 2, 2, 6, 6, 6, 6, 6, 6, 6, 6],
    ...:         [1, 1, 1, 0, 5, 5, 5, 0, 2, 2, 0, 0, 2, 0, 0, 6, 6, 6, 0, 0, 6, 6],
    ...:         [1, 1, 1, 0, 0, 0, 0, 0, 2, 2, 0, 2, 2, 2, 0, 0, 2, 6, 0, 0, 6, 6]]
    ...:         

In [14]: data[np.isin(data, [2, 4])] = 0
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-06ee1662f1f2> in <module>()
----> 1 data[np.isin(data, [2, 4])] = 0

TypeError: only integer scalar arrays can be converted to a scalar index

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM