{"id":3061,"date":"2021-04-22T07:59:46","date_gmt":"2021-04-22T02:29:46","guid":{"rendered":"https:\/\/python-programs.com\/?p=3061"},"modified":"2021-11-22T18:44:56","modified_gmt":"2021-11-22T13:14:56","slug":"how-to-get-numpy-array-dimensions-using-numpy-ndarray-shape-numpy-ndarray-size-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-get-numpy-array-dimensions-using-numpy-ndarray-shape-numpy-ndarray-size-in-python\/","title":{"rendered":"How to get Numpy Array Dimensions using numpy.ndarray.shape & numpy.ndarray.size() in Python"},"content":{"rendered":"
In this article, we will be discussing how to count several elements in 1D, 2D, and 3D Numpy array. Moreover, we will be discussing the counting of rows and columns in a 2D array and the number of elements per axis in a 3D Numpy array.<\/p>\n
Let’s get started!<\/strong><\/em><\/p>\n This module is used to get a current shape of an array, but it is also used to reshaping the array in place by assigning a tuple of arrays dimensions to it. The function is:<\/p>\n We will use this function for determining the dimensions of the 1D and 2D array.<\/p>\n Let us start with a 2D Numpy array.<\/p>\n <\/p>\n <\/p>\n Code:<\/p>\n <\/p>\n Code:<\/p>\n <\/p>\n Code:<\/p>\n Total Number of elements in 2D Numpy array: 12<\/p>\n Now, we will work on a 1D NumPy array.<\/p>\n <\/p>\n Code:<\/p>\n Now, we will see the module which provides a function to get the number of elements in a Numpy array along the axis.<\/p>\n numpy.<\/span>size<\/span>(<\/span>arr, axis=<\/span>None<\/span>)<\/span><\/p>\n We will use this module for getting the dimensions of a 2D and 1D Numpy array.<\/p>\n We will begin with a 2D Numpy array.<\/p>\n <\/p>\n Code:<\/p>\n Output:<\/p>\n <\/p>\n Code:<\/p>\n <\/p>\n Code:<\/p>\n Output:<\/p>\n Now, we will be working on the 3D Numpy array.<\/p>\n <\/p>\n Code:<\/p>\nGet the Dimensions of a Numpy array using ndarray.shape()<\/h2>\n
NumPy.ndarray.shape<\/h3>\n
ndarray.shape<\/pre>\n
Get Dimensions of a 2D NumPy array using ndarray.shape:<\/h3>\n
2D Numpy Array \r\n[[11 12 13 11] \r\n[21 22 23 24] \r\n[31 32 33 34]]<\/pre>\n
Get the number of rows in\u00a0this 2D NumPy array:<\/h3>\n
numOfRows = arr2D.shape[0]\r\nprint('Number\u00a0of\u00a0Rows\u00a0:\u00a0',\u00a0numOfRows)<\/pre>\n<\/div>\n
Number of Rows : 3<\/pre>\n
Get a number of columns in this 2D NumPy array:<\/h3>\n
numOfColumns = arr2D.shape[1]\r\nprint('Number\u00a0of\u00a0Columns\u00a0:\u00a0',\u00a0numOfColumns)<\/pre>\n<\/div>\n
Number of Columns: 4<\/pre>\n
Get the total number of elements in this 2D NumPy array:<\/h3>\n
print('Total\u00a0Number\u00a0of\u00a0elements\u00a0in\u00a02D\u00a0Numpy\u00a0array\u00a0:\u00a0',\u00a0arr2D.shape[0]\u00a0*\u00a0arr2D.shape[1])<\/pre>\n<\/div>\n
Get Dimensions of a 1D NumPy array using ndarray.shape<\/h3>\n
arr = np.<\/span>array<\/span>([<\/span>4<\/span>, <\/span>5<\/span>, <\/span>6<\/span>, <\/span>7<\/span>, <\/span>8<\/span>, <\/span>9<\/span>, <\/span>10<\/span>, <\/span>11<\/span>])<\/span><\/pre>\n
Shape of 1D numpy array : <\/span>(<\/span>8<\/span>,<\/span>)\r\n<\/span>length of 1D numpy array : 8<\/span><\/pre>\n<\/div>\n<\/div>\n
Get the Dimensions of a Numpy array using NumPy.shape()<\/h2>\n
Get Dimensions of a 2D numpy array using numpy.size()<\/h3>\n
arr2D = np.<\/span>array<\/span>([[<\/span>11<\/span> ,<\/span>12<\/span>,<\/span>13<\/span>,<\/span>11<\/span>]<\/span>, <\/span>[<\/span>21<\/span>, <\/span>22<\/span>, <\/span>23<\/span>, <\/span>24<\/span>]<\/span>, <\/span>[<\/span>31<\/span>,<\/span>32<\/span>,<\/span>33<\/span>,<\/span>34<\/span>]])\r\n<\/span>print(<\/span>'2D Numpy Array'<\/span>)\r\n<\/span>print(<\/span>arr2D<\/span>)<\/span><\/pre>\n<\/div>\n
Get a number of rows and columns of this 2D NumPy array:<\/h3>\n
numOfRows = np.<\/span>size<\/span>(<\/span>arr2D, <\/span>0<\/span>)\r\n<\/span># get number of columns in 2D numpy array\r\nnumOfColumns = np.size<\/span>(<\/span>arr2D, <\/span>1)\r\n<\/span>print(<\/span>'Number of Rows : '<\/span>, numOfRows<\/span>)\r\n<\/span>print(<\/span>'Number of Columns : '<\/span>, numOfColumns<\/span>)<\/span><\/pre>\n<\/div>\n
Number of Rows : <\/span>3\r\n<\/span>Number of Columns: 4<\/span><\/pre>\n<\/div>\n
Get a total number of elements in this 2D NumPy array:<\/h3>\n
print<\/span>(<\/span>'Total Number of elements in 2D Numpy array : '<\/span>, np.<\/span>size<\/span>(<\/span>arr2D<\/span>))<\/span><\/pre>\n
Total Number of elements <\/span>in<\/span> 2D Numpy array: <\/span>12<\/span><\/pre>\n<\/div>\n<\/div>\n
Get Dimensions of a 3D NumPy array using numpy.size()<\/h2>\n<\/div>\n
arr3D = np.<\/span>array<\/span>([<\/span> [[<\/span>11<\/span>, <\/span>12<\/span>, <\/span>13<\/span>, <\/span>11<\/span>]<\/span>, <\/span>[<\/span>21<\/span>, <\/span>22<\/span>, <\/span>23<\/span>, <\/span>24<\/span>]<\/span>, <\/span>[<\/span>31<\/span>, <\/span>32<\/span>, <\/span>33<\/span>, <\/span>34<\/span>