Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想用张量的特定分量制作一个数组。我找到了绝妙的命令np.argwhere()。这将返回满足特定标准的张量的索引,但是它没有将它们命名为张量的组件,即它们[0,0,1,1]与x[0,0,1,1]张量相比返回x。
np.argwhere()
[0,0,1,1]
x[0,0,1,1]
x
是否有一种内置或巧妙的方法来获取满足特定标准的张量组件,其中组件用它们的索引和附加的张量名称编写?
您可以使用where而不是argwhere...
where
argwhere
>>> x = np.arange(6).reshape(2,3) >>> x array([[0, 1, 2], [3, 4, 5]]) >>> np.argwhere(x > 1) array([[0, 2], [1, 0], [1, 1], [1, 2]]) >>> np.where(x > 1) (array([0, 1, 1, 1]), array([2, 0, 1, 2])) >>> x[np.where(x > 1)] array([2, 3, 4, 5])