如何使用 numpy 生成完整的直方图?

发布于 2024-08-04 11:30:32 字数 247 浏览 2 评论 0原文

我在 numpy.array 中有一个很长的列表。我想为其生成一个直方图。但是,Numpy 的 内置直方图 需要预先定义的 bin 数量。生成每个值一个箱的完整直方图的最佳方法是什么?

I have a very long list in a numpy.array. I want to generate a histogram for it. However, Numpy's built in histogram requires a pre-defined number of bins. What's the best way to generate a full histogram with one bin for each value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韬韬不绝 2024-08-11 11:30:32

如果您有一个整数数组并且最大值不太大,您可以使用 numpy.bincount:

hist = dict((key,val) for key, val in enumerate(numpy.bincount(data)) if val)

编辑:
如果您有浮点数据或分布在很大范围内的数据,您可以通过执行以下操作将其转换为整数:

bins = numpy.unique(data)
bincounts = numpy.bincount(numpy.digitize(data, bins) - 1)
hist = dict(zip(bins, bincounts))

If you have an array of integers and the max value isn't too large you can use numpy.bincount:

hist = dict((key,val) for key, val in enumerate(numpy.bincount(data)) if val)

Edit:
If you have float data, or data spread over a huge range you can convert it to integers by doing:

bins = numpy.unique(data)
bincounts = numpy.bincount(numpy.digitize(data, bins) - 1)
hist = dict(zip(bins, bincounts))
舂唻埖巳落 2024-08-11 11:30:32

每个值都有一个容器听起来有点奇怪,但不会

bins=a.max()-a.min()

给出类似的结果?

A bin for every value sounds a bit strange but wouldn't

bins=a.max()-a.min()

give a similar result?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文