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)
?
The point is that say a vector can be seen either as
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,)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With