SG++
CrossValidationExample.cpp

This example can be found under datadriven/examples/CrossValidationExample.cpp.

/* Copyright (C) 2008-today The SG++ project
* This file is part of the SG++ project. For conditions of distribution and
* use, please see the copyright notice provided with SG++ or at
* sgpp.sparsegrids.org
*
* regressionPipeline.cpp
*
* Created on: 01.06.2016
* Author: Michael Lettrich
*/
#include <iostream>
#include <memory>
#include <string>
int main(int argc, char** argv) {
const std::string path = [argc, &argv]() {
if (argc != 2) {
std::cout << "No or bad path given, aborting\n";
exit(1);
return std::string{};
} else {
return std::string{argv[1]};
}
}();
auto dataSource = std::unique_ptr<DataSource>(DataSourceBuilder().withPath(path).assemble());
std::cout << "reading input file: " << path << std::endl;
auto dataset = std::unique_ptr<Dataset>(dataSource->getNextSamples());
auto config = FitterConfigurationLeastSquares{};
config.setupDefaults();
auto& gridConfig = config.getGridConfig();
gridConfig.level_ = 2;
gridConfig.type_ = GridType::ModLinear;
gridConfig.dim_ = dataset->getDimension();
auto& regularizationConfig = config.getRegularizationConfig();
regularizationConfig.lambda_ = 10e-1;
std::cout << "starting 5 fold cross validation with seed 42" << std::endl;
auto model = ModelFittingLeastSquares{config};
double stdDeviation = 0;
CrossValidation scorer{new MSE{}, new RandomShufflingFunctor{}, 42, 5};
auto score = scorer.calculateScore(model, *dataset, &stdDeviation);
std::cout << "Score = " << score << " with stdDeviation " << stdDeviation << std::endl;
return 0;
}