For 循环基于两个过滤列



我正在尝试根据实际和预测列创建一个新列("Conf_Type"(。实际和预测列的输出有四种组合:(0,0(、(1,1(、(0,1(、(1,0(,我正在尝试将它们分类到新列中。

      Actual   Predicted     A         B
0         1          1  0.002753  0.997247
1         0          0  0.909696  0.090304
2         1          1  0.100924  0.899076
3         0          1  0.114059  0.885941
4         1          0  0.237289  0.762711
5         1          1  0.077710  0.922290
6         0          0  0.677748  0.322252
7         1          1  0.096327  0.903673
8         0          1  0.039741  0.960259
9         0          1  0.096884  0.903116
10        1          1  0.045345  0.954655

我尝试使用"for"循环,但它不断出现值错误。

Conf_Type = []
for row in visual:
    if visual['Actual'] == 1 & visual['Predicted'] == 1:
        Conf_Type.append('True Negative')
    elif visual['Actual'] == 0 & visual['Predicted'] == 0:
        Conf_Type.append('True Positive')
    elif visual['Actual'] == 1 & visual['Predicted'] == 0:
        Conf_Type.append('False Positive')
    elif visual['Actual'] == 0 & visual['Predicted'] == 1:
        Conf_Type.append('False Negative')
visual['Conf_Type'] = Conf_Type
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

想知道在这种情况下"for"循环是否正确,或者我应该使用另一种方法。

你应该像我在这里一样使用apply。

number_meaning = ['true positive', 'false negative', 'false positive', 'true negative']
df['categorized'] = df['Actual'] + 2 * df['Predicted']
df['result'] = df['categorized'].apply(lambda x: number_meaning[x])

或使用您定义的函数:

def meaning(row):
    if row['Actual'] == 1 & row['Predicted'] == 1:
        return 'True Negative'
    elif row['Actual'] == 0 & row['Predicted'] == 0:
        return 'True Positive'
    elif row['Actual'] == 1 & row['Predicted'] == 0:
        return 'False Positive'
    elif row['Actual'] == 0 & row['Predicted'] == 1:
        return 'False Negative'
df['result'] = df.apply(meaning, axis=1)
# iterate every row by row number
for i in list(df.index):
    if df.loc[i,"Actual"] ==df.loc[i,"Predicted"]==0 or df.loc[i,"Actual"] ==df.loc[i,"Predicted"]==1:
        # set up the value of row 'i', column 'Conf_Type' based on this condition
        df.loc[i, "Conf_Type"] = True
    else:
        df.loc[i, "Conf_Type"] = False

最新更新