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

adaboost_dense_batch.py

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

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