How to get the number of dimensions of a matrix using NumPy in Python?

How to get the number of dimensions of a matrix using NumPy in Python?

To get the number of dimensions of a matrix (or an array) using NumPy, you can use the ndim attribute of the array.

Here's how you can do it:

import numpy as np # Create a 2D matrix matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Get the number of dimensions num_dimensions = matrix.ndim print(num_dimensions) # This will output: 2 

In this example, matrix is a 2D array, so matrix.ndim will return 2. If you have a 1D array, it will return 1, and so on for higher-dimensional arrays.


More Tags

uibarbuttonitem sharepoint-2013 yarnpkg subscript database-restore recorder.js backwards-compatibility nuxtjs3 video-thumbnails windows-1252

More Programming Guides

Other Guides

More Programming Examples