我正在尝试根据“实际”和“预测”列创建一个新列(“ 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”循环是正确的还是应该使用其他方法。
答案 0 :(得分:0)
您应该像在这里一样使用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)
答案 1 :(得分:0)
# 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