#include "daal.h"
#include "service.h"
using namespace std;
using namespace daal;
using namespace daal::algorithms::decision_forest::regression;
const string trainDatasetFileName = "../data/batch/df_regression_train.csv";
const string testDatasetFileName = "../data/batch/df_regression_test.csv";
const size_t categoricalFeaturesIndices[] = { 3 };
const size_t nFeatures = 13;
const size_t nTrees = 100;
training::ResultPtr trainModel();
void testModel(const training::ResultPtr& res);
void loadData(const std::string& fileName, NumericTablePtr& pData, NumericTablePtr& pDependentVar);
int main(int argc, char *argv[])
{
checkArguments(argc, argv, 2, &trainDatasetFileName, &testDatasetFileName);
training::ResultPtr trainingResult = trainModel();
testModel(trainingResult);
return 0;
}
training::ResultPtr trainModel()
{
NumericTablePtr trainData;
NumericTablePtr trainDependentVariable;
loadData(trainDatasetFileName, trainData, trainDependentVariable);
training::Batch<> algorithm;
algorithm.input.set(training::data, trainData);
algorithm.input.set(training::dependentVariable, trainDependentVariable);
algorithm.parameter.nTrees = nTrees;
algorithm.parameter.varImportance = daal::algorithms::decision_forest::training::MDA_Raw;
algorithm.parameter.resultsToCompute = daal::algorithms::decision_forest::training::computeOutOfBagError |
daal::algorithms::decision_forest::training::computeOutOfBagErrorPerObservation;
algorithm.compute();
training::ResultPtr trainingResult = algorithm.getResult();
printNumericTable(trainingResult->get(training::variableImportance), "Variable importance results: ");
printNumericTable(trainingResult->get(training::outOfBagError), "OOB error: ");
printNumericTable(trainingResult->get(training::outOfBagErrorPerObservation), "OOB error per observation (first 10 rows):", 10);
return trainingResult;
}
void testModel(const training::ResultPtr& trainingResult)
{
NumericTablePtr testData;
NumericTablePtr testGroundTruth;
loadData(testDatasetFileName, testData, testGroundTruth);
prediction::Batch<> algorithm;
algorithm.input.set(prediction::data, testData);
algorithm.input.set(prediction::model, trainingResult->get(training::model));
algorithm.compute();
prediction::ResultPtr predictionResult = algorithm.getResult();
printNumericTable(predictionResult->get(prediction::prediction),
"Decision forest prediction results (first 10 rows):", 10);
printNumericTable(testGroundTruth, "Ground truth (first 10 rows):", 10);
}
void loadData(const std::string& fileName, NumericTablePtr& pData, NumericTablePtr& pDependentVar)
{
FileDataSource<CSVFeatureManager> trainDataSource(fileName,
DataSource::notAllocateNumericTable,
DataSource::doDictionaryFromContext);
pData.reset(new HomogenNumericTable<>(nFeatures, 0, NumericTable::notAllocate));
pDependentVar.reset(new HomogenNumericTable<>(1, 0, NumericTable::notAllocate));
NumericTablePtr mergedData(new MergedNumericTable(pData, pDependentVar));
trainDataSource.loadDataBlock(mergedData.get());
NumericTableDictionaryPtr pDictionary = pData->getDictionarySharedPtr();
for(size_t i = 0, n = sizeof(categoricalFeaturesIndices) / sizeof(categoricalFeaturesIndices[0]); i < n; ++i)
(*pDictionary)[categoricalFeaturesIndices[i]].featureType = data_feature_utils::DAAL_CATEGORICAL;
}