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

如何用Pandas/Plotly绘制自定义分箱归一化类直方图

需求:按任务分箱归一化的算法成功率直方图

问题背景

我正在测试多种算法解决不同任务的效果。任务具备固有特征,可自然划分为不同类别,且各类别任务数量不均。算法在任务上有不同表现,未完成的任务不会被记录。我希望绘制类直方图展示各算法在每个任务类别中的成功率,且每个分箱数值需按该分箱的总任务数归一化。

数据结构

任务数据集(DataFrame)

Index ['task']: 360 entries, t0001 to t0360
Data columns (total 7 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   V       360 non-null    float64
 1   nk      360 non-null    int64  
 2   nt      360 non-null    int64  
 3   nm      360 non-null    int64  
 4   Vtot    360 non-null    float64
...
 dtypes: float64(4), int64(3)

结果数据集(DataFrame)

MultiIndex ['task', 'algo']: 862 entries, ('t0001', 'H0') to ('t0080', 'TDC-87-06-03')
Data columns (total 19 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   time    862 non-null    float64
 1   qmem    862 non-null    int64
...
dtypes: float64(17), int64(2)

逻辑实现思路(SQL版本)

WITH Bins(bin, floor, ceil, total) AS (
  SELECT
     "something" AS bin,
     foo - 0.5 AS floor,
     foo + 0.5 AS ceil,
     count(*) AS total 
  FROM Tasks
  GROUP BY bin
)
SELECT Results.algo, Bins.bin, count(*) / Bins.total as rate
FROM Results, Tasks, Bins 
WHERE Results.task = Tasks.task AND Tasks.foo BETWEEN Bins.floor AND Bins.ceil
GROUP BY algo, Bins.bin

绘图说明

  • 得到上述计算结果后,可通过px.bars(df, color='algo', x='bin', y='rate')绘制目标直方图。
  • 直接使用px.histogram(df, color='algo', x='foo', nbins=whatever)绘制合并后的数据集,得到的是绝对计数,无法满足按分箱总任务数计算相对占比(即count(*) / Bins.total)的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 02:49:52