From 4e89dd4d3d7adbd295b83c99056bd1191aeb266c Mon Sep 17 00:00:00 2001 From: AayushJB03 Date: Tue, 28 Oct 2025 00:20:14 +0530 Subject: [PATCH 1/4] Add NumPy ndarray shape term entry (#7824) --- .../concepts/ndarray/terms/shape/shape.md | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 content/numpy/concepts/ndarray/terms/shape/shape.md diff --git a/content/numpy/concepts/ndarray/terms/shape/shape.md b/content/numpy/concepts/ndarray/terms/shape/shape.md new file mode 100644 index 00000000000..ed1ebb4bcce --- /dev/null +++ b/content/numpy/concepts/ndarray/terms/shape/shape.md @@ -0,0 +1,102 @@ +--- +Title: 'shape' +Description: 'Returns a tuple representing the dimensions of an ndarray.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'NumPy' + - 'Arrays' + - 'Data Structures' +CatalogContent: + - 'learn-python-3' + - 'paths/data-science' +--- + +The **`shape`** attribute of a NumPy ndarray returns a tuple of integers indicating the size of the array in each dimension. It provides information about the structure and layout of the array. + +## Syntax + +```pseudo +array.shape +``` + +- `array`: The ndarray whose dimensions are to be retrieved. + +The `shape` attribute returns a tuple where each element represents the number of elements along the corresponding dimension. + +## Example + +The following example demonstrates how to use the `shape` attribute to get the dimensions of different ndarrays: + +```py +import numpy as np + +# 1D array +arr_1d = np.array([1, 2, 3, 4, 5]) +print(arr_1d.shape) + +# 2D array +arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) +print(arr_2d.shape) + +# 3D array +arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) +print(arr_3d.shape) +``` + +The output for the above code will be: + +``` +(5,) +(2, 3) +(2, 2, 2) +``` + +## Codebyte Example + +The following codebyte example shows how to use the `shape` attribute and modify array dimensions: + +```codebyte/python +import numpy as np + +# Create a 1D array +original_array = np.array([1, 2, 3, 4, 5, 6]) +print("Original shape:", original_array.shape) + +# Reshape to 2D array +reshaped_array = original_array.reshape(2, 3) +print("Reshaped to 2D:", reshaped_array.shape) +print(reshaped_array) + +# Reshape to 3D array +reshaped_3d = original_array.reshape(2, 3, 1) +print("Reshaped to 3D:", reshaped_3d.shape) +``` + +## Frequently Asked Questions + +
+1. Can the shape of an array be modified directly? +

Yes, the shape attribute can be modified by assigning a new tuple to it, which reshapes the array in-place. However, the new shape must be compatible with the total number of elements. For example: array.shape = (2, 3) will reshape a 6-element array into a 2x3 matrix.

+
+ +
+2. What does it mean when a dimension in shape is 1? +

A dimension with size 1 indicates that the array has only one element along that axis. For example, a shape of (3, 1, 4) means 3 elements in the first dimension, 1 element in the second dimension, and 4 elements in the third dimension. This is often used to maintain specific dimensionality for broadcasting operations.

+
+ +
+3. How does shape relate to the size and ndim attributes? +

The shape attribute returns a tuple of dimensions, ndim returns the number of dimensions (length of the shape tuple), and size returns the total number of elements (product of all dimensions in shape). For example, an array with shape (2, 3, 4) has ndim=3 and size=24.

+
+ +
+4. What is the difference between shape and reshape? +

The shape attribute is used to view or modify the dimensions of an existing array in-place, while reshape() is a method that returns a new array with a modified shape without changing the original array. Use shape when you want to modify the array directly, and reshape() when you want to preserve the original.

+
+ +
+5. Why does a 1D array shape have a comma, like (5,)? +

The comma in (5,) distinguishes a tuple with one element from a scalar value in parentheses. In Python, (5) is just the number 5, while (5,) is a tuple containing one element. This notation ensures that shape consistently returns a tuple regardless of the number of dimensions.

+
From 2d6c4f629c3c88a640c306c224de9e6fbedca24a Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 28 Oct 2025 13:57:42 +0530 Subject: [PATCH 2/4] Enhance documentation for ndarray shape attribute Added detailed explanations for parameters and return values of the shape attribute. Updated examples and FAQs for clarity. --- .../concepts/ndarray/terms/shape/shape.md | 46 ++++++------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/shape/shape.md b/content/numpy/concepts/ndarray/terms/shape/shape.md index ed1ebb4bcce..43ebc69b85d 100644 --- a/content/numpy/concepts/ndarray/terms/shape/shape.md +++ b/content/numpy/concepts/ndarray/terms/shape/shape.md @@ -5,15 +5,16 @@ Subjects: - 'Computer Science' - 'Data Science' Tags: - - 'NumPy' - 'Arrays' - 'Data Structures' + - 'NumPy' + - 'Tuples' CatalogContent: - 'learn-python-3' - 'paths/data-science' --- -The **`shape`** attribute of a NumPy ndarray returns a tuple of integers indicating the size of the array in each dimension. It provides information about the structure and layout of the array. +The **`shape`** attribute of a NumPy `ndarray` returns a [tuple](https://www.codecademy.com/resources/docs/python/tuples) of integers indicating the size of the array in each dimension. It provides information about the structure and layout of the array. ## Syntax @@ -21,9 +22,17 @@ The **`shape`** attribute of a NumPy ndarray returns a tuple of integers indicat array.shape ``` -- `array`: The ndarray whose dimensions are to be retrieved. +**Parameters:** + +This attribute does not take any parameters. -The `shape` attribute returns a tuple where each element represents the number of elements along the corresponding dimension. +**Return value:** + +Returns a tuple of integers representing the size of the array along each dimension. + +- For a 1D array, it returns a single value (e.g., `(5,)`). +- For a 2D array, it returns two values i.e. rows and columns (e.g., `(3, 4)`). +- For higher dimensions, it includes one integer per axis. ## Example @@ -47,7 +56,7 @@ print(arr_3d.shape) The output for the above code will be: -``` +```shell (5,) (2, 3) (2, 2, 2) @@ -73,30 +82,3 @@ print(reshaped_array) reshaped_3d = original_array.reshape(2, 3, 1) print("Reshaped to 3D:", reshaped_3d.shape) ``` - -## Frequently Asked Questions - -
-1. Can the shape of an array be modified directly? -

Yes, the shape attribute can be modified by assigning a new tuple to it, which reshapes the array in-place. However, the new shape must be compatible with the total number of elements. For example: array.shape = (2, 3) will reshape a 6-element array into a 2x3 matrix.

-
- -
-2. What does it mean when a dimension in shape is 1? -

A dimension with size 1 indicates that the array has only one element along that axis. For example, a shape of (3, 1, 4) means 3 elements in the first dimension, 1 element in the second dimension, and 4 elements in the third dimension. This is often used to maintain specific dimensionality for broadcasting operations.

-
- -
-3. How does shape relate to the size and ndim attributes? -

The shape attribute returns a tuple of dimensions, ndim returns the number of dimensions (length of the shape tuple), and size returns the total number of elements (product of all dimensions in shape). For example, an array with shape (2, 3, 4) has ndim=3 and size=24.

-
- -
-4. What is the difference between shape and reshape? -

The shape attribute is used to view or modify the dimensions of an existing array in-place, while reshape() is a method that returns a new array with a modified shape without changing the original array. Use shape when you want to modify the array directly, and reshape() when you want to preserve the original.

-
- -
-5. Why does a 1D array shape have a comma, like (5,)? -

The comma in (5,) distinguishes a tuple with one element from a scalar value in parentheses. In Python, (5) is just the number 5, while (5,) is a tuple containing one element. This notation ensures that shape consistently returns a tuple regardless of the number of dimensions.

-
From f1cbd74823983ee536e4e4c8db6a187c6c0e7af5 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 28 Oct 2025 13:58:23 +0530 Subject: [PATCH 3/4] Fix syntax for ndarray shape reference --- content/numpy/concepts/ndarray/terms/shape/shape.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/numpy/concepts/ndarray/terms/shape/shape.md b/content/numpy/concepts/ndarray/terms/shape/shape.md index 43ebc69b85d..bc9562d9e1f 100644 --- a/content/numpy/concepts/ndarray/terms/shape/shape.md +++ b/content/numpy/concepts/ndarray/terms/shape/shape.md @@ -19,7 +19,7 @@ The **`shape`** attribute of a NumPy `ndarray` returns a [tuple](https://www.cod ## Syntax ```pseudo -array.shape +ndarray.shape ``` **Parameters:** From e9cf174a7315ca20ccca08758f4b6b129debc2f8 Mon Sep 17 00:00:00 2001 From: Sriparno Roy <89148144+Sriparno08@users.noreply.github.com> Date: Thu, 30 Oct 2025 21:18:47 +0530 Subject: [PATCH 4/4] Minor changes --- content/numpy/concepts/ndarray/terms/shape/shape.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/shape/shape.md b/content/numpy/concepts/ndarray/terms/shape/shape.md index bc9562d9e1f..be586925e48 100644 --- a/content/numpy/concepts/ndarray/terms/shape/shape.md +++ b/content/numpy/concepts/ndarray/terms/shape/shape.md @@ -1,5 +1,5 @@ --- -Title: 'shape' +Title: '.shape' Description: 'Returns a tuple representing the dimensions of an ndarray.' Subjects: - 'Computer Science' @@ -14,7 +14,7 @@ CatalogContent: - 'paths/data-science' --- -The **`shape`** attribute of a NumPy `ndarray` returns a [tuple](https://www.codecademy.com/resources/docs/python/tuples) of integers indicating the size of the array in each dimension. It provides information about the structure and layout of the array. +The **`.shape`** attribute of a NumPy `ndarray` returns a [tuple](https://www.codecademy.com/resources/docs/python/tuples) of integers specifying the size of the array in each dimension. It provides information about the structure and layout of the array. ## Syntax @@ -36,7 +36,7 @@ Returns a tuple of integers representing the size of the array along each dimens ## Example -The following example demonstrates how to use the `shape` attribute to get the dimensions of different ndarrays: +The following example demonstrates how to use the `.shape` attribute to get the dimensions of different ndarrays: ```py import numpy as np @@ -64,7 +64,7 @@ The output for the above code will be: ## Codebyte Example -The following codebyte example shows how to use the `shape` attribute and modify array dimensions: +The following codebyte example shows how to use the `.shape` attribute and modify array dimensions: ```codebyte/python import numpy as np