我有两个专栏,预测和地面真相.
我想用numpy或pandas作为一个系列获得真正积极的数量.
例如,我的数据是:
Prediction GroundTruth
True True
True False
True True
False True
False False
True True
tp_list = [1,1,2,2,2,3]
在numpy或pandas中有这样的单行方式吗?
目前,这是我的解决方案:
tp = 0
for p, g in zip(data.Prediction, data.GroundTruth):
if p and g: # TP case
tp = tp + 1
tp_list.append(tp)
解决方法:
要获得真阳性的运行计数(即累积和),即当且仅当GroundTruth == True时,预测==真,解决方案是@RafaelC答案的修改:
(df['Prediction'] & df['GroundTruth']).cumsum()
0 1
1 1
2 2
3 2
4 2
5 3
(df['Prediction'] & df['GroundTruth']).cumsum().tolist()
[1, 1, 2, 2, 2, 3]