Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn Array Shape

In NumPy, the shape of an array refers to the dimensions (or the size) of each axis (or dimension) of the array. It tells you how many elements are along each axis. The shape of an array is represented as a tuple of integers, where each integer represents the size of the corresponding dimension.


Example

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])

Here, arr has a shape of (2, 3) because it has 2 rows and 3 columns.

  1. The first dimension (axis 0) has a size of 2, which represents the number of rows.
  2. The second dimension (axis 1) has a size of 3, which represents the number of columns.

print(arr.shape) # Output: (2, 3)


Here are a few key points about Array shapes


Rank : The number of dimensions in the array is also known as its rank. For example, a 1-dimensional array has a rank of 1, a 2-dimensional array has a rank of 2, and so on.
Size : The total number of elements in the array is the product of the sizes of all its dimensions. For example, in the array [[1, 2, 3], [4, 5, 6]], the size is 2 (rows) * 3 (columns) = 6.
Reshaping You can change the shape of an array using the reshape() method. However, the total number of elements must remain the same. For example, you can reshape a (6,) array into a (2, 3) array.
Array Operations : When performing operations on arrays, NumPy automatically broadcasts arrays with different shapes if possible. Broadcasting allows you to perform operations even when the shapes of the arrays don't match exactly.






Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java