Hướng dẫn find minimum index in array python - tìm chỉ số tối thiểu trong mảng python

Tôi cần tìm chỉ số của nhiều hơn một giá trị tối thiểu xảy ra trong một mảng. Tôi khá được biết đến với np.argmin nhưng nó cho tôi chỉ số có giá trị tối thiểu rất đầu tiên trong một mảng. Ví dụ.

a = np.array([1,2,3,4,5,1,6,1])    
print np.argmin(a)

Điều này cho tôi 0, thay vào đó tôi mong đợi, 0,5,7.

Cảm ơn!

Hướng dẫn find minimum index in array python - tìm chỉ số tối thiểu trong mảng python

Đã hỏi ngày 23 tháng 10 năm 2013 lúc 16:09Oct 23, 2013 at 16:09

2

Cái này cần phải dùng mẹo:

a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())

Argmin không trả lại một danh sách như bạn mong đợi trong trường hợp này.

Đã trả lời ngày 23 tháng 10 năm 2013 lúc 16:21Oct 23, 2013 at 16:21

Hướng dẫn find minimum index in array python - tìm chỉ số tối thiểu trong mảng python

Tom Swiftytom SwiftyTom Swifty

2.7942 Huy hiệu vàng16 Huy hiệu bạc25 Huy hiệu Đồng2 gold badges16 silver badges25 bronze badges

2

Có lẽ

mymin = np.min(a)
min_positions = [i for i, x in enumerate(a) if x == mymin]

Nó sẽ cho [0,5,7].

Đã trả lời ngày 23 tháng 10 năm 2013 lúc 16:20Oct 23, 2013 at 16:20

Hướng dẫn find minimum index in array python - tìm chỉ số tối thiểu trong mảng python

Tonjotonjotonjo

1.3461 Huy hiệu vàng14 Huy hiệu bạc27 Huy hiệu đồng1 gold badge14 silver badges27 bronze badges

2

Tôi nghĩ rằng đây sẽ là cách dễ nhất, mặc dù nó không sử dụng bất kỳ chức năng Numpy lạ mắt nào

a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)

Đã trả lời ngày 23 tháng 10 năm 2013 lúc 16:26Oct 23, 2013 at 16:26

jrk0414jrk0414jrk0414

1341 Huy hiệu vàng1 Huy hiệu bạc11 Huy hiệu đồng1 gold badge1 silver badge11 bronze badges

Đưa ra một danh sách N số nguyên, hãy tìm vị trí phần tử tối đa và tối thiểu trong danh sách Python. & NBSP;

Example:

Input :  3, 4, 1, 3, 4, 5
Output :  The maximum is at position 6
          The minimum is at position 3

Phương pháp 1: Sử dụng phương pháp bản địa

Cách tiếp cận ngây thơ sẽ là đi qua trong danh sách và theo dõi mức tối thiểu và tối đa cùng với các chỉ số của họ. Chúng ta phải thực hiện so sánh N cho tối thiểu và đồng thời so sánh N cho tối đa. Dưới đây là việc thực hiện phương pháp ngây thơ. & NBSP;naive approach will be to traverse in the list and keep track of the minimum and maximum along with their indices. We have to do N comparisons for minimum and at the same time N comparisons for maximum. Below is the implementation of the naive approach. 

Python3

gfg_list

a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
0
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
1
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
2
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
3
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
4
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
3
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
6
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
3
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
8
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
3__

mymin = np.min(a)
min_positions = [i for i, x in enumerate(a) if x == mymin]
2
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
0
mymin = np.min(a)
min_positions = [i for i, x in enumerate(a) if x == mymin]
4
mymin = np.min(a)
min_positions = [i for i, x in enumerate(a) if x == mymin]
5
mymin = np.min(a)
min_positions = [i for i, x in enumerate(a) if x == mymin]
6
mymin = np.min(a)
min_positions = [i for i, x in enumerate(a) if x == mymin]
5
mymin = np.min(a)
min_positions = [i for i, x in enumerate(a) if x == mymin]
1

mymin = np.min(a)
min_positions = [i for i, x in enumerate(a) if x == mymin]
9
a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
0
a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
1
a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
2
a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
3
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
4
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
3
a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
6
a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
7

a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
8
a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
9
Input :  3, 4, 1, 3, 4, 5
Output :  The maximum is at position 6
          The minimum is at position 3
0

Input :  3, 4, 1, 3, 4, 5
Output :  The maximum is at position 6
          The minimum is at position 3
1
Input :  3, 4, 1, 3, 4, 5
Output :  The maximum is at position 6
          The minimum is at position 3
2
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
0
Input :  3, 4, 1, 3, 4, 5
Output :  The maximum is at position 6
          The minimum is at position 3
4

a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
8
a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
9
Input :  3, 4, 1, 3, 4, 5
Output :  The maximum is at position 6
          The minimum is at position 3
7

Input :  3, 4, 1, 3, 4, 5
Output :  The maximum is at position 6
          The minimum is at position 3
1
Input :  3, 4, 1, 3, 4, 5
Output :  The maximum is at position 6
          The minimum is at position 3
9____10
Input :  3, 4, 1, 3, 4, 5
Output :  The maximum is at position 6
          The minimum is at position 3
4

Minimum Element in the list [8, 1, 7, 10, 5] is 1
Maximum Element in the list [8, 1, 7, 10, 5] is 10
2
a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
3
Minimum Element in the list [8, 1, 7, 10, 5] is 1
Maximum Element in the list [8, 1, 7, 10, 5] is 10
4
Minimum Element in the list [8, 1, 7, 10, 5] is 1
Maximum Element in the list [8, 1, 7, 10, 5] is 10
5
Minimum Element in the list [8, 1, 7, 10, 5] is 1
Maximum Element in the list [8, 1, 7, 10, 5] is 10
6
Minimum Element in the list [8, 1, 7, 10, 5] is 1
Maximum Element in the list [8, 1, 7, 10, 5] is 10
7

Minimum Element in the list [8, 1, 7, 10, 5] is 1
Maximum Element in the list [8, 1, 7, 10, 5] is 10
2
a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
3
The maximum is at position 6
The minimum is at position 3
0
Minimum Element in the list [8, 1, 7, 10, 5] is 1
Maximum Element in the list [8, 1, 7, 10, 5] is 10
5
Minimum Element in the list [8, 1, 7, 10, 5] is 1
Maximum Element in the list [8, 1, 7, 10, 5] is 10
6
The maximum is at position 6
The minimum is at position 3
3

Output:

Minimum Element in the list [8, 1, 7, 10, 5] is 1
Maximum Element in the list [8, 1, 7, 10, 5] is 10

Phương pháp 2: Sử dụng hàm inbuild

Chức năng sẵn có của Python cho phép chúng tôi tìm thấy nó trong một dòng, chúng tôi có thể tìm thấy mức tối thiểu trong danh sách bằng hàm min () và sau đó sử dụng hàm index () để tìm ra chỉ mục của phần tử tối thiểu đó. Tương tự, chúng ta có thể thực hiện tương tự để tìm ra phần tử tối đa bằng cách sử dụng hàm tối đa () và sau đó tìm ra chỉ số của phần tử tối đa bằng cách sử dụng hàm sẵn có chỉ số () trong Python. & NBSP; allows us to find it in one line, we can find the minimum in the list using the min() function and then use the index() function to find out the index of that minimum element. Similarly we can do the same for finding out the maximum element using the max() function and then find out the index of the maximum element using the index() inbuilt function in python. 

Lưu ý: Index () Trả về chỉ số của lần xuất hiện đầu tiên trong trường hợp có nhiều lần xuất hiện của một phần tử. Vì vậy, nếu tối đa (hoặc tối thiểu) xảy ra nhiều lần, lần xuất hiện đầu tiên được trả lại. Dưới đây là việc thực hiện phương pháp trên: & nbsp;index() returns the index of the first occurrence in case there are multiple occurrences of an element. So if maximum (or minimum) occurs more than once, the first occurrence is returned. Below is the implementation of the above approach: 

Python3

The maximum is at position 6
The minimum is at position 3
4
The maximum is at position 6
The minimum is at position 3
5

a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
8
The maximum is at position 6
The minimum is at position 3
7
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
0
The maximum is at position 6
The minimum is at position 3
9
Min:  8
Max:  5
0
Min:  8
Max:  5
1

a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
8
Min:  8
Max:  5
3
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
0
The maximum is at position 6
The minimum is at position 3
9
Min:  8
Max:  5
6
Min:  8
Max:  5
1

a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
8
Minimum Element in the list [8, 1, 7, 10, 5] is 1
Maximum Element in the list [8, 1, 7, 10, 5] is 10
2 np.argmin0np.argmin1np.argmin2
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
4

a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
8
Minimum Element in the list [8, 1, 7, 10, 5] is 1
Maximum Element in the list [8, 1, 7, 10, 5] is 10
2 np.argmin6np.argmin7np.argmin2
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
4

gfg_list 0

a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
0
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
1gfg_list 3
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
3gfg_list 5____13
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
4
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
3gfg_list 3
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
3gfg_list 5__13____

a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
05
a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
6
Min:  8
Max:  5
1

Output:

The maximum is at position 6
The minimum is at position 3

Phương pháp 3: Sử dụng gấu trúc

Trong phương thức này, chúng tôi sẽ sử dụng idxmin () và idxmax () để in chỉ mục tối đa và chỉ mục tối thiểu bằng mô -đun Pandas.

Python3

a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
08
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
09

Is

a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
23
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
24
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
3
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
26
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
3
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
21
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
3__

a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
23
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
35
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
3
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
19
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
3
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
39
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
3
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
41
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
3
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
43
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
3
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
13
mymin = np.min(a)
min_positions = [i for i, x in enumerate(a) if x == mymin]
1

Minimum Element in the list [8, 1, 7, 10, 5] is 1
Maximum Element in the list [8, 1, 7, 10, 5] is 10
2
a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
3
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
49
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
50

Minimum Element in the list [8, 1, 7, 10, 5] is 1
Maximum Element in the list [8, 1, 7, 10, 5] is 10
2
a       = np.array([1,2,3,4,5,1,6,1])                                        
min_val = a.min()                                                            

print "min_val = {0}".format(min_val)                                        

# Find all of them                                                           
min_idxs = [idx for idx, val in enumerate(a) if val == min_val]              
print "min_idxs = {0}".format(min_idxs)
3
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
53
a = np.array([1,2,3,4,5,1,6,1]) 
print np.where(a == a.min())
54

Output:

Min:  8
Max:  5