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

brownboost_dense_batch.py

1 # file: brownboost_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.brownboost import prediction, training
25 from daal.algorithms import classifier
26 from daal.data_management import (
27  FileDataSource, DataSourceIface, NumericTableIface, HomogenNumericTable, MergedNumericTable
28 )
29 
30 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
31 if utils_folder not in sys.path:
32  sys.path.insert(0, utils_folder)
33 from utils import printNumericTables
34 
35 DAAL_PREFIX = os.path.join('..', 'data')
36 
37 # Input data set parameters
38 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'brownboost_train.csv')
39 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'brownboost_test.csv')
40 
41 nFeatures = 20
42 
43 trainingResult = None
44 predictionResult = None
45 testGroundTruth = None
46 
47 
48 def trainModel():
49  global trainingResult
50 
51  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
52  trainDataSource = FileDataSource(
53  trainDatasetFileName,
54  DataSourceIface.notAllocateNumericTable,
55  DataSourceIface.doDictionaryFromContext
56  )
57  # Create Numeric Tables for training data and labels
58  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
59  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
60  mergedData = MergedNumericTable(trainData, trainGroundTruth)
61 
62  # Retrieve the data from the input file
63  trainDataSource.loadDataBlock(mergedData)
64 
65  # Create an algorithm object to train the BrownBoost model
66  algorithm = training.Batch()
67 
68  # Pass the training data set and dependent values to the algorithm
69  algorithm.input.set(classifier.training.data, trainData)
70  algorithm.input.set(classifier.training.labels, trainGroundTruth)
71 
72  # Train the BrownBoost model and retrieve the results of the training algorithm
73  trainingResult = algorithm.compute()
74 
75 
76 def testModel():
77  global testGroundTruth, predictionResult
78 
79  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
80  testDataSource = FileDataSource(
81  testDatasetFileName,
82  DataSourceIface.notAllocateNumericTable,
83  DataSourceIface.doDictionaryFromContext
84  )
85 
86  # Create Numeric Tables for testing data and labels
87  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
88  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
89  mergedData = MergedNumericTable(testData, testGroundTruth)
90 
91  # Retrieve the data from input file
92  testDataSource.loadDataBlock(mergedData)
93 
94  # Create algorithm objects for BrownBoost prediction with the default method
95  algorithm = prediction.Batch()
96 
97  # Pass the testing data set and trained model to the algorithm
98  algorithm.input.setTable(classifier.prediction.data, testData)
99  algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
100 
101  # Compute prediction results and retrieve algorithm results
102  # (Result class from classifier.prediction)
103  predictionResult = algorithm.compute()
104 
105 
106 def printResults():
107  printNumericTables(
108  testGroundTruth,
109  predictionResult.get(classifier.prediction.prediction),
110  "Ground truth", "Classification results",
111  "BrownBoost classification results (first 20 observations):", 20
112  )
113 
114 if __name__ == "__main__":
115 
116  trainModel()
117  testModel()
118  printResults()

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