numpy array question
Peter Otten
__peter__ at web.de
Thu Apr 2 11:02:38 EDT 2020
jagmit sandhu wrote:
> python newbie. I can't understand the following about numpy arrays:
>
> x = np.array([[0, 1],[2,3],[4,5],[6,7]])
> x
> array([[0, 1],
> [2, 3],
> [4, 5],
> [6, 7]])
> x.shape
> (4, 2)
> y = x[:,0]
> y
> array([0, 2, 4, 6])
> y.shape
> (4,)
>
> Why is the shape for y reported as (4,) ? I expected it to be a (4,1)
> array. thanks in advance
Why do you expect every array to be a 2D matrix? Why not 3D or 4D? In the
shape of y the 4 could be followed by an infinite amount of ones
(4, 1, 1, 1, 1, 1,...)
Instead numpy uses as many dimensions as you provide:
>>> import numpy as np
>>> np.array(42).shape
()
>>> np.array([42]).shape
(1,)
>>> np.array([[42]]).shape
(1, 1)
>>> np.array([[[42]]]).shape
(1, 1, 1)
I think this is a reasonable approach. Dont you?
More information about the Python-list
mailing list