Exemplo n.º 1
0
void  RandomSplitJobManager::GenerateFinalResultsReport ()
{
  KKStr reportFileName = osGetRootName (ManagerName ()) + "_Results.html;";

  ofstream f (reportFileName.Str ());

  f << "Run Time Parameters" << endl
    << "Run Time"          << "\t" << osGetLocalDateTime ()  << endl
    << "configFileName"    << "\t" << configFileName         << endl
    << "DataFileName"      << "\t" << dataFileName           << endl
    << "Format"            << "\t" << format->DriverName ()  << endl
    << "DataIndexFileName" << "\t" << dataIndexFileName      << endl
    << "NumFolds"          << "\t" << numFolds               << endl
    << "NumSplits"         << "\t" << numSplits              << endl
    << "splitFraction"     << "\t" << splitFraction          << endl
    << endl;


  KKJobList::const_iterator  idx;

  ConfusionMatrix2  avgResults (*(this->MLClasses ()));
  KKB::uint  x = 0;

  for  (idx = Jobs ()->begin ();  idx != Jobs ()->end ();  idx++)
  {
    RandomSplitJobPtr j = dynamic_cast<RandomSplitJobPtr> (*idx);
    if  (j->RandomSplitsResults () != NULL)
    {
      f << endl
        << "Random Split[" << j->SplitNum () << "]" << endl;
      j->RandomSplitsResults ()->PrintConfusionMatrixTabDelimited (f);
      f << endl << endl;

      j->PrintClassCounts (f);
      f << endl << endl;

      avgResults.AddIn (*(j->RandomSplitsResults ()), log);
      x++;
    }
  }

  f << endl << "Mean Average of all random Splits." << endl;
  avgResults.FactorCounts (1.0 / (double)x);
  avgResults.PrintConfusionMatrixTabDelimited (f);
  f << endl 
    << endl
    << endl
    << endl
    << "Class Counts" << endl
    << endl;

  kkuint32  numClasses = (kkuint32)mlClasses->size ();

  VectorFloat   classAccs;
  VectorDouble  knownCounts;
  VectorDouble  predCounts;
  VectorDouble  adjCounts;
  VectorDouble  adjCountsStdError;
  VectorDouble  predDelta;       
  VectorDouble  adjDelta;
  
  KKStr l1, l2, l3;
  mlClasses->ExtractThreeTitleLines (l1, l2, l3);

  VectorKKStr  knownCountLines;
  VectorKKStr  predCountLines;
  VectorKKStr  adjCountLines;
  VectorKKStr  deltaPredCountLines;
  VectorKKStr  deltaAdjCountLines;
  VectorKKStr  accLines;


  ConfusionMatrix2  totalCM (*MLClasses ());
  int  totalCmCount = 0;


  // Known Counts
  for  (idx = Jobs ()->begin ();  idx != Jobs ()->end ();  idx++)
  {
    RandomSplitJobPtr j = dynamic_cast<RandomSplitJobPtr> (*idx);
    if  (j->RandomSplitsResults () != NULL)
    {
      KKStr splitNumStr = StrFormatInt (j->SplitNum (), "ZZZ0");
      j->GetClassCounts (classAccs, knownCounts, predCounts, adjCounts, adjCountsStdError, predDelta, adjDelta);

      totalCM.AddIn (*(j->RandomSplitsResults ()), log);
      totalCmCount++;
      KKStr accLine        = "Acc By Class\t" + splitNumStr;
      KKStr knownLine      = "Known\t"        + splitNumStr;
      KKStr predLine       = "Predicted\t"    + splitNumStr;
      KKStr adjLine        = "Adjusted\t"     + splitNumStr;
      KKStr deltaPredLine  = "Delta Pred\t"   + splitNumStr;
      KKStr deltaAdjLine   = "Delta Adj\t"    + splitNumStr;


      double  totalAcc       = 0.0;
      double  totalDeltaPred = 0.0;
      double  totalDeltaAdj  = 0.0;

      for  (x = 0;  x < numClasses;  x++)
      {
        accLine       << "\t" << StrFormatDouble (classAccs   [x], "zz0.00") << "%";
        knownLine     << "\t" << StrFormatDouble (knownCounts [x], "-Z,ZZZ,ZZ0.0");
        predLine      << "\t" << StrFormatDouble (predCounts  [x], "-Z,ZZZ,ZZ0.0");
        adjLine       << "\t" << StrFormatDouble (adjCounts   [x], "-Z,ZZZ,ZZ0.0");
        deltaPredLine << "\t" << StrFormatDouble (predDelta   [x], "-Z,ZZZ,ZZ0.0");
        deltaAdjLine  << "\t" << StrFormatDouble (adjDelta    [x], "-Z,ZZZ,ZZ0.0");
        totalAcc       += classAccs [x];
        totalDeltaPred += fabs (predDelta[x]);
        totalDeltaAdj  += fabs (adjDelta[x]);
      }


      accLine       << "\t" << StrFormatDouble ((totalAcc        / (double)classAccs.size ()), "ZZ0.00") << "%";
      deltaPredLine << "\t" << StrFormatDouble ((totalDeltaPred  / (double)predDelta.size ()), "ZZ0.00");
      deltaAdjLine  << "\t" << StrFormatDouble ((totalDeltaAdj   / (double)adjDelta.size  ()), "ZZ0.00");

      accLines.push_back            (accLine);
      knownCountLines.push_back     (knownLine);
      predCountLines.push_back      (predLine);
      adjCountLines.push_back       (adjLine);
      deltaPredCountLines.push_back (deltaPredLine);
      deltaAdjCountLines.push_back  (deltaAdjLine);
    }
  }
  double  factor = 0.0;
  if  (totalCmCount > 0)
    factor = 1.0 / (double)totalCmCount;

  totalCM.FactorCounts (factor);

  f << endl << endl
    << "Average Confusion  Matrix" << endl
    << endl;
  totalCM.PrintConfusionMatrixTabDelimited (f);

  f << ""            << "\t" << ""      << "\t" << l1 << endl
    << ""            << "\t" << "Split" << "\t" << l2 << endl
    << "Description" << "\t" << "Num"   << "\t" << l3 << endl;

  f << endl << endl;
  for  (x = 0;  x < knownCountLines.size ();  x++)
    f << knownCountLines[x] << endl;

  f << endl << endl;
  for  (x = 0;  x < predCountLines.size ();  x++)
    f << predCountLines[x] << endl;

  f << endl << endl;
  for  (x = 0;  x < adjCountLines.size ();  x++)
    f << adjCountLines[x] << endl;

  f << endl << endl;
  for  (x = 0;  x < deltaPredCountLines.size ();  x++)
    f << deltaPredCountLines[x] << endl;

  f << endl << endl;
  for  (x = 0;  x < deltaAdjCountLines.size ();  x++)
    f << deltaAdjCountLines[x] << endl;

  f << endl << endl;
  for  (x = 0;  x < knownCountLines.size ();  x++)
    f << accLines[x] << endl;

  VectorFloat  avgAccuracies = totalCM.AccuracyByClass ();
  f << "Avg-Accuracies";
  for  (x = 0;  x < avgAccuracies.size ();  x++)
    f << "\t" << StrFormatDouble (avgAccuracies[x], "zz0.00") << "%";
  f << "\t" << StrFormatDouble (totalCM.Accuracy (), "zz0.00") << "%";
  f << endl;

  f << endl << endl;

  f.close ();
}  /* GenerateFinalResultsReport */