0

Here's what I think axis is and want to know if my understanding is correct

We count opening bracket [ from left indexed from 0 and that's the axis

eg1) [[1,2],[3,4]]

for [ find all elements that has one [ left to it, are the elements for the axis 0. ([[ for axis 1 and so on)

0 axis: you see `[`: [x, y] where x = [1,2], y=[3,4]
1 axis: you see `[[`: [[x, y]] where x = [1,3], y = [2,4]

eg2) [[[1,2,3], [4,5,6]], [[7,8,9], [10,11,12]]]

0 axis: you see `[` [x, y] where x = [[1,2,3], [4,5,6]], y= [[7,8,9], [10,11,12]]
1 axis: you see `[[`,  x = [1,2,3], [7,8,9] y = [4,5,6], [10,11,12]
2 axis: you see `[[[`, x = [1,4,7,10] y = [2,5,8,11] z = [3,6,9,12]

If there's a function that takes a value along the axis, I could verify if I'm right, but.. closest thing I found was np.take

Paritosh Singh
  • 6,034
  • 2
  • 14
  • 33
eugene
  • 39,839
  • 68
  • 255
  • 489
  • Does this post help in any way? https://stackoverflow.com/questions/17079279/how-is-axis-indexed-in-numpys-array – rayryeng Mar 28 '19 at 16:04
  • 1
    your understand looks spot on, although you focus on the `[[` symbols too much. A better way is to look at the `.shape` of a numpy array. The left/outermost axis is always 0, and it increases to the right/inwards. – Paritosh Singh Mar 28 '19 at 16:10
  • In the last case I expect `x=[[1,4],[7,10]]`. That is `arr[:,:,i]` from a (2,2,3) array. – hpaulj Mar 28 '19 at 16:26

1 Answers1

0

For the (2,2) shape array:

In [13]: arr = np.array([[1,2],[3,4]])                                          
In [14]: arr                                                                    
Out[14]: 
array([[1, 2],
       [3, 4]])

Python unpacking just iterates on the array - that is on the first dimension, 'rows':

In [15]: x, y = arr                                                             
In [16]: x,y                                                                    
Out[16]: (array([1, 2]), array([3, 4]))

To unpack columns, we could transpose the array, so the 2nd dimension is first. But I think a list comprehension is clearer:

In [17]: x, y = [arr[:,i] for i in range(2)]                                    
In [18]: x,y                                                                    
Out[18]: (array([1, 3]), array([2, 4]))

For the 3d array:

In [19]: arr = np.arange(1,13).reshape(2,2,3)                                   
In [20]: arr                                                                    
Out[20]: 
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])

The results of iterating on the first dimension should be obvious - the 2 blocks or panes.

You got the 2nd dimension right. For the third, the results are 3 (2,2) arrays:

In [21]: x,y,z=[arr[:,:,i] for i in range(arr.shape[2])]                        
In [22]: x                                                                      
Out[22]: 
array([[ 1,  4],
       [ 7, 10]])
In [23]: y                                                                      
Out[23]: 
array([[ 2,  5],
       [ 8, 11]])
In [24]: z                                                                      
Out[24]: 
array([[ 3,  6],
       [ 9, 12]])

x,y,z= from this transpose would also work:

In [25]: arr.transpose(2,0,1)                                                   
Out[25]: 
array([[[ 1,  4],
        [ 7, 10]],

       [[ 2,  5],
        [ 8, 11]],

       [[ 3,  6],
        [ 9, 12]]])

np.take can be used like my indexing:

[np.take(arr,i,2) for i in range(3)]
hpaulj
  • 221,503
  • 14
  • 230
  • 353