用两个数组的矩阵索引NumPy数组。

huangapple go评论60阅读模式
英文:

Index numpy array by matrix of two arrays

问题

You can achieve the desired indexing into the matrix using NumPy like this:

result = weights[positions1[:, np.newaxis], positions2]

This will give you a result similar to the matrix you described:

array([[8, 8, 7],
       [5, 5, 4],
       [1, 1, 0]])

Each element of result corresponds to the values obtained by indexing weights with the combinations of positions1 and positions2 as specified in your question.

英文:

I have a 2D numpy array like

weights = np.array(
    [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9],
    ]
)

And i have two 1-D numpy arrays that i want to use to index into weights

positions1 = np.array([2, 1, 0])
positions2 = np.array([1, 1, 0])

If i want the results for stepping through the arrays together and indexing into the matrix that way i can do

print(weights[positions1[...], positions2[...]])

And get [8 5 1]

However now i want to index into the matrix with all possible combinations of positions1 and positions2 so that i get a matrix like

[
[weights[2, 1], weights[2, 1], weights[2, 0]],
[weights[1, 1], weights[1, 1], weights[1, 0]],
[weights[0, 1], weights[0, 1], weights[0, 0]],
]

So

[
[weights[pos1[0], pos2[0]], weights[pos1[0], pos2[1]], weights[pos1[0], pos2[2]]],
[weights[pos1[1], pos2[0]], weights[pos1[1], pos2[1]], weights[pos1[1], pos2[2]]],
[weights[pos1[2], pos2[0]], weights[pos1[2], pos2[1]], weights[pos1[2], pos2[2]]],
]

What would be the canonical way to do that in numpy?
I know that this is kinda like an outer product but i dont actually want to multiply the values in my array but just get a tuple of their values to index into the matrix with.

答案1

得分: 2

需要更改第一个索引器的形状:

weights[positions1[:, None], positions2]

或者对于通用版本,如Chrysophylaxs所指出的:

weights[np.ix_[positions1, positions2]]

输出:

array([[8, 8, 7],
       [5, 5, 4],
       [2, 2, 1]])
英文:

You need change the shape of the first indexer:

weights[positions1[:,None], positions2]

Or for a generalized version, as pointed out by Chrysophylaxs:

weights[np.ix_[positions1, positions2]]

Output:

array([[8, 8, 7],
       [5, 5, 4],
       [2, 2, 1]])

答案2

得分: 1

另一种可能的解决方案:

weights[*np.meshgrid(positions1, positions2, indexing='ij')]

如果上述在您的Python版本上不起作用,您可以使用@mozway建议的方法:

weights[tuple(np.meshgrid(positions1, positions2, indexing='ij'))]

输出:

array([[8, 8, 7],
       [5, 5, 4],
       [2, 2, 1]])
英文:

Another possible solution:

weights[*np.meshgrid(positions1, positions2, indexing='ij')]

If the above does not work on your Python version, you can use what @mozway suggests:

weights[tuple(np.meshgrid(positions1, positions2, indexing='ij'))]

Output:

array([[8, 8, 7],
       [5, 5, 4],
       [2, 2, 1]])

huangapple
  • 本文由 发表于 2023年5月11日 01:10:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定