每个 bin 中 "label fraction" 数据的二维直方图颜色
2D histogram colour by "label fraction" of data in each bin
从此处找到的 post 开始:
我想根据 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 并能够 return 每个 bin 中的原始数据值作为多维数组将有助于快速显式计算分数。
数组中元素低于阈值的分数可以计算为
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))
从此处找到的 post 开始:
我想根据 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 并能够 return 每个 bin 中的原始数据值作为多维数组将有助于快速显式计算分数。
数组中元素低于阈值的分数可以计算为
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))