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

adaboost_dense_batch.py

1 # file: adaboost_dense_batch.py
2 #===============================================================================
3 # Copyright 2014-2018 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.adaboost import prediction, training
23 from daal.algorithms import classifier
24 from daal.data_management import (
25  FileDataSource, DataSourceIface, HomogenNumericTable, MergedNumericTable, NumericTableIface
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', 'adaboost_train.csv')
37 
38 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'adaboost_test.csv')
39 
40 nFeatures = 20
41 
42 trainingResult = None
43 predictionResult = None
44 testGroundTruth = None
45 
46 
47 def trainModel():
48  global trainingResult
49 
50  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
51  trainDataSource = FileDataSource(
52  trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
53  DataSourceIface.doDictionaryFromContext
54  )
55 
56  # Create Numeric Tables for training data and labels
57  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
58  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
59  mergedData = MergedNumericTable(trainData, trainGroundTruth)
60 
61  # Retrieve the data from the input file
62  trainDataSource.loadDataBlock(mergedData)
63 
64  # Create an algorithm object to train the AdaBoost model
65  algorithm = training.Batch()
66 
67  # Pass the training data set and dependent values to the algorithm
68  algorithm.input.set(classifier.training.data, trainData)
69  algorithm.input.set(classifier.training.labels, trainGroundTruth)
70 
71  # Train the AdaBoost model and retrieve the results of the training algorithm
72  trainingResult = algorithm.compute()
73 
74 
75 def testModel():
76  global predictionResult, testGroundTruth
77 
78  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
79  testDataSource = FileDataSource(
80  testDatasetFileName, 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 AdaBoost 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 def printResults():
104  printNumericTables(
105  testGroundTruth, predictionResult.get(classifier.prediction.prediction),
106  "Ground truth", "Classification results",
107  "AdaBoost classification results (first 20 observations):", 20, flt64=False
108  )
109 
110 if __name__ == "__main__":
111 
112  trainModel()
113  testModel()
114  printResults()

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