Skip to content

Commit 3d42b93

Browse files
[Term Entry] Python - NumPy built-in functions: .argmax() (#6434)
* [Term Entry] Python - NumPy built-in functions: .argmax() * Update argmax.md * Update argmax.md
1 parent 1670a0a commit 3d42b93

File tree

1 file changed

+151
-34
lines changed
  • content/numpy/concepts/built-in-functions/terms/argmax

1 file changed

+151
-34
lines changed
Lines changed: 151 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
Title: '.argmax()'
3-
Description: 'Returns the indices of the maximum values along a specified axis in an array.'
3+
Description: 'Returns the indices of the maximum values along a specified axis in an array'
44
Subjects:
55
- 'Computer Science'
66
- 'Data Science'
@@ -14,7 +14,9 @@ CatalogContent:
1414
- 'paths/data-science'
1515
---
1616

17-
The NumPy **`.argmax()`** function returns the indices of the maximum values along a specified axis in an [array](https://www.codecademy.com/resources/docs/numpy/ndarray).
17+
The NumPy **`.argmax()`** function returns the indices of the maximum values along a specified axis in an array. This powerful function helps identify the positions of peak values within arrays, rather than the values themselves. `.argmax()` is particularly useful in data analysis, machine learning, and scientific computing where locating maximal elements in datasets is essential.
18+
19+
In data science workflows, `.argmax()` serves multiple purposes including finding winning classifications in neural networks, locating peaks in signal processing, and identifying optimal parameters in optimization problems. By returning indices instead of values, it provides positional information that can be used for further data manipulation or analysis.
1820

1921
## Syntax
2022

@@ -24,63 +26,178 @@ numpy.argmax(array, axis=None, out=None, keepdims=<no value>)
2426

2527
**Parameters:**
2628

27-
- `array`: The input array in which to find the indices of maximum values.
28-
- `axis` (Optional): The axis along which to find the maximum values. If `None` (default), the function works on the flattened array.
29-
- `out` (Optional): An array to place the result in. It must match the expected output shape.
30-
- `keepdims` (Optional): If `True`, the reduced dimensions are kept with size `1`, making the output broadcast-compatible with the input.
29+
- `array`: Input array to find maximum values in.
30+
- `axis`(Optional): Integer specifying the axis along which to find maximum values. By default (`None`), the index is calculated for the flattened array.
31+
- `out` (Optional): If provided, the result will be inserted into this array. It should have the appropriate shape and dtype.
32+
- `keepdims` (Optional): If `True`, the axes which are reduced are left in the result as dimensions with size one, allowing the result to broadcast correctly against the input array.
3133

3234
**Return value:**
3335

34-
Returns an integer index for 1D arrays and a NumPy array of indices for multi-dimensional arrays, indicating the positions of the maximum values along the specified axis.
36+
Returns an array of indices into the array. It has the same shape as the input array with the dimension along the specified axis removed, unless `keepdims` is set to `True`.
37+
38+
## Example 1: Finding Maximum Element Position in a 1D Array
39+
40+
The simplest application of `.argmax()` is finding the position of the maximum value in a one-dimensional array. This is useful when the position itself carries meaningful information, as illustrated in the following example:
41+
42+
```py
43+
import numpy as np
44+
45+
# Create a simple 1D array
46+
temperature_readings = np.array([23.5, 25.2, 24.8, 28.9, 26.1, 22.3])
47+
48+
# Find the position of the highest temperature
49+
max_temp_position = np.argmax(temperature_readings)
50+
51+
print("Temperature readings:", temperature_readings)
52+
print("Position of maximum value:", max_temp_position)
53+
print("Maximum temperature value:", temperature_readings[max_temp_position])
54+
```
55+
56+
This example results in the following output:
57+
58+
```shell
59+
Temperature readings: [23.5 25.2 24.8 28.9 26.1 22.3]
60+
Position of maximum value: 3
61+
Maximum temperature value: 28.9
62+
```
63+
64+
The code identifies position 3 (zero-indexed) as containing the maximum temperature of 28.9, making it easy to pinpoint exactly when the highest temperature occurred.
3565

36-
## Example
66+
## Example 2: Working with Multi-dimensional Arrays
3767

38-
The following example demonstrates how to use the `.argmax()` function with 1D and 2D arrays:
68+
`.argmax()` becomes especially powerful when working with multi-dimensional arrays. By specifying the axis parameter, maximum values along rows or columns can be found, making it useful for tasks like identifying the highest value in each feature of a dataset, as demonstrated here:
3969

4070
```py
4171
import numpy as np
4272

43-
# Create a 1D array
44-
scores = np.array([10, 30, 20, 50, 40])
73+
# Create a 2D array representing sales data for 3 products across 4 quarters
74+
sales_data = np.array([
75+
[105, 140, 95, 115], # Product A sales
76+
[120, 110, 125, 130], # Product B sales
77+
[90, 100, 110, 105] # Product C sales
78+
])
79+
80+
# Find which quarter had highest sales for each product (axis=1 means operate along rows)
81+
best_quarter = np.argmax(sales_data, axis=1)
82+
83+
# Find which product had highest sales in each quarter (axis=0 means operate along columns)
84+
best_product = np.argmax(sales_data, axis=0)
85+
86+
print("Sales data:")
87+
print(sales_data)
88+
print("\nBest quarter for each product:", best_quarter)
89+
print("Quarter names for reference: [Q1, Q2, Q3, Q4]")
90+
print("Best product for each quarter:", best_product)
91+
print("Product names for reference: [Product A, Product B, Product C]")
92+
```
93+
94+
This example provides the following output:
95+
96+
```shell
97+
Sales data:
98+
[[105 140 95 115]
99+
[120 110 125 130]
100+
[ 90 100 110 105]]
101+
102+
Best quarter for each product: [1 3 2]
103+
Quarter names for reference: [Q1, Q2, Q3, Q4]
104+
Best product for each quarter: [1 0 1 1]
105+
Product names for reference: [Product A, Product B, Product C]
106+
```
45107

46-
# Find max indices
47-
max_index = np.argmax(scores)
108+
The results tell us that Product A performs best in Q2, Product B in Q4, and Product C in Q3. Additionally, the best-selling product per quarter is: Product B for Q1, Product A for Q2, and Product B for both Q3 and Q4.
48109

49-
print("Index of max value:", max_index)
50-
print("Max value:", scores[max_index])
110+
## Example 3: Machine Learning Applications with keepdims
51111

52-
# Create a 2D array
53-
matrix = np.array([[1, 5, 3],
54-
[4, 2, 6]])
112+
The `.argmax()` function is commonly used in machine learning to identify predicted classes from probability outputs. The `keepdims` parameter helps maintain the original dimensions, making it easier to integrate into larger computational graphs, as shown in this case:
55113

56-
# Find max indices along columns (axis=0)
57-
print("Max indices per column:", np.argmax(matrix, axis=0))
114+
```py
115+
import numpy as np
58116

59-
# Find max indices along rows (axis=1)
60-
print("Max indices per row:", np.argmax(matrix, axis=1))
117+
# Simulated output from a neural network - probabilities for 3 classes across 4 samples
118+
class_probabilities = np.array([
119+
[0.2, 0.7, 0.1], # Sample 1 predictions
120+
[0.8, 0.1, 0.1], # Sample 2 predictions
121+
[0.25, 0.25, 0.5], # Sample 3 predictions
122+
[0.4, 0.4, 0.2] # Sample 4 predictions
123+
])
124+
125+
# Find the predicted class (highest probability) for each sample
126+
predicted_classes = np.argmax(class_probabilities, axis=1)
127+
128+
# Using keepdims to maintain dimensionality for further processing
129+
predicted_with_dims = np.argmax(class_probabilities, axis=1, keepdims=True)
130+
131+
print("Neural network class probabilities:")
132+
print(class_probabilities)
133+
print("\nPredicted class for each sample:", predicted_classes)
134+
print("Class names for reference: [Class A, Class B, Class C]")
135+
print("\nWith keepdims=True:")
136+
print(predicted_with_dims)
137+
print("Shape without keepdims:", predicted_classes.shape)
138+
print("Shape with keepdims:", predicted_with_dims.shape)
139+
140+
# Demonstrate how keepdims helps with broadcasting
141+
# Convert predictions to one-hot encoding
142+
num_classes = class_probabilities.shape[1]
143+
one_hot = np.zeros_like(class_probabilities)
144+
one_hot[np.arange(len(predicted_classes)), predicted_classes] = 1
145+
146+
print("\nOne-hot encoded predictions:")
147+
print(one_hot)
61148
```
62149

63-
The code above produces the following output:
150+
This example produces the following output:
64151

65152
```shell
66-
Index of max value: 3
67-
Max value: 50
68-
Max indices per column: [1 0 1]
69-
Max indices per row: [1 2]
153+
Neural network class probabilities:
154+
[[0.2 0.7 0.1 ]
155+
[0.8 0.1 0.1 ]
156+
[0.25 0.25 0.5 ]
157+
[0.4 0.4 0.2 ]]
158+
159+
Predicted class for each sample: [1 0 2 0]
160+
Class names for reference: [Class A, Class B, Class C]
161+
162+
With keepdims=True:
163+
[[1]
164+
[0]
165+
[2]
166+
[0]]
167+
Shape without keepdims: (4,)
168+
Shape with keepdims: (4, 1)
169+
170+
One-hot encoded predictions:
171+
[[0. 1. 0.]
172+
[1. 0. 0.]
173+
[0. 0. 1.]
174+
[1. 0. 0.]]
70175
```
71176

177+
This example demonstrates how `.argmax()` helps convert neural network probability outputs into class predictions, and also showcases how the `keepdims` parameter preserves the array dimensions for further operations like broadcasting.
178+
72179
## Codebyte Example
73180

74-
Run the following code to understand the working of the `.argmax()` function:
181+
In educational data analysis, `.argmax()` can be used to identify the top-performing student in each subject based on test scores. The following example demonstrates how to find which student scored the highest in different subjects:
75182

76-
```codebyte/python
183+
```codebyte/py
77184
import numpy as np
78185
79-
# Create a 2D array
80-
data = np.array([[10, 20, 30], [40, 50, 60]])
186+
# Create a 2D array representing scores of 4 students in 3 subjects
187+
scores = np.array([
188+
[85, 90, 78], # Student 1
189+
[88, 76, 92], # Student 2
190+
[79, 85, 88], # Student 3
191+
[91, 89, 84] # Student 4
192+
])
193+
194+
# Find the best student in each subject (axis=0 means finding max index for each column)
195+
best_students = np.argmax(scores, axis=0)
81196
82-
# Find the index of the maximum value along axis 1 (rows)
83-
result = np.argmax(data, axis=1)
197+
# Subject names for reference
198+
subjects = ["Math", "Science", "English"]
84199
85-
print(result)
200+
# Display results
201+
for i, subject in enumerate(subjects):
202+
print(f"Top scorer in {subject}: Student {best_students[i] + 1}")
86203
```

0 commit comments

Comments
 (0)