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

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

Index numpy array by matrix of two arrays

问题

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

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

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

  1. array([[8, 8, 7],
  2. [5, 5, 4],
  3. [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

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

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

  1. positions1 = np.array([2, 1, 0])
  2. 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

  1. 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

  1. [
  2. [weights[2, 1], weights[2, 1], weights[2, 0]],
  3. [weights[1, 1], weights[1, 1], weights[1, 0]],
  4. [weights[0, 1], weights[0, 1], weights[0, 0]],
  5. ]

So

  1. [
  2. [weights[pos1[0], pos2[0]], weights[pos1[0], pos2[1]], weights[pos1[0], pos2[2]]],
  3. [weights[pos1[1], pos2[0]], weights[pos1[1], pos2[1]], weights[pos1[1], pos2[2]]],
  4. [weights[pos1[2], pos2[0]], weights[pos1[2], pos2[1]], weights[pos1[2], pos2[2]]],
  5. ]

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

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

  1. weights[positions1[:, None], positions2]

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

  1. weights[np.ix_[positions1, positions2]]

输出:

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

You need change the shape of the first indexer:

  1. weights[positions1[:,None], positions2]

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

  1. weights[np.ix_[positions1, positions2]]

Output:

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

答案2

得分: 1

另一种可能的解决方案:

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

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

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

输出:

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

Another possible solution:

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

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

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

Output:

  1. array([[8, 8, 7],
  2. [5, 5, 4],
  3. [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:

确定