Reputation: 41
I'm new to numpy & have a question about it :
according to docs.scipy.org, the "shape" method is "the dimensions of the array. For a matrix with n rows and m columns, shape will be (n,m)"
Suppose I am to create a simple array as below:
np.array([[0,2,4],[1,3,5]])
Using the "shape" method, it returns (2,3) (i.e. the array has 2 rows & 3 columns)
However, for an array ([0,2,4]), the shape method would return (3,) (which means it has 3 rows according to the definition above)
I'm confused : the array ([0,2,4]) should have 3 columns not 3 rows so I expect it to return (,3) instead.
Can anyone help to clarify ? Thanks a lot.
Upvotes: 2
Views: 1880
Reputation: 22954
Numpy supports not only 2-dimensional arrays, but multi-dimensional arrays, and by multi-dimension I mean 1-D, 2-D, 3-D .... n-D, And there is a format for representing respective dimension array. The len
of array.shape
would get you the number of dimensions of that array. If the array is 1-D, the there is no need to represent as (m, n)
or if the array is 3-D then it (m, n)
would not be sufficient to represent its dimensions.
So the output of array.shape
would not always be in (m, n)
format, it would depend upon the array itself and you will get different outputs for different dimensions.
Upvotes: 1
Reputation: 3208
This is just notation - in Python, tuples are distinguished from expression grouping (or order of operations stuff) by the use of commas - that is, (1,2,3)
is a tuple and (2x + 4) ** 5
contains an expression 2x + 4
. In order to keep single-element tuples distinct from single-element expressions, which would otherwise be ambiguous ((1)
vs (1)
- which is the single-element tuple and which a simple expression that evaluates to 1
?), we use a trailing comma to denote tuple-ness.
What you're getting is a single dimension response, since there's only one dimension to measure, packed into a tuple
type.
Upvotes: 4