获取列表列表中项目的最高值和位置

时间:2018-04-11 08:49:16

标签: python list scikit-learn

我使用System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. at System.Web.Hosting.IIS7WorkerRequest.SendResponseFromFileStream (System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a) at System.Web.Hosting.IIS7WorkerRequest.SendResponseFromFile (System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a) at System.Web.HttpFileResponseElement.System.Web.IHttpResponseElement.Send (System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a) at System.Web.HttpWriter.Send (System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a) at System.Web.HttpResponse.UpdateNativeResponse (System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a) at System.Web.HttpRuntime.FinishRequestNotification (System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a) 来拟合和预测模型,但我希望每个项目有前5个预测(就概率而言)。 所以我使用sklearn,它给了我一个列表列表,如:

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],这就是我想要的完整列表列表(如果可能的话,还有它旁边的概率)。

当尝试在上面的代码上使用for循环时,它返回[0],[1]

2 个答案:

答案 0 :(得分:0)

这样的事情:

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

答案 1 :(得分:0)

for i in predicted_proba:
    for index, value in enumerate(i):
        if value > 0:
            print(index)

希望这有帮助。