Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For a np.array([1, 2, 3]) why is the shape (3,) instead of (3,1)? [duplicate]

I noticed that for a rank 1 array with 3 elements numpy returns (3,) for the shape. I know that this tuple represents the size of the array along each dimension, but why isn't it (3,1)?

import numpy as np

a = np.array([1, 2, 3])  # Create a rank 1 array
print a.shape            # Prints "(3,)"

b = np.array([[1,2,3],[4,5,6]])   # Create a rank 2 array
print b.shape                     # Prints "(2, 3)"
like image 473
Alaa Awad Avatar asked Mar 11 '23 01:03

Alaa Awad


1 Answers

In a nutshell, this is because it's one-dimensional array (hence the one-element shape tuple). Perhaps the following will help clear things up:

>>> np.array([1, 2, 3]).shape
(3,)
>>> np.array([[1, 2, 3]]).shape
(1, 3)
>>> np.array([[1], [2], [3]]).shape
(3, 1)

We can even go three dimensions (and higher):

>>> np.array([[[1]], [[2]], [[3]]]).shape
(3, 1, 1)
like image 70
NPE Avatar answered Apr 28 '23 04:04

NPE