我有一个numpy的2-D数组,行作为观察值,列作为协变量。我想选择与最后n列的指定示例匹配的行。例如,n = 2:
A = [[0,1,0],[3,0,1],[5,1,0]]
与target=[1,0]
将返回B = [[0,1,0],[5,1,0]]
。
import numpy as np
A = np.array([[0,1,0],[3,0,1],[5,1,0]])
target = [1,0]
B = A[(A[:, -len(target):] == target).all(axis=1)]
print(B)
# [[0 1 0]
# [5 1 0]]
说明
print(A[:, -len(target):])
# [[1 0]
# [0 1]
# [1 0]]
print(A[:, -len(target):] == target)
# [[ True True]
# [False False]
# [ True True]]
print((A[:, -len(target):] == target).all(axis=1))
# [ True False True]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句