I have two columns, Prediction and Ground Truth. I want to get a count of true positives as a series using either numpy or pandas.
For example, my data is:
Prediction GroundTruth
True True
True False
True True
False True
False False
True True
I want a list that should have the following output:
tp_list = [1,1,2,2,2,3]
Is there a one-liner way to do this in numpy or pandas?
Currently, this is my solution:
tp = 0
for p, g in zip(data.Prediction, data.GroundTruth):
if p and g: # TP case
tp = tp + 1
tp_list.append(tp)
To get a running count (ie, cumulative sum) of true positives , ie, Prediction == True
if and only if GroundTruth == True
, the solution is a modification of @RafaelC's answer:
(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]
If you want to know how many True
you predicted that are actually True
, use
(df['Prediction'] & df['GroundTruth']).cumsum()
0 1
1 1
2 2
3 2
4 2
5 3
dtype: int64
(thanks @Peter Leimbigiler for chiming in)
If you want to know how many you have predicted correctly just compare and use cumsum
(df['Prediction'] == df['GroundTruth']).cumsum()
which outputs
0 1
1 1
2 2
3 2
4 3
5 4
dtype: int64
Can always get a list by using .tolist()
(df4['Prediction'] == df4['GroundTruth']).cumsum().tolist()
[1, 1, 2, 2, 3, 4]
Maybe you can using all
df.all(1).cumsum().tolist()
Out[156]: [1, 1, 2, 2, 2, 3]
numpy
solution
np.cumsum(np.all(df.values,1))
Out[159]: array([1, 1, 2, 2, 2, 3], dtype=int32)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.