KM.cpp

#include <Rng/GlobalRng.h>
#include <ReClaM/ArtificialDistributions.h>
#include <ReClaM/Dataset.h>
#include <ReClaM/KernelMeanClassifier.h>
#include <ReClaM/ClassificationError.h>
#include <stdio.h>
#include <iostream>


using namespace std;


int main()
{
    Rng::seed(10);

    double gamma = 0.5;
    RBFKernel k(gamma);

    cout << endl;
    cout << "*** kernel mean classifier ***" << endl;
    cout << endl;

    // create the xor problem with uniformly distributed examples

    cout << "Generating 100 training and 10000 test examples ..." << flush;
    Chessboard chess(2, 2);
    Dataset dataset(chess, 100, 10000);
    const Array<double>& x = dataset.getTrainingData();
    const Array<double>& y = dataset.getTrainingTarget();
    cout << " done." << endl;

    // create the kernel mean classifier
    KernelMeanClassifier kmc(x, y, &k);

    // estimate the accuracy on the test set
    cout << "Testing ..." << flush;
    ClassificationError ce;
    double acc = 1.0 - ce.error(kmc, dataset.getTestData(), dataset.getTestTarget());
    cout << " done." << endl;
    cout << "Estimated accuracy: " << 100.0 * acc << "%" << endl << endl;

    // lines below are for self-testing this example, please ignore
    if (acc >= 0.8) exit(EXIT_SUCCESS);
    else exit(EXIT_FAILURE);
}