parastoo91
parastoo91

Reputation: 151

What ndarray.shape returns?

I am creating an 3 dimensional array.

three = np.array([[(1, 2),(4, 5),(7, 8)],
                  [(1, 2),(4, 5),(7, 8)],
                  [(1, 2),(4, 5),(7, 8)],
                  [(1, 2),(4, 5),(7, 8)]], dtype=int)

I want to check the output of three.shape but I get this: (4,3,2) which seems to be (Z, X, Y) and not the convention of Numpy which is (X,Y,Z).

I don't understand why I get this weird output. I appreciate an explanation about it.

Upvotes: 0

Views: 57

Answers (3)

James
James

Reputation: 36608

Numpy reads in your list of lists of tuples from the outside in. The outer most list contains 4 elements. Those will each be a "row", or your X dimension.

[ row
  row
  row
  row ]

Each "row" contains 3 tuples, those are the "columns", or your Y dimension.

[ row( col, col, col )
  row( col, col, col )
  row( col, col, col )
  row( col, col, col ) ]

And so forth for the 2 elements in the tuple.

You should expect it be (4,3,2), as that is what you have given to numpy.

Upvotes: 3

Shyryu
Shyryu

Reputation: 149

Every list in your list is a row and the tuples in them the columns. 4,3,2 means It is a 4 rows,3 columns and it is 2 dimensional array

Upvotes: 0

Haiz
Haiz

Reputation: 134

My understanding: your top level has 4 elements of [(1,2),(4,5),(7,8)], next level has three elements: (1,2),(4,5), and (7,8), the next level has 2 elements: 1,2, or 4,5, or 7,8. So the shape is (4,3,2)

Upvotes: 0

Related Questions