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

df_reg_dense_batch.py

1 # file: df_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 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
29 )
30 
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
35 
36 DAAL_PREFIX = os.path.join('..', 'data')
37 
38 # Input data set parameters
39 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'df_regression_train.csv')
40 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'df_regression_test.csv')
41 
42 nFeatures = 13
43 
44 # Decision forest parameters
45 nTrees = 100
46 
47 # Model object for the decision forest regression algorithm
48 model = None
49 predictionResult = None
50 testGroundTruth = None
51 
52 
53 def trainModel():
54  global model
55 
56  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
57  trainDataSource = FileDataSource(
58  trainDatasetFileName,
59  DataSourceIface.notAllocateNumericTable,
60  DataSourceIface.doDictionaryFromContext
61  )
62 
63  # Create Numeric Tables for training data and labels
64  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
65  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
66  mergedData = MergedNumericTable(trainData, trainGroundTruth)
67 
68  # Retrieve the data from the input file
69  trainDataSource.loadDataBlock(mergedData)
70 
71  # Get the dictionary and update it with additional information about data
72  dict = trainData.getDictionary()
73 
74  # Add a feature type to the dictionary
75  dict[3].featureType = features.DAAL_CATEGORICAL
76 
77  # Create an algorithm object to train the decision forest regression model
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;
82 
83  # Pass the training data set and dependent values to the algorithm
84  algorithm.input.set(training.data, trainData)
85  algorithm.input.set(training.dependentVariable, trainGroundTruth)
86 
87  # Train the decision forest regression model and retrieve the results of the training algorithm
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)
93 
94 def testModel():
95  global testGroundTruth, predictionResult
96 
97  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
98  testDataSource = FileDataSource(
99  testDatasetFileName,
100  DataSourceIface.notAllocateNumericTable,
101  DataSourceIface.doDictionaryFromContext
102  )
103 
104  # Create Numeric Tables for testing data and labels
105  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
106  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
107  mergedData = MergedNumericTable(testData, testGroundTruth)
108 
109  # Retrieve the data from input file
110  testDataSource.loadDataBlock(mergedData)
111 
112  # Get the dictionary and update it with additional information about data
113  dict = testData.getDictionary()
114 
115  # Add a feature type to the dictionary
116  dict[3].featureType = features.DAAL_CATEGORICAL
117 
118  # Create algorithm objects for decision forest regression prediction with the default method
119  algorithm = prediction.Batch()
120 
121  # Pass the testing data set and trained model to the algorithm
122  algorithm.input.setTable(prediction.data, testData)
123  algorithm.input.set(prediction.model, model)
124 
125  # Compute prediction results and retrieve algorithm results
126  predictionResult = algorithm.compute()
127 
128 
129 def printResults():
130 
131  printNumericTable(
132  predictionResult.get(prediction.prediction),
133  "Decision forest prediction results (first 10 rows):", 10
134  )
135  printNumericTable(
136  testGroundTruth,
137  "Ground truth (first 10 rows):", 10
138  )
139 
140 if __name__ == "__main__":
141 
142  trainModel()
143  testModel()
144  printResults()

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