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

brownboost_dense_batch.py

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

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