我有一个 numpy 数组 y [1,2,3,4,5,6,1,2,3,4,5,6]
然后我有一个矩阵 X 是 numpy csr 格式。
1)我需要屏蔽y中的元素6。然后,我需要屏蔽 X 中的相应行。
所以,y.shape 是 12。应该是 10。X 是 12,20。应该是 10,20。
我如何在 numpy 中执行此操作
如果您的“掩码”输出y
应该小于您的输入,那么您并没有真正使用掩码。
正如您在上一个问题中所建议的那样,您可以很容易地找到与y
6 不同的索引
condition = (y != 6)
这是 abool 数组,您可以使用它来检索y
不是 6的值
y = y[condition]
您可以使用它condition
来获取 的相应行X
,除了它是 CSR 并且因此不支持花哨的索引格式。您仍然可以将其转换为 LIL 然后再返回。
您还可以获得y !=6
与
(indices,) = np.nonzero(y != 6)
它是一个常规整数数组,您可以使用它来索引您的X
.
我不喜欢回答自己的问题,但正确的解决方案是处理 csr 矩阵:
X = X[np.where(y != 6)[0]]
y = y[y != 6]
我不是 100% 确定我理解你的问题,但也许这会有所帮助:
>>> import numpy as np
>>> a = np.array(range(1,7)*2) #Your array.
>>> a
array([1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6])
>>> b = np.array(list(a)*20).reshape(12,20) #just some matrix of right size and shape. np.empty(12,20) would probably work just as well.
>>> b
array([[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2],
[3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4],
[5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2],
[3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4],
[5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2],
[3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4],
[5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2],
[3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4],
[5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]])
>>> mask = a != 6 #mask. True for all points except ones where value == 6.
>>> b[mask,:] #take points along first axis where mask==True, all points along second axis.
array([[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2],
[3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4],
[5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2],
[3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4],
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2],
[3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4],
[5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2],
[3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4]])
使用非零。
(假设 x 已经定义)
import numpy as np
y = np.array([1,2,3,4,5,6,1,2,3,4,5,6])
keepers = np.nonzero(y != 6)
y = y[keepers]
x = x[keepers, :]