4

I need to work with this beautiful np array

import numpy as np
train_predicteds = np.asarray([ 
 [[0.1, 0.2, 0.3], [0.5, 0.6, 0.7], [0.7, 0.8, 0.9]],
 [[0.3, 0.1, 0.4], [0.4, 0.5, 0.6], [0.5, 0.6, 0.1]]])

Now I want to get the elements in this fashion:

[[0.1, 0.3], [0.2, 0.1], [0.3, 0.4],
 [0.5, 0.4], [0.6, 0.5], [0.7, 0.6],
 [0.7, 0.5], [0.8, 0.6], [0.9, 0.1]]

Some sort of solution I found was using these two lines:

aux = [item[0] for item in train_predicteds]
x = [item[0] for item in aux]

Which produces me x equals to

[0.10000000000000001, 0.30000000000000001]

But I can't merge these two lines in a single one, is it possible? Or is there a better pythonic solution?

Thanks guys

3 Answers 3

4

Starting with:

In [17]: arr = np.asarray([  
    ...:  [[0.1, 0.2, 0.3], [0.5, 0.6, 0.7], [0.7, 0.8, 0.9]], 
    ...:  [[0.3, 0.1, 0.4], [0.4, 0.5, 0.6], [0.5, 0.6, 0.1]]])                 
In [18]: arr                                                                    
Out[18]: 
array([[[0.1, 0.2, 0.3],
        [0.5, 0.6, 0.7],
        [0.7, 0.8, 0.9]],

       [[0.3, 0.1, 0.4],
        [0.4, 0.5, 0.6],
        [0.5, 0.6, 0.1]]])
In [19]: arr.shape                                                              
Out[19]: (2, 3, 3)

After trying several transpose orders I got:

In [26]: arr.transpose(1,2,0)        # shape (3,3,2) moves 1st dim to end                                          
Out[26]: 
array([[[0.1, 0.3],
        [0.2, 0.1],
        [0.3, 0.4]],

       [[0.5, 0.4],
        [0.6, 0.5],
        [0.7, 0.6]],

       [[0.7, 0.5],
        [0.8, 0.6],
        [0.9, 0.1]]])

The first two dimensions can be merged with reshape:

In [27]: arr.transpose(1,2,0).reshape(9,2)                                      
Out[27]: 
array([[0.1, 0.3],
       [0.2, 0.1],
       [0.3, 0.4],
       [0.5, 0.4],
       [0.6, 0.5],
       [0.7, 0.6],
       [0.7, 0.5],
       [0.8, 0.6],
       [0.9, 0.1]])
Sign up to request clarification or add additional context in comments.

Comments

4

The better Pythonic solution

>>> train_predicteds[:,0,0]
array([0.1, 0.3])

3 Comments

I think OP was trying to recreate the entire array from his second code block though, and was getting caught stumped how to expand his algorithm from calculating one item to the full thing. Perhaps I misunderstood though
I was able to reconstruct element by element with my "sort of a solution", but I wanted something more stylish.
@GabrielPellegrino You should consider to accept hpaulj solution instead, it's much more "numpythonic" than the other 2 answers here.
2

You can do this with a simple loop comprehension:

import numpy as np
train_predicteds = np.asarray([ 
 [[0.1, 0.2, 0.3], [0.5, 0.6, 0.7], [0.7, 0.8, 0.9]],
 [[0.3, 0.1, 0.4], [0.4, 0.5, 0.6], [0.5, 0.6, 0.1]]])

result = [list(train_predicteds[:, i, j]) for i in range(3) for j in range(3)]

Output:

[[0.1, 0.3], [0.2, 0.1], [0.3, 0.4],
 [0.5, 0.4], [0.6, 0.5], [0.7, 0.6],
 [0.7, 0.5], [0.8, 0.6], [0.9, 0.1]]

UPDATE:

Thanks to Reedinationer for pointing it out

Or if you prefer a more generalized form:

result = [list(train_predicteds[:, i, j]) for i in range(len(train_predicteds[0])) for j in range(len(train_predicteds[0, 0]))]

7 Comments

Oush! That's nice =) Thanks, mylord!
OP may have wanted something more general like result = [list(train_predicteds[:, x, y]) for x in range(len(train_predicteds) + 1) for y in range(len(train_predicteds[0]))], but this output is clean! Good job.
@Reedinationer You're right. But in my experience, usually the straight answer that solves the specific problem is more wanted
@Reedinationer, your solution had problems with this input. train_predicteds = np.asarray([ ... [[0.1, 0.2, 0.3], [0.5, 0.6, 0.7], [0.7, 0.8, 0.9]], ... [[0.3, 0.1, 0.4], [0.4, 0.5, 0.6], [0.5, 0.6, 0.1]], ... [[0.3, 0.1, 0.4], [0.4, 0.5, 0.6], [0.5, 0.6, 0.1]], ... [[0.3, 0.1, 0.4], [0.4, 0.5, 0.6], [0.5, 0.6, 0.1]], ... [[0.3, 0.1, 0.4], [0.4, 0.5, 0.6], [0.5, 0.6, 0.1]]])
@Reedinationer Thanks, I fixed the small problem in your answer and added it to mine
|

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.