22 from daal.algorithms
import decision_forest
23 from daal.algorithms.decision_forest.regression
import prediction, training
24 from daal.data_management
import (
25 FileDataSource, DataSourceIface, NumericTableIface,
26 HomogenNumericTable, MergedNumericTable, data_feature_utils
29 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
30 if utils_folder
not in sys.path:
31 sys.path.insert(0, utils_folder)
32 from utils
import printNumericTable
34 DAAL_PREFIX = os.path.join(
'..',
'data')
37 trainDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'df_regression_train.csv')
38 testDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'df_regression_test.csv')
47 predictionResult =
None
48 testGroundTruth =
None
55 trainDataSource = FileDataSource(
57 DataSourceIface.notAllocateNumericTable,
58 DataSourceIface.doDictionaryFromContext
62 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
63 trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
64 mergedData = MergedNumericTable(trainData, trainGroundTruth)
67 trainDataSource.loadDataBlock(mergedData)
70 dict = trainData.getDictionary()
73 dict[3].featureType = data_feature_utils.DAAL_CATEGORICAL
76 algorithm = training.Batch()
77 algorithm.parameter.nTrees = nTrees
78 algorithm.parameter.varImportance = decision_forest.training.MDA_Raw
79 algorithm.parameter.resultsToCompute = decision_forest.training.computeOutOfBagError|decision_forest.training.computeOutOfBagErrorPerObservation;
82 algorithm.input.set(training.data, trainData)
83 algorithm.input.set(training.dependentVariable, trainGroundTruth)
86 trainingResult = algorithm.compute()
87 model = trainingResult.get(training.model)
88 printNumericTable(trainingResult.getTable(training.variableImportance),
"Variable importance results: ")
89 printNumericTable(trainingResult.getTable(training.outOfBagError),
"OOB error: ")
90 printNumericTable(trainingResult.getTable(training.outOfBagError),
"OOB error (first 10 rows): ", 10)
93 global testGroundTruth, predictionResult
96 testDataSource = FileDataSource(
98 DataSourceIface.notAllocateNumericTable,
99 DataSourceIface.doDictionaryFromContext
103 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
104 testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
105 mergedData = MergedNumericTable(testData, testGroundTruth)
108 testDataSource.loadDataBlock(mergedData)
111 dict = testData.getDictionary()
114 dict[3].featureType = data_feature_utils.DAAL_CATEGORICAL
117 algorithm = prediction.Batch()
120 algorithm.input.setTable(prediction.data, testData)
121 algorithm.input.set(prediction.model, model)
124 predictionResult = algorithm.compute()
130 predictionResult.get(prediction.prediction),
131 "Decision forest prediction results (first 10 rows):", 10
135 "Ground truth (first 10 rows):", 10
138 if __name__ ==
"__main__":