我想用张量的特定分量制作一个数组。我发现了很棒的命令 np.argwhere()
。这将返回满足特定条件的张量的索引,但是它不会将它们命名为张量的分量,即它们返回为 [0,0,1,1]
与 x [0,0,1,1]
表示张量 x
。
是否有内置或灵巧的方法来获取满足特定条件的张量分量,其中分量写有它们的索引和附加的张量名称?
最佳答案
您可以使用 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])
关于python - 抓取附有张量名称的张量索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13919386/