Skip to content

Commit feb94ff

Browse files
[Term Entry] Python NumPy - ndarray: argmin()
* [Edit] Python: Python CLI arguments * Update command-line-arguments.md * [Term Entry] PyTorch Tensor Operations: .log2() * [Term Entry] Python NumPy - ndarray: argmin() * Delete docs/content/pytorch/concepts/tensor-operations/terms/log2/log2.md * Update argmin.md ---------
1 parent 4b38b94 commit feb94ff

File tree

1 file changed

+105
-0
lines changed
  • content/numpy/concepts/ndarray/terms/argmin

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
Title: 'argmin()'
3+
Description: 'Returns the index of the minimum value in a NumPy array or along a specified axis.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Array'
9+
- 'Data'
10+
- 'NumPy'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/data-science'
14+
---
15+
16+
The **`ndarray.argmin()`** method returns the index of the minimum value in a NumPy array. The search can be performed on the flattened array or along a specified axis, and the result reflects where the smallest element appears rather than the value itself.
17+
18+
## Syntax
19+
20+
```pseudo
21+
ndarray.argmin(axis=None, out=None, *, keepdims=False)
22+
```
23+
24+
**Parameters:**
25+
26+
- `axis` (optional): Axis along which to find the minimum index.
27+
- `None` (default): Searches the entire flattened array.
28+
- `0`: Searches column-wise
29+
- `1`: Searches row-wise
30+
- `out` (optional): Output array that receives the result. Must have the appropriate shape.
31+
- `keepdims` (optional): If `True`, the reduced axes are kept with size 1, preserving the dimension structure.
32+
33+
**Return value:**
34+
35+
An integer or array of integers representing the indices of the minimum values.
36+
37+
## Example 1: Finding the Minimum Index in a 1D Array
38+
39+
In this example, the smallest number in the array is identified, and its index is returned:
40+
41+
```py
42+
import numpy as np
43+
44+
arr = np.array([12, 5, 7, 3, 9])
45+
idx = arr.argmin()
46+
print(idx)
47+
```
48+
49+
The output of this code is:
50+
51+
```shell
52+
3
53+
```
54+
55+
## Example 2: Finding Minimum Indices Along Rows
56+
57+
In this example, `argmin()` is applied along `axis=1`, so each row returns the index of its smallest element:
58+
59+
```py
60+
import numpy as np
61+
62+
arr = np.array([[4, 9, 1],
63+
[8, 3, 6]])
64+
65+
idx = arr.argmin(axis=1)
66+
print(idx)
67+
```
68+
69+
The output of this code is:
70+
71+
```shell
72+
[2 1]
73+
```
74+
75+
## Codebyte Example
76+
77+
In this example, the minimum index is found both in the flattened array and along each column to show how the output changes with the `axis` parameter:
78+
79+
```codebyte/python
80+
import numpy as np
81+
82+
arr = np.array([[15, 7, 9],
83+
[2, 11, 5],
84+
[6, 4, 8]])
85+
86+
flat_index = arr.argmin()
87+
col_indices = arr.argmin(axis=0)
88+
89+
print("Index of minimum in flattened array:", flat_index)
90+
print("Index of minimum in each column:", col_indices)
91+
```
92+
93+
## Frequently Asked Questions
94+
95+
### 1. What does NumPy `argmin()` do?
96+
97+
NumPy's `argmin()` returns the index location of the smallest element inside an array. Instead of giving the minimum value, it identifies where that value appears, which is essential for position-based analysis.
98+
99+
### 2. What is the difference between `argmin()` and `nanargmin()`?
100+
101+
`argmin()` considers all values, including NaNs, while `nanargmin()` ignores NaNs and returns the index of the smallest non-NaN value.
102+
103+
### 3. What is the difference between `argmin()` and `min()` in Python?
104+
105+
`min()` (or `ndarray.min()`) returns the smallest value itself, while `argmin()` returns the index where that value occurs. One answers “what is the smallest value”, and the other answers “where is that smallest value located”.

0 commit comments

Comments
 (0)