Ejemplo n.º 1
0
/*
    "getBestStump()" function saves the best decision stump's threshold, weak hypothesis and its error;
*/
void DSC::getBestStump(Threshold &threshold, ivec &hypothesis, double &error)
{
    /* Separating weights by classes: */
    mat class1Weights = weights(find(classes == -1));
    mat class2Weights = weights(find(classes == +1));

    /* Accumulate weights for given thresholds ("number of thresholds" x "number of features"): */
    mat thresholdWeights1 = accumarray(join_rows(class1Thresholds, featureIndexes1),
                                       repmat(class1Weights, features.n_cols, 1),
                                       SizeMat(N_THRESHOLDS, features.n_cols));
    mat thresholdWeights2 = accumarray(join_rows(class2Thresholds, featureIndexes2),
                                       repmat(class2Weights, features.n_cols, 1),
                                       SizeMat(N_THRESHOLDS, features.n_cols));

    /*
        Looking for threshold with minimum error;
        Here we construct cummulative sum of weights for seaprate classes, then we add them;
        In order to find the smallest error, we consider both directions of a threshold;
    */
    mat thresholdErrors = flipud(cumsum(flipud(thresholdWeights1))) + cumsum(thresholdWeights2);
    thresholdErrors = join_cols(thresholdErrors, sum(weights) - thresholdErrors);
    uword index;
    uword featureType;
    error = thresholdErrors.min(index, featureType);
    threshold.featureType = featureType;
    if (index > N_THRESHOLDS - 1)
    {
        threshold.direction = -1;
        index -= N_THRESHOLDS;
    }
    else
    {
        threshold.direction = +1;
    }

    threshold.value = minFeatures(featureType) + (ranges(featureType) / N_THRESHOLDS) * index;

    /* Evaluating hypothesis for derived threshold: */
    classify(threshold, features, hypothesis);

    return;
}
Ejemplo n.º 2
0
    void rotate(vector<vector<int> > &matrix) {
	flipud(matrix);
	transpose(matrix);
    }