簡體   English   中英

獲取列表中項目的最高價值和位置

[英]Get top values and positions of items in list of lists

我已經使用sklearn來擬合和預測模型,但是我希望每個項目都具有前5個預測(以概率而言)。 因此,我使用了predict_proba ,它給了我類似的列表列表:

probabilities = [[0.8,0.15,0.5,0,0],[0.4,0.6,0,0,0],[0,0,0,0,1]]我想做的是遍歷此列表列表,可讓我大致了解所做的每個預測,以及預測在列表中的位置(代表類別)。

[i for i, j in enumerate(predicted_proba[0]) if j > 0]使用[i for i, j in enumerate(predicted_proba[0]) if j > 0]則返回我[0],[1] ,這是我想要的完整列表列表(如果可能的話,還可以使用概率)。

當嘗試對上述代碼使用for循環時,它返回IndexError

像這樣:

probabilities = [[0.8, 0.15, 0.5, 0, 0], [0.4, 0.6, 0, 0, 0], [0, 0, 0, 0, 1]]
for list in range(0,len(probabilities)):
    print("Iteration_number:", list)
    for index, prob in enumerate(probabilities[list]):
        print("index", index, "=", prob)

結果是:

Iteration_number: 0
index 0 = 0.8
index 1 = 0.15
index 2 = 0.5
index 3 = 0
index 4 = 0
Iteration_number: 1
index 0 = 0.4
index 1 = 0.6
index 2 = 0
index 3 = 0
index 4 = 0
Iteration_number: 2
index 0 = 0
index 1 = 0
index 2 = 0
index 3 = 0
index 4 = 1
for i in predicted_proba:
    for index, value in enumerate(i):
        if value > 0:
            print(index)

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM