#include "daal.h"
#include "service.h"
using namespace std;
using namespace daal;
using namespace daal::algorithms::gbt::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 maxIterations = 40;
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().maxIterations = maxIterations;
algorithm.compute();
return algorithm.getResult();
}
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),
"Gragient boosted trees 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;
}