![]() |
VOOZH | about |
In NumPy, determining dimensions of a matrix is a common task. Knowing number of rows and columns helps in data manipulation, analysis and reshaping operations. Below are the different methods to find the number of rows and columns of a matrix efficiently.
The .shape attribute of a NumPy array returns a tuple containing the number of rows and columns.
('Rows:', 2)
('Columns:', 3)
Explanation:
Indexing can be used to obtain the number of rows and columns by accessing the elements of the .shape tuple.
('Rows:', 2)
('Columns:', 3)
Explanation:
reshape() is primarily used to change the shape of an array, but it can also help visualize dimensions by converting a 1D array into a 2D matrix.
[[1 2 3]
[4 5 6]
[7 8 9]]
('Rows:', 3)
('Columns:', 3)
Explanation: