You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure ML混淆矩阵Raw配置显示浮点数而非整数的问题求助

解决Azure ML中混淆矩阵Raw模式显示浮点数的问题

我使用Azure Machine Learning Service进行二分类建模,调用azureml.training.tabular.score.scoring库的score_classification方法跟踪分析模型结果,代码如下:

metrics = score_classification(
        y_test, y_pred_probs, metrics_names_list, class_labels, train_labels, sample_weight=sample_weights, use_binary=True)

参数说明:

  • y_test:由0和1组成的数组
  • y_pred_probs:每个元素为浮点值的数组
  • metrics_names_list:需计算的指标列表:['f1_score_classwise', 'confusion_matrix']
  • class_labels:包含两个元素的数组[0, 1]
  • train_labels:包含两个元素的列表['False', 'True']

问题:在Azure ML门户的指标页面中,混淆矩阵切换到Raw模式时,单元格显示的是浮点数而非预期的整数样本数量。

解决方案

1. 移除样本加权参数(无需加权场景)

如果建模场景不需要对样本进行加权,直接删除sample_weight=sample_weights参数即可。此时score_classification会直接统计样本数量,混淆矩阵Raw模式下将显示整数。

修改后的代码:

metrics = score_classification(
        y_test, y_pred_probs, metrics_names_list, class_labels, train_labels, use_binary=True)

2. 手动转换混淆矩阵为整数(需加权场景)

如果必须使用样本加权,可以在计算完成后手动将混淆矩阵的浮点数转换为整数,再重新记录到Azure ML指标中:

# 计算原始指标
metrics = score_classification(
        y_test, y_pred_probs, metrics_names_list, class_labels, train_labels, sample_weight=sample_weights, use_binary=True)

# 将混淆矩阵转换为整数类型
confusion_matrix = metrics['confusion_matrix'].astype(int)

# 手动记录处理后的混淆矩阵
from azureml.core.run import Run
current_run = Run.get_context()
current_run.log_confusion_matrix(
    name='confusion_matrix',
    value=confusion_matrix,
    class_labels=train_labels
)

3. 验证输入数据的正确性

确认y_test是纯整数数组,y_pred_probs的处理逻辑正确,避免因输入数据类型问题导致计数异常。

内容的提问来源于stack exchange,提问作者elldora

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 19:18:35