Exemple #1
0
int main(int argc, char* argv[]) {
    ParameterABCrossValidate parameters = parseCommandline(argc, argv);
    
    if (parameters.verbose) {
        std::string boostingTypeName[3] = {"discrete", "real", "gentle"};
        std::cerr << std::endl;
        std::cerr << "Traing data:  " << parameters.trainingDataFilename << std::endl;
        std::cerr << "   Type:      " << boostingTypeName[parameters.boostingType] << std::endl;
        std::cerr << "   #rounds:   " << parameters.roundTotal << std::endl;
        std::cerr << "   #" << parameters.k_cv << " fold cross-valiation" << std::endl;
        std::cerr << std::endl;
    }
    
    AdaBoost adaBoost;
    adaBoost.setBoostingType(parameters.boostingType);
    adaBoost.readAllSamples(parameters.trainingDataFilename, parameters.k_cv);
    double accuracyAll = 0.0;
    for (int i = 0; i < parameters.k_cv; i++) {
        if (parameters.verbose)
            std::cout << "====== CV Round " << i << " ======" << std::endl;
        adaBoost.setCrossValidationSampels(i, parameters.k_cv);
        adaBoost.train(parameters.roundTotal, parameters.verbose);
        TestResult res = adaBoost.test(false, "");
        accuracyAll += res.accuracyAll;
        if (parameters.verbose) {
            std::cout << "Accuracy = " << res.accuracyAll << std::endl;
            std::cout << "  precision: " << res.precision << " , negative: " << res.recall << std::endl;

        }
    }
    std::cout << "\nCross Validation Accuracy: " << accuracyAll / parameters.k_cv << std::endl;
}
int main (int argc, const char * argv[])
{
    //Create a new AdaBoost instance
    AdaBoost adaBoost;

    //Set the weak classifier you want to use
    adaBoost.setWeakClassifier( DecisionStump() );

    //Load some training data to train the classifier
    ClassificationData trainingData;

    if( !trainingData.load("AdaBoostTrainingData.grt") ){
        cout << "Failed to load training data!\n";
        return EXIT_FAILURE;
    }

    //Use 20% of the training dataset to create a test dataset
    ClassificationData testData = trainingData.partition( 80 );

    //Train the classifier
    if( !adaBoost.train( trainingData ) ){
        cout << "Failed to train classifier!\n";
        return EXIT_FAILURE;
    }

    //Save the model to a file
    if( !adaBoost.save("AdaBoostModel.grt") ){
        cout << "Failed to save the classifier model!\n";
        return EXIT_FAILURE;
    }

    //Load the model from a file
    if( !adaBoost.load("AdaBoostModel.grt") ){
        cout << "Failed to load the classifier model!\n";
        return EXIT_FAILURE;
    }

    //Use the test dataset to test the AdaBoost model
    double accuracy = 0;
    for(UINT i=0; i<testData.getNumSamples(); i++){
        //Get the i'th test sample
        UINT classLabel = testData[i].getClassLabel();
        vector< double > inputVector = testData[i].getSample();

        //Perform a prediction using the classifier
        if( !adaBoost.predict( inputVector ) ){
            cout << "Failed to perform prediction for test sampel: " << i <<"\n";
            return EXIT_FAILURE;
        }

        //Get the predicted class label
        UINT predictedClassLabel = adaBoost.getPredictedClassLabel();
        double maximumLikelhood = adaBoost.getMaximumLikelihood();
        vector< double > classLikelihoods = adaBoost.getClassLikelihoods();
        vector< double > classDistances = adaBoost.getClassDistances();

        //Update the accuracy
        if( classLabel == predictedClassLabel ) accuracy++;

        cout << "TestSample: " << i <<  " ClassLabel: " << classLabel;
        cout << " PredictedClassLabel: " << predictedClassLabel << " Likelihood: " << maximumLikelhood;
        cout << endl;
    }

    cout << "Test Accuracy: " << accuracy/double(testData.getNumSamples())*100.0 << "%" << endl;

    return EXIT_SUCCESS;
}