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

logitboost_dense_batch.py

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

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