NumPy Array Dimensions
“The multidimensional or the ndarrays are the fixedsize containers of multidimensions having the items stored in them of the same size and type. The number of dimensions in an array describes the rank for that array. We can define these dimensions in the number for an array, and the dimensions represent the number of the indices that are required to be specified for the particular individual item or element in the array.
If we want to know about the number of the dimensions for the array, then we can use the function from the “numpy”, which will return the number of dimensions, the length of every dimension of the array, and the size of the array using the extension method as the “numpy. ndarray ()”. For the introduction to “Numpy”, it’s a library that provides the builtin function for arrayrelated operations in the python language.”
Procedure
This article will give an introduction based on the working and declaration principles for the numpy array dimensions. We will get to know about the functions (builtin one) from the numpy package to get the array dimension and the length of the dimension.
Syntax
If we want to know about the array’s dimension, as in, we want to know the size, the number and the shape, then the python library numpy offers many functions related to each specific attribute of the array. In the article, we will be discussing the three of them, and they are as follows:

 array. ndim
 array. shape
 array. size
Return Value
All the abovementioned functions will give the dimensions, the shape of the specified dimensions, and the size of the array or dimension length, respectively, in the output.
Example # 01
We will use one of the abovementioned functions and will implement that function to verify its results. So, the first function we will be implementing is the “array. ndim ()”. We will be executing the program for this function in the “spyder”, which is known as the software platform for python. The first step for the implementation of this function would be to create a project in the shell and then save it in the required file directories.
For writing any program in the python shell, we have to install some libraries that help to fetch the information of the functions that we call in the program and make the execution of the program possible. Some of these libraries come installed with the software, and others we have to download and install in the software with the help of specific commands through the terminal window. So, we installed one such library to use the functions offered by it in the project. This library is named “numpy”.
We will import the module of the name “numpy” from its installed libraries. Coming back to the example, we will use the numpy module and will create an array object and assign it to the variable “array”. For this example, we will create a 2darray object having its elements indexed in the two dimensions. The class related to the array represents the ndarrays. The elements of the numpy array can be initialized with the python list, and we can access them by the square [] brackets. So, using these brackets “[]” and the “np. array ()”, we will specify the elements and the dimension of the array.
The 2dimensional array will look like “np. array ([[ 9, 8, 7], [ 8, 5, 3]])”. On this twodimensional array, we will apply the “array. ndim” function, and it will return the dimension for this array. To display the result for this function on the screen, just pass this function to the parameter of the function print () or call the same function in the parameter of the print () function. We have called this function using the array name in the parameter of the function print () to save time. To implement the program, copy the code written in python language from the figure below and paste it into the compiler.
# Create an array object
array = np.array( [[ 9, 8, 7], [ 8, 5, 3]] )
# Print the array dimensions
print("number of the dimensions: ", array.ndim)
Once we will paste the program for the above example and will run the program on the python compiler, then the program will display the output, which will have the number of dimensions that should be equal to 2 since we had defined the twodimensional array in our example.
Example # 02
After knowing the method to get the dimensions for the array now, let us solve another example and write a program for finding the shape of the array once we have declared an array. The shape of the array contributes to that attribute in the numpy array that is used to get the estimation for the length of the array’s dimensions. To know about the shape of the array, start by importing the “numpy” array module and then using this module to try to create another 2darray with the method as defined in the previous example.
The twodimensional array will have the elements specified by the “np. array ()” method as “[[ 9, 8, 7], [ 8, 5, 3]]”. We will use this array and will call the function “array. shape” and pass it to the parameters of the print() function to display the results that we will get from this shape function. We will again print this array with the “array. size” to know the size of the elements in the array. The output for this code is mentioned below:
# Create an array object
array = np.array( [[ 9, 8, 7], [ 8, 5, 3]] )
# Print the shape of an array
print("Shape of the array: ", array.shape)
# Print size number of elements in total of array
print("array size: ", array.size)
The function has returned the shape and the size of the twodimensional array that we had specified in the shape and size method.
Conclusion
This guide has all the information and methods explained to get the dimensions for an array. We have mentioned three functions with the name “. shape, .ndim, and the .size ” to get the shape, the number of dimensions, and the size of the array, respectively. We have done two examples that explain how we can call these methods in the program.
Source: linuxhint.com