4

Assume I have the following array

test=np.asarray([
    [[ 0.26139668,  0.271985  ,  0.89647514,  0.10352486],
    [ 0.30488212,  0.31384717,  0.84089744,  0.15910256],
    [ 0.32112577,  0.32958562,  0.79216727,  0.20783273]],
    [[ 0.6017121 ,  0.60530397,  0.49432092,  0.50567908],
    [ 0.61610247,  0.61954059,  0.49649699,  0.50350301],
    [ 0.63149809,  0.63477652,  0.49945702,  0.50054298]],
    [[ 0.6017121 ,  0.60530397,  0.49432092,  0.50567908],
    [ 0.61610247,  0.61954059,  0.49649699,  0.50350301],
    [ 0.63149809,  0.63477652,  0.49945702,  0.50054298]],
    [[ 0.6017121 ,  0.60530397,  0.49432092,  0.50567908],
    [ 0.61610247,  0.61954059,  0.49649699,  0.50350301],
    [ 0.63149809,  0.63477652,  0.49945702,  0.50054298]]])

Now the result I am looking for are in the first column of each element from test. So basically I need test[0][:,0] and test[1][:,0] ... and finally test[3][:,0].

I am sure this can be done without a for loop, or not?

Since notation [:] should mean all elements from beginning till end I even tried test[:][:,0]. That didn't work. test[:,0][:,0] wasn't any better either.

So, what would be the alternative to a for loop?


The result should be:

    [0.26139668, 0.30488212, 0.32112577, 0.6017121, 0.61610247, 0.63149809,
 0.6017121, 0.61610247, 0.63149809,0.6017121, 0.61610247, 0.63149809]

Or anything similar to that. (I only need those numbers from the whole array).

2
  • 1
    show how should look the final result Commented Mar 28, 2017 at 19:15
  • @RomanPerekhrest OP edited Commented Mar 28, 2017 at 19:18

1 Answer 1

7

You have a 3D array so just pass the 0 to the third axis:

In [9]: test[:, :, 0]
Out[9]: 
array([[ 0.26139668,  0.30488212,  0.32112577],
       [ 0.6017121 ,  0.61610247,  0.63149809],
       [ 0.6017121 ,  0.61610247,  0.63149809],
       [ 0.6017121 ,  0.61610247,  0.63149809]])

If you want them all in one array you can just ravel the slice:

In [11]: test[:, :, 0].ravel()
Out[11]: 
array([ 0.26139668,  0.30488212,  0.32112577,  0.6017121 ,  0.61610247,
        0.63149809,  0.6017121 ,  0.61610247,  0.63149809,  0.6017121 ,
        0.61610247,  0.63149809])
Sign up to request clarification or add additional context in comments.

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.