ZaynerTech

Decision Tree Machine Learning

Apr 4th, 2013
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 18.16 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use Getopt::Std;
  4. #use warnings;
  5.  
  6. sub usage
  7. {
  8.  
  9.   print "\nUsage: $0 -fpbn\n-f <filename of dataset>\n-p <binary prediction column(s) ex. 1 or 2,3 &c.>\n-n <column number with names>\n-b <number of bags default 10>\n -t <filename of data to predict outcomes> \n\n";
  10.  die;
  11. }
  12. sub init
  13. {
  14.   getopts('f:p:n:b:t:', \%opts) or usage();
  15.   if(!$opts{f}){ print "Need datafile!\n"; usage (); }
  16.  
  17.   if(!$opts{p}){ print "Need to specify what we are trying to predict!\n"; usage(); }
  18.   else
  19.   {
  20.     @notdata = split(/,/, $opts{p});
  21.     @topredict1 = split(/,/, $opts{p});
  22.     foreach $tp (@topredict1){ push(@topredict, ($tp -1)); }
  23.   }
  24.  
  25.   if($opts{b}){ $baggins = $opts{b}; }
  26.   else { $baggins = 10; }
  27.  
  28.   if($opts{n}){ push(@notdata, $opts{n}); }
  29.  
  30. }
  31.  
  32. sub bagging
  33. {
  34.   ($numbags, @bagdata) = @_;  
  35.   my $x;
  36.  
  37.   #Generate $x bags
  38.   for($x = 0; $x< $numbags; $x++)
  39.   {
  40.     #Random data for each feature
  41.     for($y = 0; $y < $numfeats; $y++)
  42.     {
  43.       #Random data
  44.       for($z = 0; $z < ($#{$bagdata[$y]} + 1); $z++)
  45.       {
  46.         $randdata = int(rand($#{$bagdata[$y]} + 1));
  47.         $bags[$x][$y][$z] = $bagdata[$y][$randdata];
  48.       }
  49.     }
  50.   }
  51. }
  52.  
  53. sub parsefile
  54. {
  55.   my(@filer) = @_;
  56. my $w = 0;
  57. my $z = 0;
  58. my @datass = ();
  59. my $numfeats = 0;
  60. my $first = 0;
  61.  
  62. foreach my $line (@filer)
  63. {
  64.  
  65.   my @stuff = split(/\s+/,$line);
  66.   #make first line in file the feature names
  67.   if($first == 0)
  68.   {
  69.     foreach $feats (@stuff)
  70.     {
  71.       $datfeat[$numfeats] = $feats;
  72.        #print "$numfeats : $feats : $datfeat[$numfeats]\n";
  73.       $numfeats++;
  74.       $first++;
  75.     }
  76.   }
  77.   else
  78.   {
  79.     foreach my $dat (@stuff)
  80.     {
  81.         $datass[$w][$z] = $dat;
  82.         $w++;
  83.  
  84.     }
  85.     $w = 0;
  86.     $z++;
  87.   }
  88. #  print "DAT @{$datass[0]}\n";    
  89.     undef @stuff;
  90. }
  91. undef @filer;
  92. return @datass;
  93.  
  94. }
  95.  
  96.  
  97. sub main
  98. {
  99. my $z = 0;
  100. my $x = 0;
  101. my $y = 0;
  102. init();
  103.  
  104. open(FILE, "<", $opts{f}) or die;
  105. my @file = <FILE>;
  106. open(FILED, "<", $opts{t}) or die;
  107. my @predfile = <FILED>;
  108.  
  109. my @datas = parsefile(@file);
  110. close(FILE);
  111. @testdata = parsefile(@predfile);
  112. #my @testdata = @$tempp;
  113.  
  114. close(FILED);
  115.   for($x = 0; $x < ($#datfeat + 1); $x++)
  116.   {
  117.      foreach $notd (@notdata)
  118.      {
  119.        if($x == ($notd - 1)){ $nope = 1; }
  120.      }
  121.      if(!$nope){ $realdata[$y] = $x; $y++;}
  122.      $nope = 0;
  123.      
  124.   }
  125.  
  126.   foreach $predicting (@topredict)
  127.   {
  128.     for($y = 0; $y < $#{$datas[0]}; $y++)
  129.     {
  130.       $namelookup{$datfeat[$predicting]}{$testdata[0][$y]} = $testdata[$predicting][$y];
  131.     }
  132.  
  133.  
  134.   }
  135. #bagging($baggins, @datas);
  136. %empty = maketreesandstuff(@datas);
  137. predict(%empty);
  138.  
  139.  
  140. }
  141.  
  142. sub maketreesandstuff
  143. {
  144.  my $x;
  145.  my $y;
  146.  my %predsandprobs = ();
  147.  my %tablelookup = ();
  148.  my $sims = 100000;
  149.  (@isdata) = @_;
  150.  
  151. #count values
  152.  
  153. foreach $foreals (@realdata)
  154. {
  155.   foreach $datum (@{$isdata[$foreals]})
  156.   {
  157.     $countdatacula{$datfeat[$foreals]}{$datum} = $countdatacula{$datfeat[$foreals]}{$datum} + 1;
  158.   }
  159.  
  160. }
  161.  
  162.  
  163.  
  164. foreach $foreals (@realdata)
  165. {
  166.   foreach $predicting (@topredict)
  167.   {
  168.    
  169.     for($y = 0; $y < $sims; $y++)
  170.     {
  171.       $rando = int(rand($#{$isdata[$forreals]} + 1));
  172.       $yes = int(rand(2));
  173.      
  174.       if($isdata[$predicting][$rando] == $yes)
  175.       { $predsandprobs{$datfeat[$predicting]}{$datfeat[$foreals]}{$isdata[$foreals][$rando]}{guess}++; }
  176.        
  177.       if($isdata[$predicting][$rando] == 1)
  178.       {
  179.              
  180.        $predsandprobs{ $datfeat[$predicting] }{ $datfeat[$foreals] }{ $isdata[$foreals][$rando] }{truthone}++;
  181.        foreach $realz (@realdata)
  182.        {
  183.          if($realz != $foreals)
  184.          {
  185.            $predsandprobs{ $datfeat[$predicting] }{ $datfeat[$foreals] }{ $isdata[$foreals][$rando] }{mutual}{$datfeat[$realz]}{$isdata[$realz][$rando]}{truthone}++;
  186.          }
  187.        }
  188.       }
  189.       elsif($isdata[$predicting][$rando] == 0)
  190.       {
  191.          $predsandprobs{ $datfeat[$predicting] }{ $datfeat[$foreals] }{ $isdata[$foreals][$rando] }{truthzero}++;
  192.          foreach $realz (@realdata)
  193.          {
  194.            if($realz != $foreals)
  195.            {
  196.          
  197.              $predsandprobs{ $datfeat[$predicting] }{ $datfeat[$foreals] }{ $isdata[$foreals][$rando] }{mutual}{$datfeat[$realz]}{$isdata[$realz][$rando]}{truthzero}++;
  198.          #    print "D: $isdata[0][$rando] : $datfeat[$foreals] : $isdata[$foreals][$rando] : $datfeat[$realz] : $isdata[$realz][$rando] : $predsandprobs{ $datfeat[$predicting] }{ $datfeat[$foreals] }{ $isdata[$foreals][$rando] }{mutual}{$datfeat[$realz]}{$isdata[$realz][$rando]}{truthzero}\n";
  199.            }
  200.          }
  201.  
  202.       }
  203.     }
  204.   }
  205.  
  206. }
  207.     #calculate probabilities
  208.     #key1 = Predictor
  209.     #key2 = feature
  210.     #key3 = feature value
  211.     foreach $key1 (keys %predsandprobs)
  212.     {
  213.       foreach $key2 (keys %{$predsandprobs{$key1}})
  214.       {
  215.          foreach $key3 (keys %{$predsandprobs{$key1}{$key2}})
  216.          {
  217.            #for the binary predictor probabilities
  218.            if($predsandprobs{$key1}{$key2}{$key3}{truthzero})
  219.    { $truthierz = $predsandprobs{$key1}{$key2}{$key3}{truthzero} / ($predsandprobs{$key1}{$key2}{$key3}{truthzero} + $predsandprobs{$key1}{$key2}{$key3}{truthone}); }
  220.            else { $truthierz = 0.00000001; }
  221.  
  222.           if($predsandprobs{$key1}{$key2}{$key3}{truthone})    
  223.     { $truthiero = $predsandprobs{$key1}{$key2}{$key3}{truthone} / ($predsandprobs{$key1}{$key2}{$key3}{truthzero} + $predsandprobs{$key1}{$key2}{$key3}{truthone}); }
  224.            else { $truthiero = 0.00000001; }
  225.      
  226.            #probability of 0
  227.            $predsandprobs{$key1}{$key2}{$key3}{truthierz} = $truthierz;
  228.            $tablelookup{$key1}{$key2}{$key3}{truthierz} = $truthierz;
  229.            #probability of 1
  230.            $predsandprobs{$key1}{$key2}{$key3}{truthiero} = $truthiero;
  231.            $tablelookup{$key1}{$key2}{$key3}{truthiero} = $truthiero;
  232.  
  233.            #$predsandprobs{$key1}{$key2}{$key3}{prob} =  $predsandprobs{$key1}{$key2}{$key3}{truth} / $sims;
  234.            #print "$key1 : $key2 : $key3 : $predsandprobs{$key1}{$key2}{$key3}{truthz} $predsandprobs{$key1}{$key2}{$key3}{trutho} : $truthierz : $truthiero\n";
  235.            foreach $key4 (keys %{$predsandprobs{$key1}{$key2}{$key3}{mutual}})
  236.            {
  237.             foreach $key5 (keys %{$predsandprobs{$key1}{$key2}{$key3}{mutual}{$key4}})
  238.             {
  239.                if($predsandprobs{$key1}{$key2}{$key3}{mutual}{$key4}{$key5}{truthone})
  240.                {  
  241.                $truthiermo = $predsandprobs{$key1}{$key2}{$key3}{mutual}{$key4}{$key5}{truthone} / ($predsandprobs{$key1}{$key2}{$key3}{$key4}{mutual}{$key4}{$key5}{truthzero} + $predsandprobs{$key1}{$key2}{$key3}{mutual}{$key4}{$key5}{truthone});
  242.                }
  243.                else{ $truthiermo = 0.000001; }
  244.  
  245.               if($predsandprobs{$key1}{$key2}{$key3}{mutual}{$key4}{$key5}{truthzero})
  246.               {
  247.                $truthiermz = $predsandprobs{$key1}{$key2}{$key3}{mutual}{$key4}{$key5}{truthzero} / ($predsandprobs{$key1}{$key2}{$key3}{mutual}{$key4}{$key5}{truthzero} + $predsandprobs{$key1}{$key2}{$key3}{mutual}{$key4}{$key5}{truthone});
  248.                }
  249.                else { $truthiermz = 0.0000001; }
  250.         #   print "Truth: $key1 $key2 $key3 : $truthierz : $truthiero\n";
  251.        
  252.            $predsandprobs{$key1}{$key2}{$key3}{mutual}{$key4}{$key5}{truthierz} = $truthiermz;
  253.            $tablelookup{$key1}{$key2}{$key3}{mutual}{$key4}{$key5}{truthierz} = $truthiermz;
  254.           # probability of 1
  255.            $predsandprobs{$key1}{$key2}{$key3}{mutual}{$key4}{$key5}{truthiero} = $truthiermo;
  256.            $tablelookup{$key1}{$key2}{$key3}{mutual}{$key4}{$key5}{truthiero} = $truthiermo;
  257.        #  print "TruthM: $key1 $key2 $key3 $key4 $key5: 0 $predsandprobs{$key1}{$key2}{$key3}{mutual}{$key4}{$key5}{truthierz} : 1 $predsandprobs{$key1}{$key2}{$key3}{mutual}{$key4}{$key5}{truthiero}\n";
  258.  
  259.             } #foreach key5
  260.            }#foreach key4
  261.           print "Truth: $key1 $key2 $key3 : $truthierz : $truthiero :0 $predsandprobs{$key1}{$key2}{$key3}{truthzero} : 1 $predsandprobs{$key1}{$key2}{$key3}{truthone}\n";  
  262.          }#foreach key3
  263.  
  264.          #print "$key1 : $key2 : $featsprobs{$key1}{$key2}{tots}\n";
  265.          undef $truthierz; undef $truthiero; undef $truthiermo; undef $truthiermz;
  266.       }
  267.     }
  268.  
  269.   return %tablelookup;
  270. }
  271.  
  272. sub predict()
  273. {
  274.    my(%whatweneed) = @_;
  275.    my @doit = @testdata;
  276.    my %bestest = ();
  277.    my $x = 0;
  278.    my $y = 0;
  279.    my $onsie = 0;
  280.  
  281.   #
  282.   #testdata is global from test file
  283.   #realdata is the data with the non-feature data removed
  284.   #p1 is for probability of effect
  285.   #p0 is for probability of no effect
  286.   #
  287.   # for each feature value
  288.   for($y = 0; $y < ($#{$doit[0]} + 1); $y++)
  289.   {
  290.      #for each Predictor
  291.      foreach $key1 (keys %whatweneed) #loop Predictors
  292.      {
  293.        $bestest{$key1}{valone} = 0;
  294.        $bestest{$key1}{val0} = 0;
  295.        $onsie = 0;
  296.        
  297.       for($x = $realdata[0]; $x < $realdata[$#realdata]; $x++)
  298.       {
  299.  
  300.        if($key1)
  301.        {
  302.          #print "DHDH $x : $datfeat[$x]\n";
  303.          #print "See one: $whatweneed{$key1}{$doit[$x]}{$doit[$x][$y]}{truthiero} : $bestest{$key1}{valone}\n";
  304.          # whatweneed{predictor}{feature}{feature value} = Rank
  305.  
  306.          $randtest = int(rand(($realdata[$#realdata] - $realdata[0]) + 1) + $realdata[0]);
  307.          #print "$randtest\n";
  308.            $bestest{$key1}{$onsie}{valone} = $whatweneed{$key1}{$datfeat[$x]}{$doit[$x][$y]}{truthiero};
  309.            $bestest{$key1}{$onsie}{namevalone} = $doit[$x][$y];
  310.            $bestest{$key1}{$onsie}{nameone} = $datfeat[$x];
  311.            $bestest{$key1}{$onsie}{randvalone} = $whatweneed{$key1}{$datfeat[$randtest]}{$doit[$randtest][$y]}{truthiero};
  312.            $bestest{$key1}{$onsie}{randnamevalone} = $doit[$randtest][$y];
  313.            $bestest{$key1}{$onsie}{randnameone} = $datfeat[$randtest];
  314.    
  315.  
  316.          $randtest = int(rand(($realdata[$#realdata] - $realdata[0]) + 1) + $realdata[0]);
  317.          #print "See zero: $whatweneed{$key1}{$doit[$x]}{$doit[$x][$y]}{truthierz} : $bestest{$key1}{val0}\n";
  318.            $bestest{$key1}{$onsie}{val0} = $whatweneed{$key1}{$datfeat[$x]}{$doit[$x][$y]}{truthierz};
  319.            $bestest{$key1}{$onsie}{nameval0} = $doit[$x][$y];
  320.            $bestest{$key1}{$onsie}{name0} = $datfeat[$x];
  321.            $bestest{$key1}{$onsie}{randval0} = $whatweneed{$key1}{$datfeat[$randtest]}{$doit[$randtest][$y]}{truthierz};
  322.            $bestest{$key1}{$onsie}{randnameval0} = $doit[$randtest][$y];
  323.            $bestest{$key1}{$onsie}{randname0} = $datfeat[$randtest];
  324.            $onsie++;
  325.         }  
  326.          
  327.        }#forx
  328.         #compare probabilities
  329.          #bestest{val0} = rank
  330.          #key1 = UV, CD
  331.          #bestest{$key1}{namevalX} & doit[x][y] = value of feature
  332.          #bestest{name} & datfeat[x] =   name of feature
  333.          #whatweneed{predictor}{Feature}{p1}{rank}
  334.          undef @temp; $j = 0;
  335.  
  336.          #
  337.          #sort and rearrange probabilities by value
  338.          #
  339.          foreach $keysort (sort {$bestest{$key1}{$b}{valone} <=> $bestest{$key1}{$a}{valone}} keys %{$bestest{$key1}})
  340.          {
  341.            if($bestest{$key1}{$keysort}{valone})
  342.            {
  343.              $temp[$j] = $keysort;
  344.              $tempr[$keysort] = $j;
  345.  
  346.            }
  347.            else { $tempr[$j] = $j; $temp[$j] = $j; }
  348.            
  349.           #   print "KEY: tr:  $j : $keysort : $bestest{$key1}{$keysort}{valone} \n";
  350.            $j++;
  351.          }
  352.  
  353.          for($q = 0; $q < $onsie; $q++)
  354.          {
  355.           # print "TEMP: $q : $temp[$q] : $bestest{$key1}{$q}{valone} : $bestest{$key1}{$temp[$q]}{valone}\n";
  356.            $tempval = $bestest{$key1}{$temp[$q]}{valone};
  357.            $tempnameval = $bestest{$key1}{$temp[$q]}{namevalone};
  358.            $tempname = $bestest{$key1}{$temp[$q]}{nameone};
  359.  
  360.            $bestest{$key1}{$temp[$q]}{valone} = $bestest{$key1}{$q}{valone};
  361.            $bestest{$key1}{$temp[$q]}{namevalone} = $bestest{$key1}{$q}{namevalone};
  362.            $bestest{$key1}{$temp[$q]}{nameone} = $bestest{$key1}{$q}{nameone};
  363.  
  364.            $bestest{$key1}{$q}{valone} = $tempval;
  365.            $bestest{$key1}{$q}{namevalone} = $tempnameval;
  366.            $bestest{$key1}{$q}{nameone} = $tempname;
  367.              
  368.            $tpt = $temp[$q];
  369.            $tpz = $tempr[$q];
  370.            $temp[$q] = $q;
  371.            $temp[$tpz] = $tt;
  372.            $tempr[$q] = $q;
  373.            $tempr[$tpt] = $tz;
  374.  
  375.          #  print "QQQ1: $q : $bestest{$key1}{$q}{valone}\n";
  376.          }
  377.        undef @temp; undef @tempr; $j = 0;  
  378.         foreach $keysorts (sort {$bestest{$key1}{$b}{val0} <=> $bestest{$key1}{$a}{val0}} keys %{$bestest{$key1}})
  379.          {
  380.            if($bestest{$key1}{$keysorts}{val0})
  381.            {
  382.              $tempz[$j] = $keysorts;
  383.              $temprz[$keysorts] = $j;
  384.  
  385.            }
  386.            else { $temprz[$j] = $j; $tempz[$j] = $j; }
  387.  
  388.        #      print "KEY: $j : $keysorts : $bestest{$key1}{$keysorts}{val0} \n";
  389.            $j++;
  390.          }
  391.  
  392.          for($q = 0; $q < $onsie; $q++)
  393.          {
  394.           # print "TEMP: $q : $temp[$q] : $bestest{$key1}{$q}{valone} : $bestest{$key1}{$temp[$q]}{valone}\n";
  395.            $tempval = $bestest{$key1}{$tempz[$q]}{val0};
  396.            $tempnameval = $bestest{$key1}{$tempz[$q]}{nameval0};
  397.            $tempname = $bestest{$key1}{$tempz[$q]}{name0};
  398.  
  399.            $bestest{$key1}{$tempz[$q]}{val0} = $bestest{$key1}{$q}{val0};
  400.            $bestest{$key1}{$tempz[$q]}{nameval0} = $bestest{$key1}{$q}{nameval0};
  401.            $bestest{$key1}{$tempz[$q]}{name0} = $bestest{$key1}{$q}{name0};
  402.  
  403.            $bestest{$key1}{$q}{val0} = $tempval;
  404.            $bestest{$key1}{$q}{nameval0} = $tempnameval;
  405.            $bestest{$key1}{$q}{name0} = $tempname;
  406. #           print "iTEMP: $tempz[$q] $temprz[$tempz[$q]] $tempz[$temprz[$q]] $q\n";
  407.            $tt = $tempz[$q];
  408.            $tz = $temprz[$q];
  409.            $tempz[$q] = $q;
  410.            $tempz[$tz] = $tt;
  411.            $temprz[$q] = $q;
  412.            $temprz[$tt] = $tz;
  413.            
  414.        #    $tt = $temprz[$tempz[$q]];
  415.  
  416.  #          print "iTEMP: $tempz[$q] $temprz[$tempz[$q]] $tempz[$temprz[$q]] $q\n";
  417.   #         print "QQQ2: $doit[0][$y] : $q : $bestest{$key1}{$q}{val0} \n";
  418.          }
  419.  
  420.          undef @temp; undef @tempr;
  421.        } #foreach $key1
  422.  
  423.        foreach $key1 (keys %bestest)
  424.        {
  425.          # whatweneed{predictor}{feature}
  426.          $probone = $bestest{$key1}{0}{valone};
  427.          $prob0 = $bestest{$key1}{0}{val0};
  428.          $randprobone = $bestest{$key1}{0}{randvalone};
  429.          $randprob0 = $bestest{$key1}{0}{randval0};
  430.          $matchestotal{$key1}++;
  431.          
  432.      #print "1: $probone $bestest{$key1}{1}{valone} $bestest{$key1}{2}{valone} 2: $prob0 $bestest{$key1}{1}{val0} $bestest{$key1}{2}{val0}\n";
  433.          #print "HELP!  $key1 : k $bestest{$key1}{val1} k: $whatweneed{$key1}{$bestest{$key1}{name1}}{p1}{$bestest{$key1}{val1}}{prob} \n";
  434.          if($probone >= $prob0)
  435.          {
  436.           #  print "1\n";
  437.             if($bestest{$key1}{1}{valone} >= $bestest{$key1}{0}{val0})
  438.             {
  439.               if($namelookup{$key1}{$doit[0][$y]} == 1){ $matches{$key1}++; $matchesnum{$key1}{one}++;}
  440. #print "prediction11 : $key1 : $doit[0][$y] : Name $namelookup{$key1}{$doit[0][$y]} : Feat $bestest{$key1}{nameone} : Value $bestest{$key1}{namevalone} : Prob $bestest{$key1}{valone} : $prob0\n";
  441.            }
  442.             elsif($bestest{$key1}{2}{valone} >= $bestest{$key1}{2}{val0})
  443.             {
  444.                if($namelookup{$key1}{$doit[0][$y]} == 1){ $matches{$key1}++; $matchesnum{$key1}{one}++;}
  445.      #print "prediction12 : $key1 : $doit[0][$y] : Feat $bestest{$key1}{nameone} : Value $bestest{$key1}{namevalone} : Prob $bestest{$key1}{valone} : $prob0\n";
  446.  
  447.             }
  448.             else{  
  449.                if($namelookup{$key1}{$doit[0][$y]} == 0){ $matches{$key1}++; $matchesnum{$key1}{zero}++;}
  450.       # print "prediction1_0 : $key1 : $doit[0][$y] : Feat $bestest{$key1}{name0} : Value $bestest{$key1}{nameval0} : Prob $bestest{$key1}{val0} : $probone\n";
  451.                 }
  452.  
  453.          }
  454.          elsif($prob0 > $probone)
  455.          {
  456.            if($bestest{$key1}{1}{val0} >= $bestest{$key1}{0}{valone})
  457.            {  
  458.              if($namelookup{$key1}{$doit[0][$y]} == 0){ $matches{$key1}++; $matchesnum{$key1}{zero}++; }
  459. #           print "prediction01 : $key1 : $doit[0][$y] : Feat $bestest{$key1}{name0} : Value $bestest{$key1}{nameval0} : Prob $bestest{$key1}{1}{val0} : $probone\n";
  460.            }
  461.            elsif($bestest{$key1}{2}{val0} >= $bestest{$key1}{2}{valone})
  462.            {
  463.                if($namelookup{$key1}{$doit[0][$y]} == 0){ $matches{$key1}++; $matchesnum{$key1}{zero}++; }
  464. #     print "prediction02 : $key1 : $doit[0][$y] : Feat $bestest{$key1}{name0} : Value $bestest{$key1}{nameval0} : Prob $bestest{$key1}{2}{val0} : $probone\n";
  465.  
  466.            }
  467.            else {  
  468.            if($namelookup{$key1}{$doit[0][$y]} == 1){ $matches{$key1}++; $matchesnum{$key1}{one}++;  }
  469.  
  470. #print "prediction0_1 : $key1 : $doit[0][$y] : Feat $bestest{$key1}{nameone} : Value $bestest{$key1}{namevalone} : Prob $bestest{$key1}{2}{valone} : $prob0\n";
  471.             }
  472.  
  473.          }
  474.         #  if($randprobone >= $randprob0)
  475.         # { print "Rand prediction1 : $key1 : $doit[0][$y] : Feat $bestest{$key1}{randnameone} : Value $bestest{$key1}{randnamevalone} : Prob $bestest{$key1}{randvalone} : $randprob0\n";}
  476.         # elsif($randprob0 > $randprobone)
  477.         # {print "Rand prediction0 : $key1 : $doit[0][$y] : Feat $bestest{$key1}{randname0} : Value $bestest{$key1}{randnameval0} : Prob $bestest{$key1}{randval0} : $randprobone\n";}
  478.  
  479.        if($namelookup{$key1}{$doit[0][$y]} == 1){ $matchesnum{$key1}{onetotal}++; }
  480.        elsif($namelookup{$key1}{$doit[0][$y]} == 0){ $matchesnum{$key1}{zerototal}++; }
  481.        } #foreach bestest
  482.      undef %bestest;
  483.  } #for
  484.  
  485.  foreach $key5 (keys %matches)
  486.  {
  487.     if(!$matchesnum{$key5}{zero}){ $matchesnum{$key5}{zero} = 0; }
  488.     if(!$matchesnum{$key5}{one}){ $matchesnum{$key5}{one} = 0; }
  489.     $valzero = $matchesnum{$key5}{zero} / $matchesnum{$key5}{zerototal};
  490.     $valone = $matchesnum{$key5}{one} / $matchesnum{$key5}{onetotal};
  491.     $valtotal = ($matches{$key5}) / $matchestotal{$key5};
  492.     #print "$matches{$key5} : $matchestotal{$key5} \n";
  493.     print "---$key5--- \nOne: $valone\nZero: $valzero\nTotal: $valtotal\n";
  494.  
  495.  }
  496. }
  497.  
  498. main();
Add Comment
Please, Sign In to add comment