#include "daal.h"
#include "service.h"
using namespace std;
using namespace daal;
using namespace daal::algorithms::logistic_regression;
const string trainedModelFileName = "../data/batch/logreg_trained_model.csv";
const string testDatasetFileName = "../data/batch/logreg_test.csv";
const size_t nFeatures = 6;
const size_t nClasses = 5;
ModelPtr buildModel();
void testModel(ModelPtr&);
int main(int argc, char *argv[])
{
checkArguments(argc, argv, 2, &trainedModelFileName, &testDatasetFileName);
ModelPtr builtModel = buildModel();
testModel(builtModel);
return 0;
}
ModelPtr buildModel()
{
FileDataSource<CSVFeatureManager> modelSource(trainedModelFileName,
DataSource::doAllocateNumericTable,
DataSource::doDictionaryFromContext);
NumericTablePtr beta(new HomogenNumericTable<>(nFeatures + 1, 0, NumericTable::doNotAllocate));
modelSource.loadDataBlock(beta.get());
BlockDescriptor<> blockResult;
beta->getBlockOfRows(0, nClasses, readOnly, blockResult);
size_t numberOfBetas = (beta->getNumberOfRows())*(beta->getNumberOfColumns());
float* first = blockResult.getBlockPtr();
float* last = first + numberOfBetas;
ModelBuilder<> modelBuilder(nFeatures, nClasses);
modelBuilder.setBeta(first, last);
beta->releaseBlockOfRows(blockResult);
printNumericTable(modelBuilder.getModel()->getBeta(), "Logistic Regression coefficients of built model:");
return modelBuilder.getModel();
}
void testModel(ModelPtr& inputModel)
{
FileDataSource<CSVFeatureManager> testDataSource(testDatasetFileName,
DataSource::doAllocateNumericTable,
DataSource::doDictionaryFromContext);
NumericTablePtr testData(new HomogenNumericTable<>(nFeatures, 0, NumericTable::doNotAllocate));
NumericTablePtr testGroundTruth(new HomogenNumericTable<>(1, 0, NumericTable::doNotAllocate));
NumericTablePtr mergedData(new MergedNumericTable(testData, testGroundTruth));
testDataSource.loadDataBlock(mergedData.get());
prediction::Batch<> algorithm(nClasses);
algorithm.input.set(algorithms::classifier::prediction::data, testData);
algorithm.input.set(algorithms::classifier::prediction::model, inputModel);
algorithm.compute();
NumericTablePtr prediction = algorithm.getResult()->get(algorithms::classifier::prediction::prediction);
printNumericTable(prediction, "Logistic Regression prediction results: (first 10 rows):", 10);
printNumericTable(testGroundTruth, "Ground truth (first 10 rows):", 10);
}