Python* API Reference for Intel® Data Analytics Acceleration Library 2018 Update 3

logitboost_dense_batch.py

1 # file: logitboost_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 ## <a name="DAAL-EXAMPLE-PY-LOGITBOOST_BATCH"></a>
17 ## \example logitboost_dense_batch.py
18 
19 import os
20 import sys
21 
22 from daal.algorithms.logitboost 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', 'logitboost_train.csv')
37 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'logitboost_test.csv')
38 
39 nFeatures = 20
40 nClasses = 5
41 
42 # LogitBoost algorithm parameters
43 maxIterations = 100 # Maximum number of terms in additive regression
44 accuracyThreshold = 0.01 # Training accuracy
45 
46 # Model object for the LogitBoost algorithm
47 model = None
48 predictionResult = None
49 testGroundTruth = None
50 
51 
52 def trainModel():
53  global model
54 
55  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
56  trainDataSource = FileDataSource(
57  trainDatasetFileName,
58  DataSourceIface.notAllocateNumericTable,
59  DataSourceIface.doDictionaryFromContext
60  )
61 
62  # Create Numeric Tables for training data and labels
63  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
64  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
65  mergedData = MergedNumericTable(trainData, trainGroundTruth)
66 
67  # Retrieve the data from the input file
68  trainDataSource.loadDataBlock(mergedData)
69 
70  # Create an algorithm object to train the LogitBoost model
71  algorithm = training.Batch(nClasses)
72  algorithm.parameter.maxIterations = maxIterations
73  algorithm.parameter.accuracyThreshold = accuracyThreshold
74 
75  # Pass the training data set and dependent values to the algorithm
76  algorithm.input.set(classifier.training.data, trainData)
77  algorithm.input.set(classifier.training.labels, trainGroundTruth)
78 
79  # Train the LogitBoost model and retrieve the results of the training algorithm
80  trainingResult = algorithm.compute()
81  model = trainingResult.get(classifier.training.model)
82 
83 
84 def testModel():
85  global testGroundTruth, predictionResult
86 
87  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
88  testDataSource = FileDataSource(
89  testDatasetFileName,
90  DataSourceIface.notAllocateNumericTable,
91  DataSourceIface.doDictionaryFromContext
92  )
93 
94  # Create Numeric Tables for testing data and labels
95  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
96  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
97  mergedData = MergedNumericTable(testData, testGroundTruth)
98 
99  # Retrieve the data from input file
100  testDataSource.loadDataBlock(mergedData)
101 
102  # Create algorithm objects for LogitBoost prediction with the default method
103  algorithm = prediction.Batch(nClasses)
104 
105  # Pass the testing data set and trained model to the algorithm
106  algorithm.input.setTable(classifier.prediction.data, testData)
107  algorithm.input.setModel(classifier.prediction.model, model)
108 
109  # Compute prediction results and retrieve algorithm results
110  # (Result class from classifier.prediction)
111  predictionResult = algorithm.compute()
112 
113 
114 def printResults():
115 
116  printNumericTables(
117  testGroundTruth,
118  predictionResult.get(classifier.prediction.prediction),
119  "Ground truth", "Classification results",
120  "LogitBoost classification results (first 20 observations):", 20, flt64=False
121  )
122 
123 if __name__ == "__main__":
124 
125  trainModel()
126  testModel()
127  printResults()

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