2

I have a 2 numpy array something like this

a = [array([ 0.1,  0.1,  0.1]), array([ 0.2,  0.2,  0.2])]

b = [0 0 0 1]

What I want is something like this --

c = [[0.1,  0.1,  0.1],[0.1,  0.1,  0.1],[0.1,  0.1,  0.1],[0.2, 0.2, 0.2]]

i.e. elements of a based on index of b.

Is there a way I can achieve this using numpy and vectorization i.e. without looping over the values?

4 Answers 4

1

If you store a as a two-dimensional numpy array:

>>> a = np.array([[0.1, 0.1, 0.1], [0.2, 0.2, 0.2]])
# result: array([[ 0.1,  0.1,  0.1],
#                [ 0.2,  0.2,  0.2]])

or even convert a to a numpy array via a = np.array(a), then you can use the list b to access the elements as desired:

>>> b = [0,0,0,1]
>>> print(a[b])
array([[ 0.1,  0.1,  0.1],
       [ 0.1,  0.1,  0.1],
       [ 0.1,  0.1,  0.1],
       [ 0.2,  0.2,  0.2]])

and if you need a list as output then use tolist() method of the numpy arrays:

>>> (np.asarray(a)[b]).tolist()
[[0.1, 0.1, 0.1], [0.1, 0.1, 0.1], [0.1, 0.1, 0.1], [0.2, 0.2, 0.2]]
Sign up to request clarification or add additional context in comments.

Comments

0

list comprehension

[a[x].tolist() for x in b]

1 Comment

can you please expand a bit on this answer to help explain how this resolves the question so the asker can understand better for themselves for next time.
0
import numpy

a = [numpy.array([ 0.1,  0.1,  0.1]), numpy.array([ 0.2,  0.2,  0.2])]
b = [0, 0, 0, 1]

Alternative 1:

print([a[x].tolist() for x in b])

Output:

[[0.1, 0.1, 0.1], [0.1, 0.1, 0.1], [0.1, 0.1, 0.1], [0.2, 0.2, 0.2]]

Alternative 2:

print(numpy.array(a)[b])

Output:

[[ 0.1  0.1  0.1]
 [ 0.1  0.1  0.1]
 [ 0.1  0.1  0.1]
 [ 0.2  0.2  0.2]]

Alternative 3:

print(list(map(lambda i: a[i], b)))

Output:

[array([ 0.1,  0.1,  0.1]), array([ 0.1,  0.1,  0.1]), array([ 0.1,  0.1,  0.1]), array([ 0.2,  0.2,  0.2])]

Alternative 4:

from operator import itemgetter
print(list(itemgetter(*b)(a)))

Output:

[array([ 0.1,  0.1,  0.1]), array([ 0.1,  0.1,  0.1]), array([ 0.1,  0.1,  0.1]), array([ 0.2,  0.2,  0.2])]

2 Comments

a[b] will not work since "a" is a list in your case.
you are right, I appreciate your help. I have updated the answer.
0

Using numpy

If you want using numpy then:

print([a[i].tolist() for i in b])

Without using numpy :

import numpy as np
a = np.array([[0.1, 0.1, 0.1], [0.2, 0.2, 0.2]])
b = [0,0,0,1]

print([value_1.tolist() for value in b for index,value_1 in enumerate(a) if index==value])

above list comprehension is same as :

final=[]
for value in b:
    for index,value_1 in enumerate(a):
        if index==value:
            final.append(value_1.tolist())

print(final)

output:

[[0.1, 0.1, 0.1], [0.1, 0.1, 0.1], [0.1, 0.1, 0.1], [0.2, 0.2, 0.2]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.