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ỏ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
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
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[]]
1a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
2a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
3a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
4a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
3a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
6a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
3a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
8a = 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]
2a = 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]
4mymin = np.min[a]
min_positions = [i for i, x in enumerate[a] if x == mymin]
5mymin = np.min[a]
min_positions = [i for i, x in enumerate[a] if x == mymin]
6mymin = np.min[a]
min_positions = [i for i, x in enumerate[a] if x == mymin]
5mymin = np.min[a]
min_positions = [i for i, x in enumerate[a] if x == mymin]
1mymin = 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]
0a = 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]
2a = 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]
3a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
4a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
3a = 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]
6a = 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]
7a = 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]
8a = 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 30
Input : 3, 4, 1, 3, 4, 5 Output : The maximum is at position 6 The minimum is at position 31
Input : 3, 4, 1, 3, 4, 5 Output : The maximum is at position 6 The minimum is at position 32
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 34
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]
8a = 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 37
Input : 3, 4, 1, 3, 4, 5 Output : The maximum is at position 6 The minimum is at position 31
Input : 3, 4, 1, 3, 4, 5 Output : The maximum is at position 6 The minimum is at position 39____10
Input : 3, 4, 1, 3, 4, 5 Output : The maximum is at position 6 The minimum is at position 34
Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 102
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]
3Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 104
Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 105
Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 106
Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 107
Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 102
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]
3The maximum is at position 6 The minimum is at position 30
Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 105
Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 106
The maximum is at position 6 The minimum is at position 33
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 34
The maximum is at position 6 The minimum is at position 35
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]
8The maximum is at position 6 The minimum is at position 37
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 39
Min: 8 Max: 50
Min: 8 Max: 51
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]
8Min: 8 Max: 53
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 39
Min: 8 Max: 56
Min: 8 Max: 51
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]
8Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 102
np.argmin
0np.argmin
1np.argmin
2 a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
4a = 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]
8Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 102
np.argmin
6np.argmin
7np.argmin
2 a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
4gfg_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
3a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
3gfg_list
5____13a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
4a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
3gfg_list
3a = 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[]]
05a = 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]
6Min: 8 Max: 51
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[]]
09Is
a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
23a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
24a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
3a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
26a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
3a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
21a = 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[]]
23a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
35a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
3a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
19a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
3a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
39a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
3a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
41a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
3a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
43a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
3a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
13mymin = np.min[a]
min_positions = [i for i, x in enumerate[a] if x == mymin]
1Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 102
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]
3a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
49a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
50Minimum Element in the list [8, 1, 7, 10, 5] is 1 Maximum Element in the list [8, 1, 7, 10, 5] is 102
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]
3a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
53a = np.array[[1,2,3,4,5,1,6,1]]
print np.where[a == a.min[]]
54Output:
Min: 8 Max: 5