package com.intel.daal.examples.gbt;
import com.intel.daal.algorithms.gbt.regression.*;
import com.intel.daal.algorithms.gbt.regression.prediction.*;
import com.intel.daal.algorithms.gbt.regression.training.*;
import com.intel.daal.algorithms.gbt.*;
import com.intel.daal.data_management.data.NumericTable;
import com.intel.daal.data_management.data.HomogenNumericTable;
import com.intel.daal.data_management.data.MergedNumericTable;
import com.intel.daal.data_management.data_source.DataSource;
import com.intel.daal.data_management.data_source.FileDataSource;
import com.intel.daal.examples.utils.Service;
import com.intel.daal.services.DaalContext;
import com.intel.daal.data_management.data.*;
class GbtRegDenseBatch {
private static final String trainDataset = "../data/batch/df_regression_train.csv";
private static final String testDataset = "../data/batch/df_regression_test.csv";
private static final int nFeatures = 13;
private static final int maxIterations = 40;
private static NumericTable testGroundTruth;
private static DaalContext context = new DaalContext();
public static void main(String[] args) throws java.io.FileNotFoundException, java.io.IOException {
TrainingResult trainingResult = trainModel();
PredictionResult predictionResult = testModel(trainingResult);
printResults(predictionResult);
context.dispose();
}
private static TrainingResult trainModel() {
FileDataSource trainDataSource = new FileDataSource(context, trainDataset,
DataSource.DictionaryCreationFlag.DoDictionaryFromContext,
DataSource.NumericTableAllocationFlag.NotAllocateNumericTable);
NumericTable trainData = new HomogenNumericTable(context, Float.class, nFeatures, 0, NumericTable.AllocationFlag.NotAllocate);
NumericTable trainGroundTruth = new HomogenNumericTable(context, Float.class, 1, 0, NumericTable.AllocationFlag.NotAllocate);
MergedNumericTable mergedData = new MergedNumericTable(context);
mergedData.addNumericTable(trainData);
mergedData.addNumericTable(trainGroundTruth);
trainDataSource.loadDataBlock(mergedData);
DataFeature categoricalFeature = trainData.getDictionary().getFeature(3);
categoricalFeature.setFeatureType(DataFeatureUtils.FeatureType.DAAL_CATEGORICAL);
TrainingBatch algorithm = new TrainingBatch(context, Float.class, TrainingMethod.defaultDense);
algorithm.parameter.setMaxIterations(maxIterations);
algorithm.input.set(InputId.data, trainData);
algorithm.input.set(InputId.dependentVariable, trainGroundTruth);
TrainingResult trainingResult = algorithm.compute();
return trainingResult;
}
private static PredictionResult testModel(TrainingResult trainingResult) {
FileDataSource testDataSource = new FileDataSource(context, testDataset,
DataSource.DictionaryCreationFlag.DoDictionaryFromContext,
DataSource.NumericTableAllocationFlag.NotAllocateNumericTable);
NumericTable testData = new HomogenNumericTable(context, Float.class, nFeatures, 0, NumericTable.AllocationFlag.NotAllocate);
testGroundTruth = new HomogenNumericTable(context, Float.class, 1, 0, NumericTable.AllocationFlag.NotAllocate);
MergedNumericTable mergedData = new MergedNumericTable(context);
mergedData.addNumericTable(testData);
mergedData.addNumericTable(testGroundTruth);
testDataSource.loadDataBlock(mergedData);
DataFeature categoricalFeature = testData.getDictionary().getFeature(3);
categoricalFeature.setFeatureType(DataFeatureUtils.FeatureType.DAAL_CATEGORICAL);
PredictionBatch algorithm = new PredictionBatch(context, Float.class, PredictionMethod.defaultDense);
Model model = trainingResult.get(TrainingResultId.model);
algorithm.input.set(NumericTableInputId.data, testData);
algorithm.input.set(ModelInputId.model, model);
return algorithm.compute();
}
private static void printResults(PredictionResult predictionResult) {
NumericTable predictionResults = predictionResult.get(PredictionResultId.prediction);
Service.printNumericTable("Gragient boosted trees prediction results (first 10 rows):", predictionResults, 10);
Service.printNumericTable("Ground truth (first 10 rows):", testGroundTruth, 10);
}
}