[英]Pandas: Calculate true positive rate for each row
I have a dataframe like this, with one column being the label and the other columns being predictions我有一个像这样的 dataframe,其中一列是 label,其他列是预测
label pred1 pred2 pred3
0 Apple Apple Orange Apple
1 Orange Orange Orange Orange
I would like to extend this dataframe with the true positive rate (TP/TP+FN) for each row.我想用每行的真阳性率 (TP/TP+FN) 扩展这个 dataframe。 This column should look like this:
此列应如下所示:
Score
0 0.66
1 1.00
I am unsure on how to go on about this.我不确定如何 go 对此。 Are there pandas functions that would help with this task?
是否有 pandas 函数可以帮助完成这项任务?
Executable code: https://www.online-python.com/WP7wbgcqMS可执行代码: https://www.online-python.com/WP7wbgcqMS
Here is one approach where we convert the data to long format and check if the label equals the prediction.这是我们将数据转换为长格式并检查 label 是否等于预测的一种方法。 The average of the True/False values will be your Score.
真/假值的平均值将是您的分数。
import pandas as pd
d = {'Label': ['Apple','Orange'], 'pred1': ['Apple','Orange'], 'pred2': ['Orange','Orange'], 'pred3': ['Apple','Orange']}
df = pd.DataFrame(data=d)
df = df.melt(id_vars='Label', value_name='pred')
df['match'] = df['Label'].eq(df['pred'])
df.groupby('Label')['match'].mean().reset_index(name='Score')
Output Output
Label Score
0 Apple 0.666667
1 Orange 1.000000
maybe like this也许像这样
temp = df.T.apply(lambda x: x[0]==x).astype(int)
(temp.sum()-1)/(temp.count()-1)
Out:
0 0.666667
1 1.000000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.