Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The order of axis when printing a NumPy array

I am working with 3d arrays in NumPy, and I have to look at the array elements a lot. Say I have array a where a.shape is (10,5,3). I am using this is the sense of 3 planes of shape (10,5), and I would like to view it as such.

For example if I

print(a)

I get

[[[ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]]

 [[ 0  0  0]
  [ 0 22  0]
  [11 22 33]
  [ 0  0  0]
  [ 0  0  0]]

 [[ 0  0  0]
  [11 22 33]
  [ 0  0  0]
  [11 22 33]
  [ 0  0  0]]

 [[ 0  0  0]
  [11 22 33]
  [ 0  0  0]
  [11 22  0]
  [ 0  0  0]]

 [[ 0  0  0]
  [11 22 33]
  [11  0  0]
  [11 22  0]
  [ 0  0  0]]

 [[ 0  0  0]
  [11 22 33]
  [ 0 22  0]
  [11  0  0]
  [ 0  0  0]]

 [[ 0  0  0]
  [11 22 33]
  [ 0  0  0]
  [11 22  0]
  [ 0  0  0]]

 [[ 0  0  0]
  [11 22 33]
  [ 0  0  0]
  [11 22 33]
  [ 0  0  0]]

 [[ 0  0  0]
  [11 22  0]
  [ 0 22 33]
  [11 22  0]
  [ 0  0  0]]

 [[ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]
  [ 0  0  0]]]

Whereas if I iterate through axis 2 and print like this, it is displayed like I want it:

>>> for i in range(a.shape[2]):
...     print(str(a[:,:,i]) + "\n")
...
[[ 0  0  0  0  0]
 [ 0  0 11  0  0]
 [ 0 11  0 11  0]
 [ 0 11  0 11  0]
 [ 0 11 11 11  0]
 [ 0 11  0 11  0]
 [ 0 11  0 11  0]
 [ 0 11  0 11  0]
 [ 0 11  0 11  0]
 [ 0  0  0  0  0]]

[[ 0  0  0  0  0]
 [ 0 22 22  0  0]
 [ 0 22  0 22  0]
 [ 0 22  0 22  0]
 [ 0 22  0 22  0]
 [ 0 22 22  0  0]
 [ 0 22  0 22  0]
 [ 0 22  0 22  0]
 [ 0 22 22 22  0]
 [ 0  0  0  0  0]]

[[ 0  0  0  0  0]
 [ 0  0 33  0  0]
 [ 0 33  0 33  0]
 [ 0 33  0  0  0]
 [ 0 33  0  0  0]
 [ 0 33  0  0  0]
 [ 0 33  0  0  0]
 [ 0 33  0 33  0]
 [ 0  0 33  0  0]
 [ 0  0  0  0  0]]

Which makes a lot more sense in my mind.

So it seems like my intuition for which way the axes should be visualized is reversed, and my array a should be of the shape (3,5,10). However, I have a whole project going around arrays of a certain format so I can't really change it at this point. I think of the order of the axes as arbitrary, and that's why I feel kind of restricted by this. So is there some quick or even built-in way I can get NumPy to print the axes in reverse, or arbitrary order? I checked out the documentation for numpy.set_printoptions and found nothing of the sort. I realize printing is kind of trivial, but I want to get around all the slicing I have to do to get a good look at my data. Or maybe someone can explain why it's not completely arbitrary and maybe I should think about array axes in a different way.

like image 291
Self Dot Avatar asked Sep 06 '17 13:09

Self Dot


People also ask

What is the axis of a NumPy array?

NumPy axes are the directions along the rows and columns. Just like coordinate systems, NumPy arrays also have axes. In a 2-dimensional NumPy array, the axes are the directions along the rows and columns.

What is order in NumPy array?

order : {'C', 'F', 'A'}, optional Specify the order of the array. If order is 'C' (default), then the array will be in C-contiguous order (last-index varies the fastest). If order is 'F', then the returned array will be in Fortran-contiguous order (first-index varies the fastest).

What is the axis of an array?

Axes are defined for arrays with more than one dimension. A 2-dimensional array has two corresponding axes: the first running vertically downwards across rows (axis 0), and the second running horizontally across columns (axis 1).

How do NumPy axes work?

Numpy Axis in Python for SumWhen we use the numpy sum() function on a 2-d array with the axis parameter, it collapses the 2-d array down to a 1-d array. It collapses the data and reduces the number of dimensions. But which axis will collapse to return the sum depends on whether we set the axis to 0 or 1.


1 Answers

Unfortunately that is something you can't just do with a simple switch in NumPys print-options. You really have to reorder the axes to get the desired output (which is pretty easy).

If it's just for printing, you can simply define a thin wrapper around the print function:

import numpy as np

def my_numpy_print(arr):
    if arr.ndim == 3:
        print(arr.transpose(2, 0, 1))
    else:  # no idea how you want it to be displayed for different dimensional arrays.
        print(arr)

arr = ...
my_numpy_print(arr)

Output:

[[[ 0  0  0  0  0]
  [ 0  0 11  0  0]
  [ 0 11  0 11  0]
  [ 0 11  0 11  0]
  [ 0 11 11 11  0]
  [ 0 11  0 11  0]
  [ 0 11  0 11  0]
  [ 0 11  0 11  0]
  [ 0 11  0 11  0]
  [ 0  0  0  0  0]]

 [[ 0  0  0  0  0]
  [ 0 22 22  0  0]
  [ 0 22  0 22  0]
  [ 0 22  0 22  0]
  [ 0 22  0 22  0]
  [ 0 22 22  0  0]
  [ 0 22  0 22  0]
  [ 0 22  0 22  0]
  [ 0 22 22 22  0]
  [ 0  0  0  0  0]]

 [[ 0  0  0  0  0]
  [ 0  0 33  0  0]
  [ 0 33  0 33  0]
  [ 0 33  0  0  0]
  [ 0 33  0  0  0]
  [ 0 33  0  0  0]
  [ 0 33  0  0  0]
  [ 0 33  0 33  0]
  [ 0  0 33  0  0]
  [ 0  0  0  0  0]]]
like image 120
MSeifert Avatar answered Oct 26 '22 20:10

MSeifert