NumPy array shape annotation / definition

In the week 2 lab following Vectorisation part 2, we use NumPy to view the shape of an array, for various arrays. I am confused because for a 1-dimensional array, calling the shape using .shape results in something like (n,), or (4,) when the number of columns is 4, whereas for 2-dimensional arrays the shape is printed in the format (m, n) where m is the number of rows and n the number of columns. I would therefore expect the result of . shape called on a 1-dimensional array to be in the format (, n) rather than (n,). Or for the above example (, 4).

Vectorisation : Coursera | Online Courses & Credentials From Top Educators. Join for Free | Coursera

There really isn’t a standard for the layout of the dimensions. Sometimes you’ll see (m,n) and sometimes it’s (n,m).

Even within a specific course or notebook, it could vary.

So you just have to look at how the dataset is organized, and adapt as necessary.

1 Like

For the shape of an array, it should be note that each of the shape is corresponding to the dimension of the array. For 1-d array, the dimensions count from 0 (first dimension), then the shape (N,) or shape at 0 is N.

1 Like

That makes sense, thanks

Since it is related, may I kindly ask what is the difference between

a = np.zeros(4) and a = np.zeros((4,))?

Thanks a lot.

Hello, @jiyanbaran,

There is no difference. Both (4, ) and 4 convey the meaning of a 1D array of 4 elements.

Cheers,
Raymond

Hello @rmwkwok

Thanks for your help!

Regards
Jiyan

1 Like

No problem, Jiyan @jiyanbaran.

Cheers,
Raymond