0

I have a Numpy array called z:

pop = np.random.randint(4, size = (3, 3, 1, 5))
z = pop.reshape(tuple(d for d in pop.shape if d > 1))

enter image description here

Using the following code, I manage to print it like this:

for x in z:
   for y in x:
       print(y, end=' ')
   print()

enter image description here

But now I need vertically each individual element of the array, for demonstration purposes, something like this:

enter image description here

Thanks for any advice.

6
  • try to transpose the array before print it, if you want use the same method: try z.transpose(). Also it is better if you actually paste the array rather than the screenshot of it, so that we can easily try the code out. Commented May 7, 2020 at 20:41
  • What's wrong with the default numpy display? In the long run I think it will be easier to adjust to that display, rather than trying to come up with your own ordering. The purpose of such a display is to give you a basic idea of the array's structure (especially as expressed by the shape). It isn't meant to show all values of a large array. Nor is it meant to be machine-readable. There are better ways of saving an array for later use. Commented May 7, 2020 at 20:50
  • @hadik Thanks, I just posted the array code Commented May 7, 2020 at 20:54
  • @hpaulj There is nothing wrong with the default display. I am working on a simulation model and for visual proposes I need that display style. Commented May 7, 2020 at 20:55
  • "each element vertically, each matrix/array as a vertical matrix (column vector)." is confusing. An element of your original array is a single number. In numpy a 'column vector' is a (n,1) array. I don't see how that applies to an array with shape: (3, 3, 1, 5). Keep in mind that a layout that looks nice when shapes are (3,3), may be overly wide when shapes are (10,10) or larger. Commented May 7, 2020 at 21:11

2 Answers 2

1
In [277]: pop = np.arange(9*5).reshape(3,3,1,5)                                                        
In [278]: pop                                                                                          
Out[278]: 
array([[[[ 0,  1,  2,  3,  4]],

        [[ 5,  6,  7,  8,  9]],

        [[10, 11, 12, 13, 14]]],


       [[[15, 16, 17, 18, 19]],

        [[20, 21, 22, 23, 24]],

        [[25, 26, 27, 28, 29]]],


       [[[30, 31, 32, 33, 34]],

        [[35, 36, 37, 38, 39]],

        [[40, 41, 42, 43, 44]]]])

Swap the last two axes will give the required 'column vectors':

In [279]: pop.transpose(0,1,3,2)                                                                       
Out[279]: 
array([[[[ 0],
         [ 1],
         [ 2],
         [ 3],
         [ 4]],

        [[ 5],
         [ 6],
         [ 7],
         [ 8],
         [ 9]],

        [[10],
         [11],
         [12],
         [13],
         [14]]],


       [[[15],
         [16],
         [17],
         [18],
         [19]],

        [[20],
         [21],
         [22],
         [23],
         [24]],

        [[25],
         [26],
         [27],
         [28],
         [29]]],


       [[[30],
         [31],
         [32],
         [33],
         [34]],

        [[35],
         [36],
         [37],
         [38],
         [39]],

        [[40],
         [41],
         [42],
         [43],
         [44]]]])

Then with a word processing block move, rearrange the blocks.

Another transpose and reshape would order the values in the desired way, but the brackets would not be what you want:

In [281]: pop.transpose(0,3,2,1).reshape(3,5,3)                                                        
Out[281]: 
array([[[ 0,  5, 10],
        [ 1,  6, 11],
        [ 2,  7, 12],
        [ 3,  8, 13],
        [ 4,  9, 14]],

       [[15, 20, 25],
        [16, 21, 26],
        [17, 22, 27],
        [18, 23, 28],
        [19, 24, 29]],

       [[30, 35, 40],
        [31, 36, 41],
        [32, 37, 42],
        [33, 38, 43],
        [34, 39, 44]]])
Sign up to request clarification or add additional context in comments.

1 Comment

This does the trick, now I'll just try to add horizontal spaces between each column so that it can be viewed better, thanks!
0

You can simply swap axis you want before printing:

z = np.swapaxes(z, 1, 0)
for x in z:
   for y in x:
       print(y, end=' ')
   print()

output:

z = np.arange(45).reshape(3,3,5)

#print(z)
[[[ 0  1  2  3  4]
  [ 5  6  7  8  9]
  [10 11 12 13 14]]

 [[15 16 17 18 19]
  [20 21 22 23 24]
  [25 26 27 28 29]]

 [[30 31 32 33 34]
  [35 36 37 38 39]
  [40 41 42 43 44]]]

#suggested code:
[0 1 2 3 4] [15 16 17 18 19] [30 31 32 33 34] 
[5 6 7 8 9] [20 21 22 23 24] [35 36 37 38 39] 
[10 11 12 13 14] [25 26 27 28 29] [40 41 42 43 44] 

EDIT: per edit in question:

z = np.transpose(z,(0,2,1))
for x in z:
  for y in x:
   print(*y, end=' ')
   print()

output:

0 5 10 
1 6 11 
2 7 12 
3 8 13 
4 9 14 
15 20 25 
16 21 26 
17 22 27 
18 23 28 
19 24 29 
30 35 40 
31 36 41 
32 37 42 
33 38 43 
34 39 44

7 Comments

maybe I don't explain it very well, I need each of the nine elements in the same position but vertically oriented
I just add a new image of what I need
@PakinJa Your printed output is different than your array. I am not sure what it represents. Mind replacing it with a corresponding array representation of your desire?
I just changed the last image to avoid confusion
@PakinJa Thank you for editing the post. However, it is still confusing and unclear. The best way would be to fill in the dots with numbers of your example array you have in your question. Thank you.
|

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.