24 from daal.algorithms
import decision_forest
25 from daal.algorithms.decision_forest.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.nTrees = nTrees
80 algorithm.parameter.varImportance = decision_forest.training.MDA_Raw
81 algorithm.parameter.resultsToCompute = decision_forest.training.computeOutOfBagError|decision_forest.training.computeOutOfBagErrorPerObservation;
84 algorithm.input.set(training.data, trainData)
85 algorithm.input.set(training.dependentVariable, trainGroundTruth)
88 trainingResult = algorithm.compute()
89 model = trainingResult.get(training.model)
90 printNumericTable(trainingResult.getTable(training.variableImportance),
"Variable importance results: ")
91 printNumericTable(trainingResult.getTable(training.outOfBagError),
"OOB error: ")
92 printNumericTable(trainingResult.getTable(training.outOfBagError),
"OOB error (first 10 rows): ", 10)
95 global testGroundTruth, predictionResult
98 testDataSource = FileDataSource(
100 DataSourceIface.notAllocateNumericTable,
101 DataSourceIface.doDictionaryFromContext
105 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
106 testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
107 mergedData = MergedNumericTable(testData, testGroundTruth)
110 testDataSource.loadDataBlock(mergedData)
113 dict = testData.getDictionary()
116 dict[3].featureType = features.DAAL_CATEGORICAL
119 algorithm = prediction.Batch()
122 algorithm.input.setTable(prediction.data, testData)
123 algorithm.input.set(prediction.model, model)
126 predictionResult = algorithm.compute()
132 predictionResult.get(prediction.prediction),
133 "Decision forest prediction results (first 10 rows):", 10
137 "Ground truth (first 10 rows):", 10
140 if __name__ ==
"__main__":