The **np.ndarray.shape** is a **Numpy** **property** that returns the tuple of array dimensions.

The **shape** property of the **Numpy** array is usually used to get the current shape of the array but may also be used to reshape an array by assigning the tuple of array dimensions to it. The shape of the array is the number of items in each dimension.

**Syntax**

numpy.shape(a)

**Parameters**

The shape() property takes one parameter called **an array. **It is the parameter whose dimension we need to get.

**Implement np.shape() function**

Numpy arange() function that returns a **ndarray** object containing evenly spaced values within the given **range**. Then we will find the shape of the array.

See the following code.

# app.py import numpy as np dim = np.arange(4) print(dim.shape)

**Output**

python3 app.py (4,)

Let’s define an array using some values and find its shape using the np shape property.

# app.py import numpy as np dim = np.array([11, 19, 21, 29, 46]) print(dim.shape)

**Output**

python3 app.py (5,)

**Find the shape of the Two-dimensional array in Numpy**

In this example, we will define one array using the numpy arange() function and then reshape() the array to 2* 2. That means our dimension of the final array will be 2*2. We will verify this with a numpy array shape property.

# app.py import numpy as np data = np.arange(4).reshape((2, 2)) print(data) print("The shape of the array is: ", data.shape)

**Output**

python3 app.py [[0 1] [2 3]] The shape of the array is: (2, 2)

Another way to write a two-dimensional array like this and print its shape.

# app.py import numpy as np data = np.array([[11, 21, 31, 41], [15, 16, 17, 18]]) print(data) print("The shape of the array is: ", data.shape)

**Output**

python3 app.py [[11 21 31 41] [15 16 17 18]] The shape of the array is: (2, 4)

The example above returns (2, 4), which means that an array has two dimensions, and each dimension has four elements.

**Create an array with ndmin = 5 in Numpy**

Create the array with 5 dimensions using **ndmin** using the vector with values 1,2,3,4 and verify that the last dimension has value 4. See the following code.

# app.py import numpy as np data = np.array([11, 21, 31, 41], ndmin=5) print(data) print("The shape of the array is: ", data.shape)

**Output**

python3 app.py [[[[[11 21 31 41]]]]] The shape of the array is: (1, 1, 1, 1, 4)

Integers at every index tell about the number of items the corresponding dimension has.

In the above example at index-4, we have value 4, so we can say that the 5th ( 4 + 1st) dimension has 4 elements.

**Find the np shape of np.zeros()**

The numpy.zeros() function returns the new array of a given shape and type, filled with zeros.

See the following code.

# app.py import numpy as np data = np.zeros((1, 2)) print(data) print("The shape of the array is: ", data.shape)

**Output**

python3 app.py [[0. 0.]] The shape of the array is: (1, 2)

**ValueError: total size of the new array must be unchanged**

If you try to change the size of the array-like assigning new dimensions to the array, it won’t be changed. If you try to do so, it will give the **ValueError: the total size of the new array must be unchanged.**

**Conclusion**

A Numpy array is a table of items (usually numbers) of the same types, indexed by a tuple of positive integers. In Numpy, several dimensions of the array are called the rank of the array.

Numpy arrays have an attribute called **shape** that returns the tuple, with each index having the number of corresponding elements.

A tuple of integers giving the size of the array along each dimension is known as the shape of the array.

We can find a dimension or shape of an array using the **np shape **function. Likewise, we can reset the dimension of an array using the np reshape() function.

In the section “Find the shape of Two-dimensional array in Numpy” the output is

python3 app.py

[[11 21 31 41]

[15 16 17 18]]

The shape of the array is: (2, 4)

And it says

“The example above returns (2, 4), which means that an array has two dimensions, and each dimension has four elements.”

But doesn’t shape return the number of elements in each dimension of the array, not the array dimensions? In this case the number of elements in the first dimension happens to equal the number of dimensions. ndim would give the number of dimensions of the array.