NumPy Array Shape

The number of items in each dimension defines an array's form. The dimension is the number of indices, or subscripts, needed to express each unique member.

How can the Shape of an Array be Obtained?

We'll use NumPy's shape property, which yields a tuple whose members match the lengths of the appropriate array dimensions.

Syntax:

Parameters:

The array is passed as a Parameter.

Return:

A tuple whose components correspond to the relevant array dimensions' lengths.

Shape Manipulation in NumPy

The following examples will help us learn how to manipulate shapes in NumPy in Python:

Example 1: Array Shape

Printing the multidimensional array's form. Two NumPy arrays, arr1 and arr2, are generated in this example to represent a 2D array and a 3D array, respectively. Each array's form is printed, exposing its sizes and measurements along each dimension.

Program Explanation:

The given Python code makes and prints the states of two clusters, arr1, and arr2, utilizing the NumPy module. The elements of arr1 are 2x4, while the components of arr2 are 2x2x2. NumPy clusters' shape property yields insights concerning the exhibit's aspects. The related aspects of each cluster are then shown by the code when it prints the states of arr1 and arr2 utilizing the print capability.

Output:

Shape of arr1: (2, 4)
Shape of arr2: (2, 2, 2)

Example 2: Shape of Array Using ndim

In this example, we are using ndmin to create an array with a vector containing the numbers 2,4,6,8,10, and we are checking the value of the last dimension.

Program Explanation:

This Python program tells the best way to make a one-layered exhibit with six aspects by utilizing the ndmin contention and the NumPy bundle. From that point forward, the cluster is printed to uncover its six-layered structure. Eventually, the product affirms that the cluster's size is five by printing and checking its structure, giving close consideration to the last estimation.

Output:

Array with 6 dimensions:
 [[[[[[ 2  4  6  8 10]]]]]]
Shape of the array: (1, 1, 1, 1, 1, 5)

Example 3: Shape of Array of Tuples

We'll build a NumPy array with tuples as each entry in this example. We'll also show you how to figure out how such an array is shaped.

Program Explanation:

Utilizing the NumPy module, the given Python code makes a variety of tuples and reports both the exhibit and its structure. Utilize NumPy's cluster capability to change a rundown of tuples into a NumPy exhibit. The exhibit's shape characteristic is then gotten to and printed to show the cluster's aspects. This little piece of code makes sense of how NumPy handles varieties of tuples and how to get their shape information.

Output:

Array of Tuples:
[[1 2]
 [3 4]
 [5 6]
 [7 8]]
Shape of the Array: (4, 2)