Exemple #1
0
/*********************************
 * Function: getStrongClassifier
 * -----------------------------
 * This function generates a strong classifier that can accurately distinguish
 * positive training examples from negative training examples
 *
 * td: set of feature vectors
 * num_classifiers: the number of weak classifiers we want to be in the strong
 * 		classifier (20 tends to be a good default it seems)
 *
 * returns: a strong classifier -- set of weak classifiers in optimal order
 */
StrongClassifier AdaBooster::getStrongClassifier(const TrainingData &trainingData, unsigned int num_classifiers){
  TrainingData td = trainingData;

  // set dimensions and number of features
  dimensions = td.dimensions();
  num_features = td.size();

  // initialize feature weights
  init_feature_weight(td);

  // vector of weak classifiers that make up a strong classifier
  vector<WeakClassifier> strong_classifier;

  // sort circle by features -- store in *sorted*
  create_feature_views(td);
  //td.printData();

  //char garbage[80]; // use this with cin.getline() below

  for (unsigned int i=0; i<num_classifiers; i++){
      // indentify best classifier
      WeakClassifier *wc = get_best_classifier();

	  // if index invalid, then we're done constructing our strong classifier
	  if (!wc)
		  return StrongClassifier(strong_classifier);

	  // otherwise, add best classifier to strong_classifier
	  strong_classifier.push_back(*wc);
	  //strong_classifier.back().printClassifier();

	  // don't need classifier anymore so delete it
	  delete wc;

	  // don't think this is useful anymore, but I'm scared to delete it
	  // without more testing, so it stays for now. TODO: delete this?
	  td.writeData(tdFile);

	  //td.printData();

	  // update weights of features
	  update_feature_weight(td, strong_classifier.back());

	  // print out status update
	  printf("\rClassifiers Calculated: %d ", i+1);
	  fflush(stdout);

      /*
      td.printData(); // prints out the set of training data
      cin.getline(garbage, 80); // if you want to pause printing, use this
      // */
  }
  // delete sorted array
  delete [] sorted;

  // StrongClassifier error reporting
  /*vector< vector<double> > strong_err = getStrongError(td, strong_classifier);
  printStrongStats(strong_err);*/

  return strong_classifier;
}