#include "daal.h"
#include "service.h"
using namespace std;
using namespace daal;
using namespace daal::algorithms;
string trainDatasetFileName = "../data/batch/logitboost_train.csv";
string testDatasetFileName = "../data/batch/logitboost_test.csv";
const size_t nFeatures = 20;
const size_t nClasses = 5;
const size_t maxIterations = 100;
const double accuracyThreshold = 0.01;
services::SharedPtr<logitboost::Model> model;
services::SharedPtr<classifier::prediction::Result> predictionResult;
services::SharedPtr<NumericTable> testGroundTruth;
void trainModel();
void testModel();
void printResults();
int main(int argc, char *argv[])
{
checkArguments(argc, argv, 2, &trainDatasetFileName, &testDatasetFileName);
trainModel();
testModel();
printResults();
return 0;
}
void trainModel()
{
FileDataSource<CSVFeatureManager> trainDataSource(trainDatasetFileName,
DataSource::notAllocateNumericTable,
DataSource::doDictionaryFromContext);
services::SharedPtr<NumericTable> trainData(new HomogenNumericTable<double>(nFeatures, 0, NumericTable::notAllocate));
services::SharedPtr<NumericTable> trainGroundTruth(new HomogenNumericTable<double>(1, 0, NumericTable::notAllocate));
services::SharedPtr<NumericTable> mergedData(new MergedNumericTable(trainData, trainGroundTruth));
trainDataSource.loadDataBlock(mergedData.get());
logitboost::training::Batch<> algorithm(nClasses);
algorithm.parameter.maxIterations = maxIterations;
algorithm.parameter.accuracyThreshold = accuracyThreshold;
algorithm.input.set(classifier::training::data, trainData);
algorithm.input.set(classifier::training::labels, trainGroundTruth);
algorithm.compute();
services::SharedPtr<logitboost::training::Result> trainingResult = algorithm.getResult();
model = trainingResult->get(classifier::training::model);
}
void testModel()
{
FileDataSource<CSVFeatureManager> testDataSource(testDatasetFileName,
DataSource::notAllocateNumericTable,
DataSource::doDictionaryFromContext);
services::SharedPtr<NumericTable> testData(new HomogenNumericTable<double>(nFeatures, 0, NumericTable::notAllocate));
testGroundTruth = services::SharedPtr<NumericTable>(new HomogenNumericTable<double>(1, 0, NumericTable::notAllocate));
services::SharedPtr<NumericTable> mergedData(new MergedNumericTable(testData, testGroundTruth));
testDataSource.loadDataBlock(mergedData.get());
logitboost::prediction::Batch<> algorithm(nClasses);
algorithm.input.set(classifier::prediction::data, testData);
algorithm.input.set(classifier::prediction::model, model);
algorithm.compute();
predictionResult = algorithm.getResult();
}
void printResults()
{
printNumericTables<int, int>(testGroundTruth,
predictionResult->get(classifier::prediction::prediction),
"Ground truth", "Classification results",
"LogitBoost classification results (first 20 observations):", 20);
}