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

gbt_reg_dense_batch.py

1 # file: gbt_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 gbt
25 from daal.algorithms.gbt.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 # Gradient boosted trees parameters
45 maxIterations = 40
46 
47 # Model object for the gradient boosted trees 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 gradient boosted trees regression model
78  algorithm = training.Batch()
79  algorithm.parameter().maxIterations = maxIterations
80 
81  # Pass the training data set and dependent values to the algorithm
82  algorithm.input.set(training.data, trainData)
83  algorithm.input.set(training.dependentVariable, trainGroundTruth)
84 
85  # Train the gradient boosted trees regression model and retrieve the results of the training algorithm
86  trainingResult = algorithm.compute()
87  model = trainingResult.get(training.model)
88 
89 def testModel():
90  global testGroundTruth, predictionResult
91 
92  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
93  testDataSource = FileDataSource(
94  testDatasetFileName,
95  DataSourceIface.notAllocateNumericTable,
96  DataSourceIface.doDictionaryFromContext
97  )
98 
99  # Create Numeric Tables for testing data and labels
100  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
101  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
102  mergedData = MergedNumericTable(testData, testGroundTruth)
103 
104  # Retrieve the data from input file
105  testDataSource.loadDataBlock(mergedData)
106 
107  # Get the dictionary and update it with additional information about data
108  dict = testData.getDictionary()
109 
110  # Add a feature type to the dictionary
111  dict[3].featureType = features.DAAL_CATEGORICAL
112 
113  # Create algorithm objects for the gradient boosted trees regression prediction with the default method
114  algorithm = prediction.Batch()
115 
116  # Pass the testing data set and trained model to the algorithm
117  algorithm.input.setTable(prediction.data, testData)
118  algorithm.input.set(prediction.model, model)
119 
120  # Compute prediction results and retrieve algorithm results
121  predictionResult = algorithm.compute()
122 
123 
124 def printResults():
125 
126  printNumericTable(
127  predictionResult.get(prediction.prediction),
128  "Gradient boosted trees prediction results (first 10 rows):", 10
129  )
130  printNumericTable(
131  testGroundTruth,
132  "Ground truth (first 10 rows):", 10
133  )
134 
135 if __name__ == "__main__":
136 
137  trainModel()
138  testModel()
139  printResults()

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