问题很简单:我有两个2d np.array,我想得到一个第三个数组,它只包含与后两个不相同的行。
例如:
X = np.array([[0,1],[1,2],[4,5],[5,6],[8,9],[9,10]])
Y = np.array([[5,6],[9,10]])
Z = function(X,Y)
Z = array([[0, 1],
[1, 2],
[4, 5],
[8, 9]])
我尝试了np.delete(X,Y,axis=0)
,但它不起作用......
答案 0 :(得分:2)
Z = np.vstack(row for row in X if row not in Y)
答案 1 :(得分:1)
numpy_indexed包(免责声明:我是它的作者)将标准的numpy数组集操作扩展到这些多维用例,效率很高:
import numpy_indexed as npi
Z = npi.difference(X, Y)
答案 2 :(得分:0)
这是基于views
的方法 -
# Based on http://stackoverflow.com/a/41417343/3293881 by @Eric
def setdiff2d(a, b):
# check that casting to void will create equal size elements
assert a.shape[1:] == b.shape[1:]
assert a.dtype == b.dtype
# compute dtypes
void_dt = np.dtype((np.void, a.dtype.itemsize * np.prod(a.shape[1:])))
orig_dt = np.dtype((a.dtype, a.shape[1:]))
# convert to 1d void arrays
a = np.ascontiguousarray(a)
b = np.ascontiguousarray(b)
a_void = a.reshape(a.shape[0], -1).view(void_dt)
b_void = b.reshape(b.shape[0], -1).view(void_dt)
# Get indices in a that are also in b
return np.setdiff1d(a_void, b_void).view(orig_dt)
示例运行 -
In [81]: X
Out[81]:
array([[ 0, 1],
[ 1, 2],
[ 4, 5],
[ 5, 6],
[ 8, 9],
[ 9, 10]])
In [82]: Y
Out[82]:
array([[ 5, 6],
[ 9, 10]])
In [83]: setdiff2d(X,Y)
Out[83]:
array([[0, 1],
[1, 2],
[4, 5],
[8, 9]])
答案 3 :(得分:-1)
navigateToView(viewName:string) {
const { navigate } = this.props.navigation;
navigate(viewName);
}