DilithiumMatrix
DilithiumMatrix

Reputation: 18677

one-dimensional array shapes (length,) vs. (length,1) vs. (length)

When I check the shape of an array using numpy.shape(), I sometimes get (length,1) and sometimes (length,). It looks like the difference is a column vs. row vector... but It doesn't seem like that changes anything about the array itself [except some functions complain when I pass an array with shape (length,1)].

What is the difference between these two?
Why isn't the shape just, (length)?

Upvotes: 18

Views: 21539

Answers (4)

Egret
Egret

Reputation: 417

A vector in Python is actually a two-dimensional array. It's just a coincidence that the number of rows is 1 (for row vectors), or the number of columns is 1 (for column vectors).

By contrast, a one-dimensional array is not a vector (neither a row vector nor a column vector). To understand this, think a concept in geometry, scalar. A scalar only has one attribute, which is numerical. By contrast, a vector has two attributes, number and direction. Fortunately, in linear algebra, vectors also have "directions", although only two possible directions - either horizontal or vertical (unlike infinite possible directions in geometry). A one-dimensional array only has numerical meaning - it doesn't show which direction this array is pointing to. This is why we need two-dimensional arrays to describe vectors.

Upvotes: 0

behzad.nouri
behzad.nouri

Reputation: 78041

The point is that say a vector can be seen either as

  • a vector
  • a matrix with only one column
  • a 3 dimensional array where the 2nd and 3rd dimensions have length one
  • ...

You can add dimensions using [:, np.newaxis] syntax or drop dimensions using np.squeeze:

>>> xs = np.array([1, 2, 3, 4, 5])
>>> xs.shape
(5,)
>>> xs[:, np.newaxis].shape  # a matrix with only one column
(5, 1)
>>> xs[np.newaxis, :].shape  # a matrix with only one row
(1, 5)
>>> xs[:, np.newaxis, np.newaxis].shape  # a 3 dimensional array
(5, 1, 1)
>>> np.squeeze(xs[:, np.newaxis, np.newaxis]).shape
(5,)

Upvotes: 11

hpaulj
hpaulj

Reputation: 231738

In Python, (length,) is a tuple, with one 1 item. (length) is just parenthesis around a number.

In numpy, an array can have any number of dimensions, 0, 1, 2, etc. You are asking about the difference between 1 and 2 dimensional objects. (length,1) is a 2 item tuple, giving you the dimensions of a 2d array.

If you are used to working with MATLAB, you might be confused by the fact that there, all arrays are 2 dimensional or larger.

Upvotes: 6

Eric Suchyta
Eric Suchyta

Reputation: 43

The (length,) array is an array where each element is a number and there are length elements in the array. The (length, 1) array is an array which also has length elements, but each element itself is an array with a single element. For example, the following uses length=3.

>>> import numpy as np
>>> a = np.array( [[1],[2],[3]] )
>>> a.shape
>>> (3, 1)
>>> b = np.array( [1,2,3] )
>>> b.shape
>>> (3,)

Upvotes: 2

Related Questions