From 74b8a0e5324f272d9309d8a3d9583a6d75f03a7a Mon Sep 17 00:00:00 2001 From: drywantonmee Date: Fri, 25 Jul 2025 01:42:39 +0800 Subject: [PATCH 1/3] Add documentation for numpy.ndarray.swapaxes --- .../ndarray/terms/swapaxes/swapaxes.md | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 content/numpy/concepts/ndarray/terms/swapaxes/swapaxes.md diff --git a/content/numpy/concepts/ndarray/terms/swapaxes/swapaxes.md b/content/numpy/concepts/ndarray/terms/swapaxes/swapaxes.md new file mode 100644 index 00000000000..da0b7dd3d8a --- /dev/null +++ b/content/numpy/concepts/ndarray/terms/swapaxes/swapaxes.md @@ -0,0 +1,63 @@ +--- +Title: '.swapaxes()' +Description: 'Interchanges two axes of a NumPy ndarray.' +Subjects: + - 'Computer Science' + - 'Data Science' +Tags: + - 'Arrays' + - 'Data Structures' + - 'Functions' + - 'NumPy' +CatalogContent: + - 'learn-python-3' + - 'paths/data-science' +--- + +The **`.swapaxes()`** method returns a view of the array with the specified axes interchanged, without copying the data. +It is useful for reorienting multi-dimensional arrays for analysis, transformation or visualization. + +## Syntax + +```pseudo +ndarray.swapaxes(axis1, axis2) +``` +**Parameters:** +- `axis1`: An `int` representing the first axis to be swapped. +- `axis2`: An `int` representing the second axis to be swapped. + +**Return value:** + +Returns a `ndarray` that is a view of the original array with `axis1` and `axis2` interchanged. + +## Example + +The following example creates a 2D array and then swaps its axes: + +```py +import numpy as np + +arr = np.array([[1, 2, 3], [4, 5, 6]]) +swapped_arr = arr.swapaxes(0, 1) +print(swapped_arr) +``` + +The code above generates the following output: + +```shell +[[1 4] + [2 5] + [3 6]] +``` + +## Codebyte Example + +The following codebyte example demonstrates the use of `.swapaxes()` on a 3D array. + +```codebyte/python +import numpy as np + +arr = np.array([[10, 20, 30], [40, 50, 60]]) +swapped_arr = arr.swapaxes(0, 1) +print(swapped_arr) +``` \ No newline at end of file From cfc1fdd3eed649f5a7cd6887b1c1b85feb0924fc Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Fri, 1 Aug 2025 16:02:15 +0530 Subject: [PATCH 2/3] minor fixes --- .../concepts/ndarray/terms/swapaxes/swapaxes.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/swapaxes/swapaxes.md b/content/numpy/concepts/ndarray/terms/swapaxes/swapaxes.md index da0b7dd3d8a..939bbd433b2 100644 --- a/content/numpy/concepts/ndarray/terms/swapaxes/swapaxes.md +++ b/content/numpy/concepts/ndarray/terms/swapaxes/swapaxes.md @@ -1,6 +1,6 @@ --- Title: '.swapaxes()' -Description: 'Interchanges two axes of a NumPy ndarray.' +Description: 'Returns a view of the array with two axes interchanged.' Subjects: - 'Computer Science' - 'Data Science' @@ -14,21 +14,22 @@ CatalogContent: - 'paths/data-science' --- -The **`.swapaxes()`** method returns a view of the array with the specified axes interchanged, without copying the data. -It is useful for reorienting multi-dimensional arrays for analysis, transformation or visualization. +NumPy's **`.swapaxes()`** method returns a view of the array with the specified axes interchanged, without copying data. It’s useful for reorienting multi-dimensional arrays for analysis, transformation, or visualization. ## Syntax ```pseudo ndarray.swapaxes(axis1, axis2) ``` + **Parameters:** + - `axis1`: An `int` representing the first axis to be swapped. - `axis2`: An `int` representing the second axis to be swapped. **Return value:** -Returns a `ndarray` that is a view of the original array with `axis1` and `axis2` interchanged. +Returns an `ndarray` view of the original array with `axis1` and `axis2` interchanged. The shape of the returned array is the same as the original, but with the specified axes swapped. ## Example @@ -57,7 +58,7 @@ The following codebyte example demonstrates the use of `.swapaxes()` on a 3D arr ```codebyte/python import numpy as np -arr = np.array([[10, 20, 30], [40, 50, 60]]) -swapped_arr = arr.swapaxes(0, 1) +arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) +swapped_arr = arr.swapaxes(0, 2) print(swapped_arr) -``` \ No newline at end of file +``` From 54cb43a11593746f55a94670119a30daa0b4c3c3 Mon Sep 17 00:00:00 2001 From: Sriparno Roy Date: Wed, 6 Aug 2025 12:27:49 +0530 Subject: [PATCH 3/3] Minor changes --- content/numpy/concepts/ndarray/terms/swapaxes/swapaxes.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/numpy/concepts/ndarray/terms/swapaxes/swapaxes.md b/content/numpy/concepts/ndarray/terms/swapaxes/swapaxes.md index 939bbd433b2..13e2205603f 100644 --- a/content/numpy/concepts/ndarray/terms/swapaxes/swapaxes.md +++ b/content/numpy/concepts/ndarray/terms/swapaxes/swapaxes.md @@ -33,7 +33,7 @@ Returns an `ndarray` view of the original array with `axis1` and `axis2` interch ## Example -The following example creates a 2D array and then swaps its axes: +This example creates a 2D array and then swaps its axes using `.swapaxes()`: ```py import numpy as np @@ -43,7 +43,7 @@ swapped_arr = arr.swapaxes(0, 1) print(swapped_arr) ``` -The code above generates the following output: +Here is the output: ```shell [[1 4] @@ -53,7 +53,7 @@ The code above generates the following output: ## Codebyte Example -The following codebyte example demonstrates the use of `.swapaxes()` on a 3D array. +This codebyte example demonstrates the use of `.swapaxes()` on a 3D array: ```codebyte/python import numpy as np