【发布时间】:2011-05-15 04:49:57
【问题描述】:
您好,我有大量数据: http://paste-it.net/public/y17027d/ 它是 67859 行 x 10 列。第 6 列包含以 0.01 为增量表示从 1 到 6 的 Z 分数的值。我想做的是将具有相同 Z 分数值的所有其他列值合计,但我当前的代码不起作用。
我现在打印出的值,但每个 Z 分数的总数不正确。
这是我的代码:
#! /usr/bin/perl
use strict;
use warnings;
use POSIX;
use Data::Dumper;
my $input = $ARGV[0];
open (DATAFILE, $input) or die $!;
open(OUT,">>"."final.output.txt");
my($line,$fMeasure,$filename,$recall,$precision,$z_score,$computer_calls,$johns_calls,$false_negatives,$false_positives,$true_positives,$count);
$fMeasure=$filename=$recall=$precision=$z_score=$computer_calls=$johns_calls=$false_negatives=$false_positives=$true_positives=$count = 0;
my %stats=();
my %zscore=();
while($line = <DATAFILE>){
# Chop off new line character, skip the comments and empty lines.
chomp($line);
my @temp = split(/\t/, $line);
$true_positives = $temp[0];
$false_positives = $temp[1];
$false_negatives = $temp[2];
$johns_calls = $temp[3];
$computer_calls = $temp[4];
$z_score = $temp[5];
$fMeasure = $temp[6];
$precision = $temp[7];
$recall = $temp[8];
$filename = $temp[9];
$stats{$z_score}{$filename}[0] = $true_positives;
$stats{$z_score}{$filename}[1] = $false_positives;
$stats{$z_score}{$filename}[2] = $johns_calls;
$stats{$z_score}{$filename}[3] = $computer_calls;
$stats{$z_score}{$filename}[4] = $fMeasure;
$stats{$z_score}{$filename}[5] = $precision;
$stats{$z_score}{$filename}[6] = $recall;
$stats{$z_score}{$filename}[6] = $filename;
$zscore{$z_score}++;
}
my $false_positives_new = 0;
my $true_positives_new = 0;
my $johns_calls_new = 0;
my $computer_calls_new = 0;
my $file_name = 0;
foreach $z_score ( sort keys %stats ) {
foreach $filename( keys %{$stats{$z_score}} ){
my $tp = $stats{$z_score}{$filename}[0];
my $fp = $stats{$z_score}{$filename}[1];
my $jc = $stats{$z_score}{$filename}[2];
my $cc = $stats{$z_score}{$filename}[3];
my $fn = $stats{$z_score}{$filename}[6];
#print "$z_score\t$jc\n";
$false_positives_new = $false_positives + $fp;
$true_positives_new = $true_positives + $tp;
$johns_calls_new = $johns_calls + $jc;
$computer_calls_new = $computer_calls + $cc;
#print OUT "$fn\n";
}
print OUT"$true_positives_new\t$false_positives_new\t$johns_calls_new\t$computer_calls_new\t$z_score \n";
$false_positives_new = 0;
$true_positives_new = 0;
$johns_calls_new = 0;
$computer_calls_new = 0;
$file_name = 0;
}
close(OUT);
close (DATAFILE);
我知道我一定做错了什么,但我不知道是什么。任何帮助将不胜感激。谢谢
【问题讨论】:
-
Math::Complex在里面做什么? -
现在什么都没有,但我很可能稍后会更改程序并需要它。但它不应该影响代码的功能。
-
我很难理解复杂的数字会如何进入数据列表,但无论如何。
-
开头那段巨大的变量声明/初始化行非常丑陋,完全没有必要。
-
@Alos:我会将所有这些值存储在一个哈希表中,而不是存储在单独的标量变量中,并从函数中返回一个哈希作为所有统计分析的结果,例如
my %data; foreach .... { @data{qw(tp fp jb cc fn)} = @{$stats{$z_score}{$filename}}[0,1,2,3,6]; $data{false_positives} += $fp; ... }