如何使用pandas或numpy计算真阳性的出现?

我有两个专栏,预测和地面真相.
我想用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]

相关文章

转载:一文讲述Pandas库的数据读取、数据获取、数据拼接、数...
Pandas是一个开源的第三方Python库,从Numpy和Matplotlib的基...
整体流程登录天池在线编程环境导入pandas和xrld操作EXCEL文件...
 一、numpy小结             二、pandas2.1为...
1、时间偏移DateOffset对象DateOffset类似于时间差Timedelta...
1、pandas内置样式空值高亮highlight_null最大最小值高亮背景...