[Tutor] difference between array([1,0,1]) and array([[1,0,1]])

Steven D'Aprano steve at pearwood.info
Fri Jun 21 21:31:57 EDT 2019


On Thu, Jun 20, 2019 at 08:39:35PM -0300, Markos wrote:
> Hi,
> 
> I'm studying Numpy and I don't understand the difference between
> 
> >>>vector_1 = np.array( [ 1,0,1 ] )
> 
> with 1 bracket and
> 
> >>>vector_2 = np.array( [ [ 1,0,1 ] ] )
> 
> with 2 brackets

I'm not really sure what you don't get here. The first is a one 
dimensional vector with three items, the second is a two dimensional 
array with one row and three columns. But you know that, because you 
checked the shape of each one:

> >>>vector_1.shape
> (3,)

> >>>vector_2.shape
> (1, 3)

If you want a 2D shape, you need to provide a 2D argument. If you want a 
3D shape, you need a 3D argument. And so forth.


> I thought that both vectors would be treated as an matrix of 1 row and 3 
> columns.

Why 1x3 rather than 3x1?


> Why this difference?

Because that's how numpy arrays are designed to work. I'm not sure what 
sort of answer you expect.



-- 
Steven


More information about the Tutor mailing list