[英]Is there a numpy way to filter an array of arrays by a list of indices
假设我有 numpy 数组x
其中x
是
x = np.array(
[
[1, 2],
[3, 4]
]
)
我还有一个索引列表i
其中i = [0, 1]
。 我想为x
中的每个n
获取索引n[i]
处的值的数组。
最佳 output 是
np.array([1, 4])
您可以使用索引:
x[np.arange(x.shape[0]), i]
output: array([1, 4])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.