Esempio n. 1
0
ErrorStruct StrongClassifier::errorForFeatures(const TrainingData &features, bool printStats) const {

  ErrorStruct e;

  for (int i=0; i<features.size(); i++) {
    FeatureVector feature = *(features.feature(i));
    if (decide(feature)) {
      feature.val() == POS ? e.true_pos++ : e.false_pos++;//it is really positive
    } else {
      feature.val() == NEG ? e.true_neg++ : e.false_neg++;//it is really negative
    }
  }

  // if all 10 samples, 3 is misclassified, error is 3/10
  e.error = (e.false_pos + e.false_neg) / ((float)features.size()); 

  if (printStats) {
    std::cout << e.true_pos << " true positives" << std::endl;
    std::cout << e.false_pos << " false positives" << std::endl;
    std::cout << e.true_neg << " true negatives" << std::endl;
    std::cout << e.false_neg << " false negatives" << std::endl;
    std::cout << e.error * 100 << "% error" << std::endl;
    std::cout << std::endl;
  }

  return e;
}
Esempio n. 2
0
/*************************************
 * Function: update_feature_weight
 * -------------------------------
 * Given TrainingData and a WeakClassifier that has been weighted in
 * get_best_classifier(), we recalculate the weights of all the features
 *
 * td: training data (set of features)
 * wc: (weighted) weak classifier
 *
 * returns true if successful, false otherwise
 */
bool AdaBooster::update_feature_weight(TrainingData &td, WeakClassifier &wc){
	// check that WeakClassifier has actually been weighted
	if (wc.weight() < 0){
		printf("Error in update_feature_weight: WeakClassifier has invalid weight\n");
		return false;
	}

	// traverse features in feature set and adjust their weights
	for (unsigned int i=0; i<num_features; i++){
		FeatureVector* fv = td.feature(i);
		// either 1 or -1 (used in weight below)
		int is_correct = is_classifier_correct(wc, *fv) ? 1 : -1;

		// calculate and update weight
		// note M_E := 2.71828
		float weight = pow(M_E, (double) -1 * wc.weight() * is_correct);
		td.setWeight(i, td.weight(i)*weight);
	}

	// calculate normalization factor
	float norm = 0;
	for (unsigned int i=0; i<num_features; i++)
		norm += td.weight(i);

	// normalize feature weights
	for (unsigned int i=0; i<num_features; i++)
		td.setWeight(i, td.weight(i)/norm);
	
	return true; // successful
}
Esempio n. 3
0
/**************************************
 * Function: create_feature_views
 * ------------------------------
 * For each dimension, we sort the feature vectors and store the result in
 * *sorted*. Note that *sorted* is a little complicated -- we need the sorting to
 * be fast so we sort feature vector pointers, and we have a vector of these
 * for each dimension.
 *
 * td: training data -- set of features
 */
void AdaBooster::create_feature_views(TrainingData &td){
	sorted = new vector<FeatureVector *>[dimensions];
	// add a feature pointer to sorted for each dimension in the training data
	for (unsigned int i=0; i<dimensions; i++){
		for (unsigned int j=0; j<num_features; j++){
			sorted[i].push_back(td.feature(j));
		}
	}

	// sort each feature vector pointer in sorted
	for (unsigned int i=0; i<dimensions; i++)
		sort(sorted[i].begin(), sorted[i].end(), idx_cmp(i));
}