Python* API Reference for Intel® Data Analytics Acceleration Library 2020 Update 1

dt_reg_dense_batch.py

1 # file: dt_reg_dense_batch.py
2 #===============================================================================
3 # Copyright 2014-2020 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #===============================================================================
17 
18 
19 
20 
21 import os
22 import sys
23 
24 from daal.algorithms.decision_tree.regression import prediction, training
25 from daal.data_management import (
26  FileDataSource, DataSourceIface, NumericTableIface, HomogenNumericTable, MergedNumericTable
27 )
28 
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 printNumericTables
33 
34 DAAL_PREFIX = os.path.join('..', 'data')
35 
36 # Input data set parameters
37 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'decision_tree_train.csv')
38 pruneDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'decision_tree_prune.csv')
39 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'decision_tree_test.csv')
40 
41 nFeatures = 5
42 
43 # Model object for the decision tree regression algorithm
44 model = None
45 predictionResult = None
46 testGroundTruth = None
47 
48 
49 def trainModel():
50  global model
51 
52  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
53  trainDataSource = FileDataSource(
54  trainDatasetFileName,
55  DataSourceIface.notAllocateNumericTable,
56  DataSourceIface.doDictionaryFromContext
57  )
58 
59  # Create Numeric Tables for training data and labels
60  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
61  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
62  mergedData = MergedNumericTable(trainData, trainGroundTruth)
63 
64  # Retrieve the data from the input file
65  trainDataSource.loadDataBlock(mergedData)
66 
67  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
68  pruneDataSource = FileDataSource(
69  pruneDatasetFileName,
70  DataSourceIface.notAllocateNumericTable,
71  DataSourceIface.doDictionaryFromContext
72  )
73 
74  # Create Numeric Tables for pruning data and labels
75  pruneData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
76  pruneGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
77  pruneMergedData = MergedNumericTable(pruneData, pruneGroundTruth)
78 
79  # Retrieve the data from the input file
80  pruneDataSource.loadDataBlock(pruneMergedData)
81 
82  # Create an algorithm object to train the decision tree regression model
83  algorithm = training.Batch()
84 
85  # Pass the training data set and dependent values to the algorithm
86  algorithm.input.set(training.data, trainData)
87  algorithm.input.set(training.dependentVariables, trainGroundTruth)
88  algorithm.input.set(training.dataForPruning, pruneData)
89  algorithm.input.set(training.dependentVariablesForPruning, pruneGroundTruth)
90 
91  # Train the decision tree regression model and retrieve the results of the training algorithm
92  trainingResult = algorithm.compute()
93  model = trainingResult.get(training.model)
94 
95 def testModel():
96  global testGroundTruth, predictionResult
97 
98  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
99  testDataSource = FileDataSource(
100  testDatasetFileName,
101  DataSourceIface.notAllocateNumericTable,
102  DataSourceIface.doDictionaryFromContext
103  )
104 
105  # Create Numeric Tables for testing data and labels
106  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
107  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
108  mergedData = MergedNumericTable(testData, testGroundTruth)
109 
110  # Retrieve the data from input file
111  testDataSource.loadDataBlock(mergedData)
112 
113  # Create algorithm objects for decision tree regression prediction with the default method
114  algorithm = prediction.Batch()
115 
116  # Pass the testing data set and trained model to the algorithm
117  #print("Number of columns: {}".format(testData.getNumberOfColumns()))
118  algorithm.input.setTable(prediction.data, testData)
119  algorithm.input.setModel(prediction.model, model)
120 
121  # Compute prediction results and retrieve algorithm results
122  predictionResult = algorithm.compute()
123 
124 
125 def printResults():
126 
127  printNumericTables(testGroundTruth, predictionResult.get(prediction.prediction),
128  "Ground truth", "Regression results",
129  "Decision tree regression results (first 20 observations):",
130  20, flt64=False)
131 
132 if __name__ == "__main__":
133 
134  trainModel()
135  testModel()
136  printResults()

For more complete information about compiler optimizations, see our Optimization Notice.