48 from daal.algorithms
import gbt
49 from daal.algorithms.gbt.regression
import prediction, training
50 from daal.data_management
import (
51 FileDataSource, DataSourceIface, NumericTableIface,
52 HomogenNumericTable, MergedNumericTable, data_feature_utils
55 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
56 if utils_folder
not in sys.path:
57 sys.path.insert(0, utils_folder)
58 from utils
import printNumericTable
60 DAAL_PREFIX = os.path.join(
'..',
'data')
63 trainDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'df_regression_train.csv')
64 testDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'df_regression_test.csv')
73 predictionResult =
None 74 testGroundTruth =
None 81 trainDataSource = FileDataSource(
83 DataSourceIface.notAllocateNumericTable,
84 DataSourceIface.doDictionaryFromContext
88 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
89 trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
90 mergedData = MergedNumericTable(trainData, trainGroundTruth)
93 trainDataSource.loadDataBlock(mergedData)
96 dict = trainData.getDictionary()
99 dict[3].featureType = data_feature_utils.DAAL_CATEGORICAL
102 algorithm = training.Batch()
103 algorithm.parameter().maxIterations = maxIterations
106 algorithm.input.set(training.data, trainData)
107 algorithm.input.set(training.dependentVariable, trainGroundTruth)
110 trainingResult = algorithm.compute()
111 model = trainingResult.get(training.model)
114 global testGroundTruth, predictionResult
117 testDataSource = FileDataSource(
119 DataSourceIface.notAllocateNumericTable,
120 DataSourceIface.doDictionaryFromContext
124 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
125 testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
126 mergedData = MergedNumericTable(testData, testGroundTruth)
129 testDataSource.loadDataBlock(mergedData)
132 dict = testData.getDictionary()
135 dict[3].featureType = data_feature_utils.DAAL_CATEGORICAL
138 algorithm = prediction.Batch()
141 algorithm.input.setTable(prediction.data, testData)
142 algorithm.input.set(prediction.model, model)
145 predictionResult = algorithm.compute()
151 predictionResult.get(prediction.prediction),
152 "Gradient boosted trees prediction results (first 10 rows):", 10
156 "Ground truth (first 10 rows):", 10
159 if __name__ ==
"__main__":