Skip to content

Commit c22131f

Browse files
docs: Add NumPy ndarray .argmax() method documentation (#7911)
* docs: Add NumPy ndarray .argmax() method documentation Created comprehensive NumPy ndarray .argmax() method documentation including: - Complete front matter with Data Science subjects and tags - Clear definition and syntax - Detailed parameters (axis, out) and return values - Three practical code examples with outputs - Codebyte interactive example with student scores Fixes #7859 Submitted for Hacktoberfest 2025. * Update argmax.md ---------
1 parent 2accad5 commit c22131f

File tree

1 file changed

+137
-0
lines changed
  • content/numpy/concepts/ndarray/terms/argmax

1 file changed

+137
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
---
2+
Title: '.argmax()'
3+
Description: 'Returns the indices of the maximum values along a specified axis.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Arrays'
9+
- 'Methods'
10+
- 'NumPy'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/data-science'
14+
---
15+
16+
The **`.argmax()`** method returns the indices of the maximum values along a specified axis in a NumPy ndarray.
17+
18+
## Syntax
19+
20+
```pseudo
21+
ndarray.argmax(axis=None, out=None, *, keepdims=False)
22+
```
23+
24+
**Parameters:**
25+
26+
- `axis` (int, optional): Axis along which to find the maximum value; flattens the array if `None`.
27+
- `out` (ndarray, optional): Output array to store the result; must match the expected shape.
28+
- `keepdims` (bool, optional): If `True`, retains reduced dimensions with size 1.
29+
30+
**Return value:**
31+
32+
Returns an integer or ndarray of integers indicating the indices of the maximum values.
33+
34+
## Example 1: Finding Maximum Index in 1D Array
35+
36+
In this example, the `.argmax()` method returns the index of the maximum value in a one-dimensional array:
37+
38+
```py
39+
import numpy as np
40+
41+
arr = np.array([3, 1, 4, 1, 5, 9, 2])
42+
max_index = arr.argmax()
43+
44+
print("Array:", arr)
45+
print("Index of maximum value:", max_index)
46+
print("Maximum value:", arr[max_index])
47+
```
48+
49+
The output of this code is:
50+
51+
```shell
52+
Array: [3 1 4 1 5 9 2]
53+
Index of maximum value: 5
54+
Maximum value: 9
55+
```
56+
57+
## Example 2: Finding Maximum Indices Along Axis in 2D Array
58+
59+
In this example, the `.argmax()` method finds the indices of maximum values along each axis in a two-dimensional array:
60+
61+
```py
62+
import numpy as np
63+
64+
matrix = np.array([[1, 5, 3],
65+
[9, 2, 8],
66+
[4, 7, 6]])
67+
68+
# Maximum index along axis 0 (columns)
69+
max_col = matrix.argmax(axis=0)
70+
71+
# Maximum index along axis 1 (rows)
72+
max_row = matrix.argmax(axis=1)
73+
74+
print("Matrix:")
75+
print(matrix)
76+
print("\nMax indices along axis 0 (columns):", max_col)
77+
print("Max indices along axis 1 (rows):", max_row)
78+
```
79+
80+
The output of this code is:
81+
82+
```shell
83+
Matrix:
84+
[[1 5 3]
85+
[9 2 8]
86+
[4 7 6]]
87+
88+
Max indices along axis 0 (columns): [1 2 1]
89+
Max indices along axis 1 (rows): [1 0 1]
90+
```
91+
92+
## Example 3: Flattened Array Maximum
93+
94+
In this example, the `.argmax()` method returns the index of the maximum element from the flattened version of the array:
95+
96+
```py
97+
import numpy as np
98+
99+
matrix = np.array([[10, 25, 15],
100+
[30, 20, 35]])
101+
102+
# Find index in flattened array
103+
flat_max_index = matrix.argmax()
104+
105+
print("Matrix:", matrix)
106+
print("Index of maximum in flattened array:", flat_max_index)
107+
print("Maximum value:", matrix.flat[flat_max_index])
108+
```
109+
110+
The output of this code is:
111+
112+
```shell
113+
Matrix: [[10 25 15]
114+
[30 20 35]]
115+
Index of maximum in flattened array: 5
116+
Maximum value: 35
117+
```
118+
119+
## Codebyte Example
120+
121+
In this example, the `.argmax()` method identifies the student with the highest score using their index position:
122+
123+
```codebyte/python
124+
import numpy as np
125+
126+
# Create sample data
127+
scores = np.array([85, 92, 78, 95, 88])
128+
students = ['Alice', 'Bob', 'Carol', 'David', 'Eve']
129+
130+
# Find the student with highest score
131+
top_student_index = scores.argmax()
132+
133+
print(f"Scores: {scores}")
134+
print(f"Highest score index: {top_student_index}")
135+
print(f"Top student: {students[top_student_index]}")
136+
print(f"Score: {scores[top_student_index]}")
137+
```

0 commit comments

Comments
 (0)