Python for循环以测试分类问题中的临界值

问题描述 投票:0回答:2

我需要编写代码来测试分类问题的临界值列表。要测试的值存储在cutoff_list变量中。然后,我想将结果混淆矩阵列表放入字典中。但是,下面的代码仅给我第一个字典条目(第一个测试值的混淆矩阵):

cutoff_list = [np.arange(0,1,0.01)]  # list of test values
dictionary = {}  
for i, v in enumerate(cutoff_list):
    actual = (df.observed)
    predicted = np.where(df.indicator > i, 1, 0)

    df_confusion = confusion_matrix(actual, predicted) / len(df.indicator)   

    dictionary[i] = df_confusion

print(dictionary)

循环或字典更新步骤有问题吗?我是Python的新手,对R有更多的经验,仍然在这里苦苦挣扎。任何帮助表示赞赏。

图书馆:

from pandas import Series, DataFrame
import pandas as pd
import numpy as np
from sklearn.metrics import confusion_matrix
python dictionary for-loop append confusion-matrix
2个回答
0
投票

您的for循环只运行一次,因为您的设置方式。

您将np.arrange(0.1.0.01)的返回包含在一个列表中,这破坏了您希望for循环运行的方式。您仅获得一个值,因为for循环仅运行一次,因为外部列表只有一项。

cutoff_list = [np.arange(0,1,0.01)]行更改为cutoff_list = np.arange(0,1,0.01),看看是否能解决您的问题。

我也想像您要在此行中使用v而不是i

    predicted = np.where(df.indicator > i, 1, 0)

因为i仅保留用作字典键的枚举值,而v将保留cutoff_values中的值。


-1
投票

请列出导入库

© www.soinside.com 2019 - 2024. All rights reserved.