numpy使用索引从另一个数组中分配值

时间:2023-01-25 13:37:23

I have this points ndarray.

我有这点ndarray。

In [141]: points
Out[141]:
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7]])

And I have this classifier that I classified the points into two classes

我有这个分类器,我将这些分类分为两类

In [142]: i5
Out[142]: array([0, 0, 1, 1])

Note, the length of points and i5 are same.

注意,点和i5的长度是相同的。

I know that the two classes have these values.

我知道这两个类都有这些值。

In [143]: c
Out[143]:
array([[2, 3],
       [4, 5]])

I want to assign the points to the values that I have classified. The final result that i expect is

我想将点分配给我已分类的值。我期望的最终结果是

In [141]: points
Out[141]:
array([[2, 3],
       [2, 3],
       [4, 5],
       [4, 5]])

How can I mutate/change points based on c indexed on i5?

如何基于i5索引的c变异/更改点?

1 个解决方案

#1


1  

Just use i5 as an index array on c and assign the indexed view on c to points

只需在c上使用i5作为索引数组,并将c上的索引视图分配给点

import numpy as np

c = np.array([[2, 3],
              [4, 5]])

i5 = np.array([0, 0, 1, 1])

points = c[i5]

# [[2 3]
#  [2 3]
#  [4 5]
#  [4 5]]

Note: Based on how you've described the problem, the initial value of points doesn't appear to matter. Is that an appropriate conclusion to derive?

注意:根据您描述问题的方式,点的初始值似乎并不重要。这是一个合适的结论吗?

#1


1  

Just use i5 as an index array on c and assign the indexed view on c to points

只需在c上使用i5作为索引数组,并将c上的索引视图分配给点

import numpy as np

c = np.array([[2, 3],
              [4, 5]])

i5 = np.array([0, 0, 1, 1])

points = c[i5]

# [[2 3]
#  [2 3]
#  [4 5]
#  [4 5]]

Note: Based on how you've described the problem, the initial value of points doesn't appear to matter. Is that an appropriate conclusion to derive?

注意:根据您描述问题的方式,点的初始值似乎并不重要。这是一个合适的结论吗?