对直方图的数字进行分组
我有一堆数字想用来生成标准分数的直方图。
因此,我计算数字的平均值和标准差,并使用此公式
x' = (x-mean)/std_dev 对
每个 x 进行归一化。结果是 -4 到 4 之间的数字。我想绘制该结果的图表。 我正在寻找一种对数字进行分组的方法,以避免出现小条。
我的计划是在区间 [-4,4] 中以连续四分之一单位为中心,即 [-4,-3.75,...,3.75,4]
示例: 0.1 => bin "0.0", 0.3 => 仓“0.25”,-1.3 => Bin“-1.5”
实现这一目标的最佳方法是什么?
I have a bunch of numbers I want to use to generate a histogram for a standard score.
Therefore I compute the mean and the standard deviation of the numbers and normalize each x with this formula
x' = (x-mean)/std_dev
The result is a number between -4 and 4. I want to chart that result. I am looking for a way to group the numbers in order to avoid to small bars.
My plan is to have bins in the interval [-4,4] centered at consecutavice quarter units, i.e [-4,-3.75,...,3.75,4]
Example: 0.1 => bin "0.0", 0.3 => bin "0.25", -1.3 => Bin "-1.5"
What is the best way to achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个不使用任何第三方库的解决方案。 这些数字应该位于数组
vals
中。我认为它执行了适当的舍入。 但我只尝试过:
并且分布有点倾斜,但这可能是随机数生成器的错误。
Here's a solution that doesn't use any third part libraries. The numbers should be in the Array
vals
.I think that it performs the proper rounding. But I only tried it with:
and the distribution got kind of skewed, but that's probably the random number generator's fault.
Rails 提供了 Enumerable#group_by —— 请参阅此处的源代码,假设您没有使用 Rails: http:// /api.rubyonrails.org/classes/Enumerable.html
假设您的列表名为 xs,您可以执行如下操作(未经测试):
然后您将得到一个如下所示的哈希值:
Rails provides Enumerable#group_by -- see source here, assuming you're not using Rails: http://api.rubyonrails.org/classes/Enumerable.html
Assuming your list is called xs, you could do something like the following (untested):
Then you'll have a hash that looks like: