• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
[Tut] np.shape()

np.shape()

This tutorial explains NumPy’s `shape()` function.

`numpy.shape(a)`

Return the shape of an array or array_like object `a`.

Return Value: `shape` — a tuple of integers that are set to the lengths of the corresponding array dimensions.

## Examples

The straightforward example is when applied to a NumPy array:

```>>> import numpy as np
>>> a = np.array([[1, 2], [3, 4]])
>>> np.shape(a)
(2, 2)```

You import the NumPy library and create a two-dimensional array from a list of lists. If you pass the NumPy array into the shape function, it returns a tuple with two values (=dimensions). Each dimension stores the number of elements in this dimension (=axis). As it is a 2×2 quadratic matrix, the result is (2,2).

The following shape is another example of a multi-dimensional array:

```>>> b = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
>>> b
array([[1, 2, 3, 4], [5, 6, 7, 8]])
>>> b.shape
(2, 4)
>>> np.shape(b)
(2, 4)```

The shape is now `(2, 4)` with two rows and four columns.

### np.shape() vs array.shape

Note that the result of `np.shape(b)` and `b.shape` is the same if `b` is a NumPy array. If `b` isn’t a NumPy array but a list, you cannot use `b.shape` as lists don’t have the shape attribute. Let’s have a look at this example:

```>>> b = [[1, 2, 3, 4], [5, 6, 7, 8]]
>>> np.shape(b)
(2, 4)```

The `np.shape()` function returns the same shape tuple—even if you pass a nested list into the function instead of a NumPy array.

But if you try to access the list.shape attribute, NumPy throws the following error:

```>>> b.shape
Traceback (most recent call last): File "<pyshell#9>", line 1, in <module> b.shape
AttributeError: 'list' object has no attribute 'shape'```

So, the difference between `np.shape()` and `array.shape` is that the former can be used for all kinds of array_like objects while the latter can only be used for NumPy arrays with the `shape` attribute.

Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)

## References

The post np.shape() first appeared on Finxter.

https://www.sickgaming.net/blog/2020/11/18/np-shape/

Forum Jump:

 Active Threads News - Atelier Ryza Developer Clarifies ... Last Post: xSicKxBot Today 05:30 AM » Replies: 0 » Views: 0 News - Doom Eternal Update 4 Adds New Ma... Last Post: xSicKxBot Today 05:30 AM » Replies: 0 » Views: 4 Unreal Engine Asset Giveaway For Decembe... Last Post: xSicKxBot Today 04:15 AM » Replies: 0 » Views: 4 [Tut] Searching The Parse Tree Using Bea... Last Post: xSicKxBot Yesterday 10:53 PM » Replies: 0 » Views: 4 News - PixelJunk Eden 2 Coming Very Soon Last Post: xSicKxBot Yesterday 10:53 PM » Replies: 0 » Views: 63 (Indie Deal) Black Friday Weekend Round-... Last Post: xSicKxBot Yesterday 08:48 PM » Replies: 0 » Views: 5 Xbox Wire - How to Buy a Digital Game as... Last Post: xSicKxBot Yesterday 06:00 PM » Replies: 0 » Views: 4 News - Video: A look at Insomniac Games’... Last Post: xSicKxBot Yesterday 06:00 PM » Replies: 0 » Views: 64 News - Saudi Arabian charity Misk acquir... Last Post: xSicKxBot Yesterday 06:00 PM » Replies: 0 » Views: 82 News - Net Neutrality-Repealing FCC Chai... Last Post: xSicKxBot Yesterday 03:20 PM » Replies: 0 » Views: 64