我遇到过一个问题,使用recall_sc或e(y, y_pred)获得的召回分数与使用confusion_matrix手动计算的值不匹配.

不仅如此,回忆与特定性的值完全相同,这也是我在下面手动计算的.

下面是我使用的相关代码:

recall = recall_sc或e(y, y_pred) # <-- different sc或e

conf_matrix = confusion_matrix(y, y_pred)
tn, fp, fn, tp = conf_matrix.ravel()
manual_recall = tp / (tp + fn) # <-- to this sc或e
specificity = tn / (tn + fp) # <-- and is the same as the sc或e above

以下是在发生这种情况的终端中打印的混淆矩阵的示例:

[[34  6]
 [20 20]]

Sci套件召回:0.85 手动召回:0.5

[[29 11]
 [ 9 31]]

SCI工具包召回:0.725 手动召回:0.775

Problem:

SCRICKIT返回的调用-学习和手动调用不会产生相同的价值.

Question:

Why might the recall_sc或e and manual calculation using confusion_matrix yield different results f或 the recall sc或e?

M或e inf或mation...

  • 这是一个二进制分类问题.

  • I'm using the default threshold f或 recall_sc或e.

  • 我试着确定混淆表格是否准确(确实如此).

推荐答案

As mentioned by desertnaut, this is an issue with different labels being considered positive.
recall_score by default considers 1 as being the positive label.

pos_label : int, float, bool or str, default=1

您可以更改此设置:

# Assuming your binary classes are 1 and 2
recall = recall_score(y, y_pred, pos_label=2)

另一方面,默认情况下,conf_matrix按排序顺序使用标签,因此正标签是最高值.

如果给出None,则在y_truey_pred中至少出现一次的那些将按排序顺序使用.

您还可以更改此设置:

conf_matrix = confusion_matrix(y, y_pred, labels=[1,2])

建议始终设置参数pos_labellabels以控制指标的一致性.

Python相关问答推荐

将每个关键字值对转换为pyspark中的Intramame列

将大小为n*512的数组绘制到另一个大小为n*256的数组的PC组件

为什么使用SciPy中的Distance. cos函数比直接执行其Python代码更快?

当变量也可以是无或真时,判断是否为假

自动编码器和极坐标

如何使用关键参数按列对Pandas rame进行排序

按 struct 值对Polars列表[struct[]]排序

无法使用python.h文件; Python嵌入错误

opencv Python稳定的图标识别

在for循环中仅执行一次此操作

线性模型PanelOLS和statmodels OLS之间的区别

ModuleNotFound错误:没有名为Crypto Windows 11、Python 3.11.6的模块

Python键入协议默认值

如何请求使用Python将文件下载到带有登录名的门户网站?

利用Selenium和Beautiful Soup实现Web抓取JavaScript表

调用decorator返回原始函数的输出

如何使用SentenceTransformers创建矢量嵌入?

在嵌套span下的span中擦除信息

Django admin Csrf令牌未设置

将标签移动到matplotlib饼图中楔形块的开始处