Python* API Reference for Intel® Data Analytics Acceleration Library 2019 Update 5

gbt_reg_dense_batch.py

Deprecation Notice: With the introduction of daal4py, a package that supersedes PyDAAL, Intel is deprecating PyDAAL and will discontinue support starting with Intel® DAAL 2021 and Intel® Distribution for Python 2021. Until then Intel will continue to provide compatible pyDAAL pip and conda packages for newer releases of Intel DAAL and make it available in open source. However, Intel will not add the new features of Intel DAAL to pyDAAL. Intel recommends developers switch to and use daal4py.

Note: To find daal4py examples, refer to daal4py documentation or browse github repository.

1 # file: gbt_reg_dense_batch.py
2 #===============================================================================
3 # Copyright 2014-2019 Intel Corporation.
4 #
5 # This software and the related documents are Intel copyrighted materials, and
6 # your use of them is governed by the express license under which they were
7 # provided to you (License). Unless the License provides otherwise, you may not
8 # use, modify, copy, publish, distribute, disclose or transmit this software or
9 # the related documents without Intel's prior written permission.
10 #
11 # This software and the related documents are provided as is, with no express
12 # or implied warranties, other than those that are expressly stated in the
13 # License.
14 #===============================================================================
15 
16 ## <a name="DAAL-EXAMPLE-PY-GBT_REG_DENSE_BATCH"></a>
17 ## \example gbt_reg_dense_batch.py
18 
19 import os
20 import sys
21 
22 from daal.algorithms import gbt
23 from daal.algorithms.gbt.regression import prediction, training
24 from daal.data_management import (
25  FileDataSource, DataSourceIface, NumericTableIface,
26  HomogenNumericTable, MergedNumericTable, features
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 printNumericTable
33 
34 DAAL_PREFIX = os.path.join('..', 'data')
35 
36 # Input data set parameters
37 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'df_regression_train.csv')
38 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'df_regression_test.csv')
39 
40 nFeatures = 13
41 
42 # Gradient boosted trees parameters
43 maxIterations = 40
44 
45 # Model object for the gradient boosted trees regression algorithm
46 model = None
47 predictionResult = None
48 testGroundTruth = None
49 
50 
51 def trainModel():
52  global model
53 
54  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
55  trainDataSource = FileDataSource(
56  trainDatasetFileName,
57  DataSourceIface.notAllocateNumericTable,
58  DataSourceIface.doDictionaryFromContext
59  )
60 
61  # Create Numeric Tables for training data and labels
62  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
63  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
64  mergedData = MergedNumericTable(trainData, trainGroundTruth)
65 
66  # Retrieve the data from the input file
67  trainDataSource.loadDataBlock(mergedData)
68 
69  # Get the dictionary and update it with additional information about data
70  dict = trainData.getDictionary()
71 
72  # Add a feature type to the dictionary
73  dict[3].featureType = features.DAAL_CATEGORICAL
74 
75  # Create an algorithm object to train the gradient boosted trees regression model
76  algorithm = training.Batch()
77  algorithm.parameter().maxIterations = maxIterations
78 
79  # Pass the training data set and dependent values to the algorithm
80  algorithm.input.set(training.data, trainData)
81  algorithm.input.set(training.dependentVariable, trainGroundTruth)
82 
83  # Train the gradient boosted trees regression model and retrieve the results of the training algorithm
84  trainingResult = algorithm.compute()
85  model = trainingResult.get(training.model)
86 
87 def testModel():
88  global testGroundTruth, predictionResult
89 
90  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
91  testDataSource = FileDataSource(
92  testDatasetFileName,
93  DataSourceIface.notAllocateNumericTable,
94  DataSourceIface.doDictionaryFromContext
95  )
96 
97  # Create Numeric Tables for testing data and labels
98  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
99  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
100  mergedData = MergedNumericTable(testData, testGroundTruth)
101 
102  # Retrieve the data from input file
103  testDataSource.loadDataBlock(mergedData)
104 
105  # Get the dictionary and update it with additional information about data
106  dict = testData.getDictionary()
107 
108  # Add a feature type to the dictionary
109  dict[3].featureType = features.DAAL_CATEGORICAL
110 
111  # Create algorithm objects for the gradient boosted trees regression prediction with the default method
112  algorithm = prediction.Batch()
113 
114  # Pass the testing data set and trained model to the algorithm
115  algorithm.input.setTable(prediction.data, testData)
116  algorithm.input.set(prediction.model, model)
117 
118  # Compute prediction results and retrieve algorithm results
119  predictionResult = algorithm.compute()
120 
121 
122 def printResults():
123 
124  printNumericTable(
125  predictionResult.get(prediction.prediction),
126  "Gradient boosted trees prediction results (first 10 rows):", 10
127  )
128  printNumericTable(
129  testGroundTruth,
130  "Ground truth (first 10 rows):", 10
131  )
132 
133 if __name__ == "__main__":
134 
135  trainModel()
136  testModel()
137  printResults()

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