【问题标题】:2D histogram colour by "label fraction" of data in each bin每个 bin 中数据的“标签分数”的 2D 直方图颜色
【发布时间】:2019-06-04 02:38:57
【问题描述】:

从这里找到的帖子继续:2D histogram coloured by standard deviation in each bin

我想通过标签值低于 Python 中某个阈值的点的分数来为 2D 网格中的每个 bin 着色。

请注意,在这个数据集中,每个点都有一个介于 0-1 之间的连续标签值。

例如,这是我制作的直方图,其中颜色表示每个 bin 中所有点的标签值的标准偏差:

这样做的方法是使用

scipy.stats.binned_statistic_2d()

(见:https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.binned_statistic_2d.html

..并将统计参数设置为“std”

但是有没有办法改变这种图,以便着色代表每个 bin 中标签值低于 0.5 的点的分数?

这样做的唯一方法可能是明确定义某种网格并计算分数,但我不确定最好的方法,因此非常感谢您对此事的任何帮助!

也许使用 scipy.stats.binned_statistic_2d 或 numpy.histogram2d 并能够将每个 bin 中的原始数据值作为多维数组返回,这将有助于能够显式地快速计算分数。

【问题讨论】:

    标签: python matplotlib scipy histogram binning


    【解决方案1】:

    数组中低于阈值的元素比例可以计算为

    fraction = lambda a, threshold: len(a[a<threshold])/len(a)
    

    因此你可以调用

    scipy.stats.binned_statistic_2d(x, y, values, statistic=lambda a: fraction(a, 0.5)) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-26
      • 1970-01-01
      • 2015-07-15
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多