Skip to content

Commit 476d6d0

Browse files
Jayden LimJayden Lim
authored andcommitted
feat(numpy): add docs for ndarray.swapaxes
1 parent 94f9513 commit 476d6d0

File tree

1 file changed

+63
-0
lines changed
  • content/numpy/concepts/ndarray/terms/swapaxes

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
Title: '.swapaxes()'
3+
Description: 'Interchanges two axes of a NumPy ndarray.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Arrays'
9+
- 'Data Structures'
10+
- 'Functions'
11+
- 'NumPy'
12+
CatalogContent:
13+
- 'learn-python-3'
14+
- 'paths/data-science'
15+
---
16+
17+
The **`.swapaxes()`** method returns a view of the array with the specified axes interchanged, without copying the data.
18+
It is useful for reorienting multi-dimensional arrays for analysis, transformation or visualization.
19+
20+
## Syntax
21+
22+
```pseudo
23+
ndarray.swapaxes(axis1, axis2)
24+
```
25+
**Parameters:**
26+
- `axis1`: An `int` representing the first axis to be swapped.
27+
- `axis2`: An `int` representing the second axis to be swapped.
28+
29+
**Return value:**
30+
31+
Returns a `ndarray` that is a view of the original array with `axis1` and `axis2` interchanged.
32+
33+
## Example
34+
35+
The following example creates a 2D array and then swaps its axes:
36+
37+
```py
38+
import numpy as np
39+
40+
arr = np.array([[1, 2, 3], [4, 5, 6]])
41+
swapped_arr = arr.swapaxes(0, 1)
42+
print(swapped_arr)
43+
```
44+
45+
The code above generates the following output:
46+
47+
```shell
48+
[[1 4]
49+
[2 5]
50+
[3 6]]
51+
```
52+
53+
## Codebyte Example
54+
55+
The following codebyte example demonstrates the use of `.swapaxes()` on a 3D array.
56+
57+
```codebyte/python
58+
import numpy as np
59+
60+
arr = np.array([[10, 20, 30], [40, 50, 60]])
61+
swapped_arr = arr.swapaxes(0, 1)
62+
print(swapped_arr)
63+
```

0 commit comments

Comments
 (0)