47 from daal.algorithms
import gbt
48 from daal.algorithms.gbt.regression
import prediction, training
49 from daal.data_management
import (
50 FileDataSource, DataSourceIface, NumericTableIface,
51 HomogenNumericTable, MergedNumericTable, data_feature_utils
54 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
55 if utils_folder
not in sys.path:
56 sys.path.insert(0, utils_folder)
57 from utils
import printNumericTable
59 DAAL_PREFIX = os.path.join(
'..',
'data')
62 trainDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'df_regression_train.csv')
63 testDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'df_regression_test.csv')
72 predictionResult =
None
73 testGroundTruth =
None
80 trainDataSource = FileDataSource(
82 DataSourceIface.notAllocateNumericTable,
83 DataSourceIface.doDictionaryFromContext
87 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
88 trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
89 mergedData = MergedNumericTable(trainData, trainGroundTruth)
92 trainDataSource.loadDataBlock(mergedData)
95 dict = trainData.getDictionary()
98 dict[3].featureType = data_feature_utils.DAAL_CATEGORICAL
101 algorithm = training.Batch()
102 algorithm.parameter().maxIterations = maxIterations
105 algorithm.input.set(training.data, trainData)
106 algorithm.input.set(training.dependentVariable, trainGroundTruth)
109 trainingResult = algorithm.compute()
110 model = trainingResult.get(training.model)
113 global testGroundTruth, predictionResult
116 testDataSource = FileDataSource(
118 DataSourceIface.notAllocateNumericTable,
119 DataSourceIface.doDictionaryFromContext
123 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
124 testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
125 mergedData = MergedNumericTable(testData, testGroundTruth)
128 testDataSource.loadDataBlock(mergedData)
131 dict = testData.getDictionary()
134 dict[3].featureType = data_feature_utils.DAAL_CATEGORICAL
137 algorithm = prediction.Batch()
140 algorithm.input.setTable(prediction.data, testData)
141 algorithm.input.set(prediction.model, model)
144 predictionResult = algorithm.compute()
150 predictionResult.get(prediction.prediction),
151 "Gradient boosted trees prediction results (first 10 rows):", 10
155 "Ground truth (first 10 rows):", 10
158 if __name__ ==
"__main__":