Han*_*Han 9 memory statistics perl
我正在寻找Perl的统计软件包(CPAN很好),它允许我以递增方式添加数据,而不必传递整个数据数组.
只需要均值,中位数,stddev,max和min是必要的,没有什么太复杂的.
原因是因为我的数据集太大而无法放入内存中.数据源位于MySQL数据库中,所以现在我只是查询数据的子集并计算它们的统计数据,然后再组合所有可管理的子集.
如果您对如何克服这个问题有其他想法,我会非常感激!
你不能做一个确切的stddev和一个中位数,除非你把整个东西保存在内存中或者两次运行数据.
更新虽然你不能做一个精确的stddev IN ONE PASS,但是有一个近似的一次通过算法,链接是对这个答案的评论.
其余的完全是微不足道的(不需要模块)在3-5行Perl中完成.STDDEV/Median也可以通过2次传递完成(我刚刚推出了一个完全按照你描述的方式编写的脚本,但出于IP原因,我很确定我不允许将它作为例子发给你,抱歉)
示例代码:
my ($min, $max)
my $sum = 0;
my $count = 0;
while (<>) {
chomp;
my $current_value = $_; #assume input is 1 value/line for simplicity sake
$sum += $current_value;
$count++;
$min = $current_value if (!defined $min || $min > $current_value);
$max = $current_value if (!defined $max || $max < $current_value);
}
my $mean = $sum * 1.0 / $count;
my $sum_mean_diffs_2 = 0;
while (<>) { # Second pass to compute stddev (use for median too)
chomp;
my $current_value = $_;
$sum_mean_diffs += ($current_value - $mean) * ($current_value - $mean);
}
my $std_dev = sqrt($sum_mean_diffs / $count);
# Median is left as excercise for the reader.
Run Code Online (Sandbox Code Playgroud)
统计::描述性::离散允许您以类似于统计::描述性的方式执行此操作,但已针对大型数据集的使用进行了优化。(例如,文档报告内存使用量提高了两个数量级 (100 倍))。
| 归档时间: |
|
| 查看次数: |
2378 次 |
| 最近记录: |