#include "daal.h"
#include "service.h"
#include <cstdio>
using namespace std;
using namespace daal;
using namespace daal::algorithms;
string trainDatasetFileName = "../data/batch/decision_tree_train.csv";
string pruneDatasetFileName = "../data/batch/decision_tree_prune.csv";
string testDatasetFileName = "../data/batch/decision_tree_test.csv";
const size_t nFeatures = 5;
const size_t nClasses = 5;
decision_tree::classification::training::ResultPtr trainingResult;
classifier::prediction::ResultPtr predictionResult;
NumericTablePtr 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);
NumericTablePtr trainData(new HomogenNumericTable<>(nFeatures, 0, NumericTable::notAllocate));
NumericTablePtr trainGroundTruth(new HomogenNumericTable<>(1, 0, NumericTable::notAllocate));
NumericTablePtr mergedData(new MergedNumericTable(trainData, trainGroundTruth));
trainDataSource.loadDataBlock(mergedData.get());
FileDataSource<CSVFeatureManager> pruneDataSource(pruneDatasetFileName,
DataSource::notAllocateNumericTable,
DataSource::doDictionaryFromContext);
NumericTablePtr pruneData(new HomogenNumericTable<>(nFeatures, 0, NumericTable::notAllocate));
NumericTablePtr pruneGroundTruth(new HomogenNumericTable<>(1, 0, NumericTable::notAllocate));
NumericTablePtr pruneMergedData(new MergedNumericTable(pruneData, pruneGroundTruth));
pruneDataSource.loadDataBlock(pruneMergedData.get());
decision_tree::classification::training::Batch<> algorithm(nClasses);
algorithm.input.set(classifier::training::data, trainData);
algorithm.input.set(classifier::training::labels, trainGroundTruth);
algorithm.input.set(decision_tree::classification::training::dataForPruning, pruneData);
algorithm.input.set(decision_tree::classification::training::labelsForPruning, pruneGroundTruth);
algorithm.compute();
trainingResult = algorithm.getResult();
}
void testModel()
{
FileDataSource<CSVFeatureManager> testDataSource(testDatasetFileName,
DataSource::notAllocateNumericTable,
DataSource::doDictionaryFromContext);
NumericTablePtr testData(new HomogenNumericTable<>(nFeatures, 0, NumericTable::notAllocate));
testGroundTruth = NumericTablePtr(new HomogenNumericTable<>(1, 0, NumericTable::notAllocate));
NumericTablePtr mergedData(new MergedNumericTable(testData, testGroundTruth));
testDataSource.loadDataBlock(mergedData.get());
decision_tree::classification::prediction::Batch<> algorithm;
algorithm.input.set(classifier::prediction::data, testData);
algorithm.input.set(classifier::prediction::model, trainingResult->get(classifier::training::model));
algorithm.compute();
predictionResult = algorithm.getResult();
}
void printResults()
{
printNumericTables<int, int>(testGroundTruth,
predictionResult->get(classifier::prediction::prediction),
"Ground truth", "Classification results",
"Decision tree classification results (first 20 observations):", 20);
}