对直方图的数字进行分组

发布于 2024-07-16 12:45:35 字数 336 浏览 8 评论 0原文

我有一堆数字想用来生成标准分数的直方图。

因此,我计算数字的平均值和标准差,并使用此公式

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 技术交流群。

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

发布评论

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

评论(2

无声无音无过去 2024-07-23 12:45:35

这是一个不使用任何第三方库的解决方案。 这些数字应该位于数组 vals 中。

MULTIPLIER  = 0.25 
multipliers = []
0.step(1, MULTIPLIER) { |n| multipliers << n }

histogram = Hash.new 0

# find the appropriate "bin" and create the histogram
vals.each do |val|
  # create an array with all the residuals and select the smallest
  cmp = multipliers.map { |group| [group, (group - val%1).abs] }
  bin = cmp.min { |a, b| a.last <=> b.last }.first
  histogram[val.truncate + bin] += 1
end

我认为它执行了适当的舍入。 但我只尝试过:

vals = Array.new(10000) { (rand * 10) % 4 * (rand(2) == 0 ? 1 : -1) }

并且分布有点倾斜,但这可能是随机数生成器的错误。

Here's a solution that doesn't use any third part libraries. The numbers should be in the Array vals.

MULTIPLIER  = 0.25 
multipliers = []
0.step(1, MULTIPLIER) { |n| multipliers << n }

histogram = Hash.new 0

# find the appropriate "bin" and create the histogram
vals.each do |val|
  # create an array with all the residuals and select the smallest
  cmp = multipliers.map { |group| [group, (group - val%1).abs] }
  bin = cmp.min { |a, b| a.last <=> b.last }.first
  histogram[val.truncate + bin] += 1
end

I think that it performs the proper rounding. But I only tried it with:

vals = Array.new(10000) { (rand * 10) % 4 * (rand(2) == 0 ? 1 : -1) }

and the distribution got kind of skewed, but that's probably the random number generator's fault.

一曲爱恨情仇 2024-07-23 12:45:35

Rails 提供了 Enumerable#group_by —— 请参阅此处的源代码,假设您没有使用 Rails: http:// /api.rubyonrails.org/classes/Enumerable.html

假设您的列表名为 xs,您可以执行如下操作(未经测试):

bars = xs.group_by {|x| #determine bin here}

然后您将得到一个如下所示的哈希值:

bars = { 0 => [elements,in,first,bin], 1 => [elements,in,second,bin], etc }

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):

bars = xs.group_by {|x| #determine bin here}

Then you'll have a hash that looks like:

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