【问题标题】:Need help collapsing a list and obtaining totals in perl需要帮助折叠列表并在 perl 中获取总数
【发布时间】: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; ... }

标签: arrays perl file hash


【解决方案1】:

好的。我能够从 pastebin 中获取数据,我认为以下代码可以满足您的需求。

#! /usr/bin/perl

use strict; use warnings;
use Data::Dumper;

my ($input) = @ARGV;
open my $DATAFILE, '<', $input
    or die "Cannot open '$input': $!";

my @field_names = qw(
    fMeasure
    recall
    precision
    z_score
    computer_calls
    johns_calls
    false_negatives
    false_positives
    true_positives
    count
);

my @track_fields = qw(
    false_positives
    false_negatives
    johns_calls
    computer_calls
);

my (%stats, %by_zscore);

while ( my $line = <$DATAFILE> ) {
    last unless $line =~ /\S/;
    chomp $line;
    my @temp = split /\t/, $line;
    my $filename = pop @temp;

    my %fields;
    @fields{ @field_names } = @temp;

    my $z_score = $fields{z_score};

    $stats{ $z_score }{$filename} = \@temp;

    for my $f ( @track_fields ) {
        $by_zscore{$z_score}{ $f } += $fields{ $f };
    }
}

print Dumper \%by_zscore;

【讨论】:

    【解决方案2】:

    我想你想说

    $false_positives_new = $false_positives_new + $fp;
    # etc.
    

    而不是

    $false_positives_new = $false_positives + $fp;
    

    【讨论】:

    • @Alos:我希望这并不意味着您将继续使用这些变量而不是哈希。
    • @Sinan 不,我已经修改它以使用哈希。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 2023-01-26
    • 1970-01-01
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多