Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 583
DilithiumMatrix Avatar asked Mar 29 '14 21:03

DilithiumMatrix


1 Answers

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,)
like image 121
behzad.nouri Avatar answered Sep 29 '22 13:09

behzad.nouri