8

Why the output here

array = np.arange(3)
array.shape

is

(3,)

and not

(1,3)

What does the missing dimension means or equals?

11
  • 1
    @NaN the reshape will change it to (1, 3), but why the original array is not (1,3)? Commented Jul 14, 2018 at 0:45
  • 1
    That is covered in the docs Commented Jul 14, 2018 at 0:49
  • 1
    You are creating a vector which by definition is only one dimension, not two. Commented Jul 14, 2018 at 0:59
  • 1
    Or, is simply tautological, equivalent to "your array is one dimensional because you created a one dimensional array" which wouldn't be wrong Commented Jul 14, 2018 at 1:34
  • 1
    @juanpa.arrivillaga yes, it is tautological as what they are asking boils down to "why does the one dimensional array that I have created only have one dimension?" Commented Jul 14, 2018 at 3:21

1 Answer 1

7

In case there's confusion, (3,) doesn't mean there's a missing dimension. The comma is part of the standard Python notation for a single element tuple. Shapes (1,3), (3,), and (3,1) are distinct,

While they can contain the same 3 elements, their use in calculations (broadcasting) is different, their print format is different, and their list equivalent is different:

In [21]: np.array([1,2,3])
Out[21]: array([1, 2, 3])
In [22]: np.array([1,2,3]).tolist()
Out[22]: [1, 2, 3]
In [23]: np.array([1,2,3]).reshape(1,3).tolist()
Out[23]: [[1, 2, 3]]
In [24]: np.array([1,2,3]).reshape(3,1).tolist()
Out[24]: [[1], [2], [3]]

And we don't have to stop at adding just one singleton dimension:

In [25]: np.array([1,2,3]).reshape(1,3,1).tolist()
Out[25]: [[[1], [2], [3]]]
In [26]: np.array([1,2,3]).reshape(1,3,1,1).tolist()
Out[26]: [[[[1]], [[2]], [[3]]]]

In numpy an array can have 0, 1, 2 or more dimensions. 1 dimension is just as logical as 2.

In MATLAB a matrix always has 2 dim (or more), but it doesn't have to be that way. Strictly speaking MATLAB doesn't even have scalars. An array with shape (3,) is missing a dimension only if MATLAB is taken as the standard.

numpy is built on Python which as scalars, and lists (which can nest). How many dimensions does a Python list have?

If you want to get into history, MATLAB was developed as a front end to a set of Fortran linear algebra routines. Given the problems those routines solved the concept of matrix with 2 dimensions, and row vs column vectors made sense. It wasn't until version 3.something that MATLAB was generalized to allow more than 2 dimensions (in the late 1990s).

numpy is based on several attempts to provide arrays to Python (e.g. numeric). Those developers took a more general approach to arrays, one where 2d was an artificial constraint. That has precedence in computer languages and mathematics (and physics). APL was developed in the 1960s, first as a mathematical notation, and then as a computer language. Like numpy its arrays can be 0d or higher. (Since I used APL before I used MATLAB, the numpy approach feels quite natural.)


In APL there aren't separate lists or tuples. So the shape of an array, rho A is itself an array, and rho rho A is the number of dimensions of A, also called the rank.

http://docs.dyalog.com/14.0/Dyalog%20APL%20Idioms.pdf

Sign up to request clarification or add additional context in comments.

4 Comments

I would say Python has various containers, and the concept of scalars doesn't really apply. And lists don't have dimensions :)
This answer really isn't an answer to what they are saying and would be better served being a comment.
@David, I started to write a comment, and then decided it was too long for that. This issue has been hashed out many times, and probably should be closed as a duplicate, or may be a matter of opinion.
@David this answers the question, insofar as I can make sense of the OPs question

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.