gpt4 book ai didi

python - 删除行直到达到度量点并提取最小值

转载 作者:行者123 更新时间:2023-12-01 06:59:35 24 4
gpt4 key购买 nike

我正在使用机器学习模型对一些文本进行分类。本质上,我将 80% 的数据拟合到模型中,并预测剩余的 20%。除此之外,对于每个分类,我都会输出 ML 模型给出的置信度和一个 check 变量,如果预测为,则该变量设置为 TRUE正确,否则FALSE

我从上述过程中获得的数据帧输出如下所示:

+----------------------+
| confidence Check |
+----------------------+
| 1 TRUE |
| 0.72 TRUE |
| 0.68 TRUE |
| 1 TRUE |
| 0.150287157 FALSE |
| 1 TRUE |
| 0.7 TRUE |
| 0.68 TRUE |
| 1 TRUE |
| 0.903333333 FALSE |
+----------------------+

我想知道数据集中最接近 95% 准确率的最小置信水平是多少。例如,如果我删除足够多具有 FALSE 值的行,从具有最低置信水平且为 FALSE 的行开始,那么数据集中的最小置信水平是多少到达了?

我将准确度计算为 TRUE 的行数除以数​​据帧中的总行数。

我该怎么做?

最佳答案

解决方案如下:

  1. 读取(下面的代码)中的数据帧,将 Check 列视为 int(而不是 bool 值),并按置信度递增的顺序排序。
  2. 现在,当您在行上扫描置信度阈值时,查看这些值:[ round(df.iloc[n:].Check.mean(), 3) for n in range(len(df.index))] 给出 [0.8, 0.889, 0.875 , 0.857, 0.833, 0.8, 1.0, 1.0, 1.0, 1.0]
  3. 找到截止行的编号 n 后,df.iloc[n].confidence 会为您提供截止置信度值,其准确度 >= 0.95。因此,您可以将截止置信度阈值选择为 df.iloc[n-1].confidence ... df.iloc[n].confidence 之间的任意数字

代码:

import pandas as pd

dat = """confidence Check
1 TRUE
0.72 TRUE
0.68 TRUE
1 TRUE
0.150287157 FALSE
1 TRUE
0.7 TRUE
0.68 TRUE
1 TRUE
0.903333333 FALSE"""

df = pd.read_csv(pd.compat.StringIO(dat), header=0, delim_whitespace=True, dtype={'confidence':'float', 'Check':'int'})
df.sort_values(by='confidence', inplace=True)

df

confidence Check
4 0.150287 0
2 0.680000 1
7 0.680000 1
6 0.700000 1
1 0.720000 1
9 0.903333 0
0 1.000000 1
3 1.000000 1
5 1.000000 1
8 1.000000 1

# Sweep over the df, finding the cutoff row which gives us 0.95 confidence...
for n in range(len(df.index)):
if df.iloc[n:].Check.mean() >= 0.95:
break

# ...then find the range for the cutoff confidence level
print("Cutoff confidence level is between:", df.iloc[n-1].confidence, df.iloc[n].confidence)

# Cutoff confidence level is between: 0.903333333 1.0

关于python - 删除行直到达到度量点并提取最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58698611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com