Senthil Kumaran
Senthil Kumaran

Reputation: 56951

numpy assign values from another array using indices

I have this points 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.

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?

Upvotes: 0

Views: 359

Answers (1)

Crispin
Crispin

Reputation: 2120

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

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?

Upvotes: 1

Related Questions