| by Arround The Web | No comments

NumPy Array Shape Method

Python programming language is a very easy high-level programming language. This is the most loved high-level programming language among developers. It offers several practical and incredible libraries containing extremely useful built-in functions. The NumPy library in the Python programming language makes mathematical computation easier and simple. In this tutorial, we will examine the NumPy array shape method to help you understand how to use the shape method in Python codes.

What Is a NumPy Array Shape Method in Python?

The NumPy library offers many useful functions for arrays, and the shape method is one of them. The NumPy array shape method in the Python program is used to get the shape of the array. The array’s form describes how many items are present in each dimension. The shape() function provided by the NumPy library returns a tuple containing the number of corresponding elements. For example, if an array is 2-dimensional, containing five items in each dimension, then the shape() function will return (2, 5). 2 represents the 2-D, and 5 indicates the item numbers in each dimension.

Learn how to utilize the NumPy array shape technique in Python scripts by looking at various examples.

Example 1

We will start with a simple example to help you understand the basic working of the NumPy array shape method. We will demonstrate the shape method by testing it on 1-D, 2-D, and 3-D arrays. The reference code is given in the screenshot below:

import numpy as npy
ary1 = npy.array([1, 2, 3, 4, 5])
ary2 = npy.array([[1, 2, 3, 4], [5, 6, 7, 8]])
ary3 = npy.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print('Shape of array 1 is = ',ary1.shape)
print('\nShape of array 2 is = ',ary2.shape)
print('\nShape of array 3 is = ',ary3.shape)

 

We imported the NumPy library in the first line with the “import numpy as npy” statement. The npy variable will be used in the whole program to call the shape() and other necessary methods. First, we declared an array “ary1”, which is a one-dimensional array containing five elements. Second, we declared another array, “ary2”, which is a two-dimensional array containing four elements in each dimension. And lastly, we declared a third array, “ary3”, which is a three-dimensional array containing two elements in each size. Three print() statements show the shape of all arrays with the shape method. Each variable containing the arrays will call the shape method so that the shape of its corresponding array will be checked. The output generated by the program is given in the screenshot below:


Here, you can note that the shape of the first array is 1-D which is why the shape method returns only (5,) representing that there are five elements in the array. The shape of the “ary2” is (2, 4), which shows that the array is 2-D, and each dimension includes four items. And lastly, the shape of the third array is (2, 2, 2), representing that the array is three-dimensional and each dimension contains two rows and two columns.

Example 2

Previously, we have explicitly declared three arrays, 1-D, 2-D, and 3-D, and checked their shape with the NumPy array shape method. Here, we will create an array with the NumPy library and then check the shape of the created array with the NumPy array shape method. Check out the reference code given in the following screenshot:

import numpy as npy
y = npy.zeros((3, 4, 5), dtype=int)
print('The computed array is:\n',y)
print('\nShape of array is = ',y.shape)

 

The NumPy library is imported into the program first to use the shape method of the NumPy library. After that, an array of zeros is created with the npy.zeros() command. As you can see, (3, 4, 5) is provided to the zeros() function, which means a 3-D array containing four rows and five columns of zeros should be created.

First, the created array is printed with the print() command, and then the shape of the created array is confirmed with the shape() function. The print() command is again used to show the result of the NumPy array shape method. The output of the computed array and the NumPy array shape method is given in the following screenshot. Refer to the following output to understand the working of the NumPy array shape method:

Example 3

So far, we have learned how to use the NumPy array shape method on the explicitly defined array and the auto-generated array with a function. Previously we learned how to create an array by providing all the essential elements of the function. Here, we will learn how to create a multidimensional array by only providing vector values. After creating the array from vectors, we will verify the dimensions of the array by using the NumPy array shape method. The reference code is given in the following screenshot:

import numpy as npy
ary = npy.array([2, 4, 6, 8], ndmin=6)
print('The array is: ',ary)
print('\nThe shape of array is: ', ary.shape)

 

First, the NumPy library is imported into the program as npy, and then the npy variable will be used to call any function of the NumPy library in the program. Here, we will use the array() function of the NumPy library to create an array and the shape method of the NumPy library to verify the dimension of the created array. The npy.array([2, 4, 6, 8]) is used to create an array with [2, 4, 6, 8] value, and ndmin = 6 is used to create an array of 6 dimensions. As you can see, we provided the vector values to the array() function and instructed it to make a six-dimensional array with the ndmin parameter.

As per the rules and working of the array() function, the six-dimensional array should be created with the first five dimensions containing only one element and the last dimension containing the provided elements. Let us verify this in the output provided below:

Conclusion

This guide was about the NumPy array shape method. The shape method provided by the Python NumPy library is used to check the dimensions of the given array. The shape of the array refers to the number of elements existing in each dimension of the array. With the help of simple and useful examples, we learned how to use the NumPy array shape method in Python programs. You can get help from these sample codes as it is, or you can modify them as needed. However, these sample programs will be helpful for your learning.

Share Button

Source: linuxhint.com

Leave a Reply