24 from daal.algorithms
import gbt
25 from daal.algorithms.gbt.regression
import prediction, training
26 from daal.data_management
import (
27 FileDataSource, DataSourceIface, NumericTableIface,
28 HomogenNumericTable, MergedNumericTable, features
31 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
32 if utils_folder
not in sys.path:
33 sys.path.insert(0, utils_folder)
34 from utils
import printNumericTable
36 DAAL_PREFIX = os.path.join(
'..',
'data')
39 trainDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'df_regression_train.csv')
40 testDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'df_regression_test.csv')
49 predictionResult =
None
50 testGroundTruth =
None
57 trainDataSource = FileDataSource(
59 DataSourceIface.notAllocateNumericTable,
60 DataSourceIface.doDictionaryFromContext
64 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
65 trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
66 mergedData = MergedNumericTable(trainData, trainGroundTruth)
69 trainDataSource.loadDataBlock(mergedData)
72 dict = trainData.getDictionary()
75 dict[3].featureType = features.DAAL_CATEGORICAL
78 algorithm = training.Batch()
79 algorithm.parameter().maxIterations = maxIterations
82 algorithm.input.set(training.data, trainData)
83 algorithm.input.set(training.dependentVariable, trainGroundTruth)
86 trainingResult = algorithm.compute()
87 model = trainingResult.get(training.model)
90 global testGroundTruth, predictionResult
93 testDataSource = FileDataSource(
95 DataSourceIface.notAllocateNumericTable,
96 DataSourceIface.doDictionaryFromContext
100 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
101 testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
102 mergedData = MergedNumericTable(testData, testGroundTruth)
105 testDataSource.loadDataBlock(mergedData)
108 dict = testData.getDictionary()
111 dict[3].featureType = features.DAAL_CATEGORICAL
114 algorithm = prediction.Batch()
117 algorithm.input.setTable(prediction.data, testData)
118 algorithm.input.set(prediction.model, model)
121 predictionResult = algorithm.compute()
127 predictionResult.get(prediction.prediction),
128 "Gradient boosted trees prediction results (first 10 rows):", 10
132 "Ground truth (first 10 rows):", 10
135 if __name__ ==
"__main__":