48 from daal.algorithms
import gbt
49 from daal.algorithms.gbt.classification
import prediction, training
50 from daal.algorithms
import classifier
51 from daal.data_management
import (
52 FileDataSource, DataSourceIface, NumericTableIface, HomogenNumericTable,
53 MergedNumericTable, data_feature_utils
56 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
57 if utils_folder
not in sys.path:
58 sys.path.insert(0, utils_folder)
59 from utils
import printNumericTable, printNumericTables
61 DAAL_PREFIX = os.path.join(
'..',
'data')
64 trainDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'df_classification_train.csv')
65 testDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'df_classification_test.csv')
72 minObservationsInLeafNode = 8
76 predictionResult =
None
77 testGroundTruth =
None
84 trainDataSource = FileDataSource(
86 DataSourceIface.notAllocateNumericTable,
87 DataSourceIface.doDictionaryFromContext
91 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
92 trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
93 mergedData = MergedNumericTable(trainData, trainGroundTruth)
96 trainDataSource.loadDataBlock(mergedData)
99 dict = trainData.getDictionary()
102 dict[0].featureType = data_feature_utils.DAAL_CONTINUOUS
103 dict[1].featureType = data_feature_utils.DAAL_CONTINUOUS
104 dict[2].featureType = data_feature_utils.DAAL_CATEGORICAL
107 algorithm = training.Batch(nClasses)
108 algorithm.parameter().maxIterations = maxIterations
109 algorithm.parameter().minObservationsInLeafNode = minObservationsInLeafNode
110 algorithm.parameter().featuresPerNode = nFeatures
113 algorithm.input.set(classifier.training.data, trainData)
114 algorithm.input.set(classifier.training.labels, trainGroundTruth)
117 trainingResult = algorithm.compute()
118 model = trainingResult.get(classifier.training.model)
121 global testGroundTruth, predictionResult
124 testDataSource = FileDataSource(
126 DataSourceIface.notAllocateNumericTable,
127 DataSourceIface.doDictionaryFromContext
131 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
132 testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
133 mergedData = MergedNumericTable(testData, testGroundTruth)
136 testDataSource.loadDataBlock(mergedData)
139 dict = testData.getDictionary()
142 dict[0].featureType = data_feature_utils.DAAL_CONTINUOUS
143 dict[1].featureType = data_feature_utils.DAAL_CONTINUOUS
144 dict[2].featureType = data_feature_utils.DAAL_CATEGORICAL
147 algorithm = prediction.Batch(nClasses)
150 algorithm.input.setTable(classifier.prediction.data, testData)
151 algorithm.input.setModel(classifier.prediction.model, model)
155 predictionResult = algorithm.compute()
160 printNumericTable(predictionResult.get(classifier.prediction.prediction),
"Gragient boosted trees prediction results (first 10 rows):",10)
161 printNumericTable(testGroundTruth,
"Ground truth (first 10 rows):",10)
163 if __name__ ==
"__main__":