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

stump_dense_batch.py

1 # file: stump_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 import classifier
25 from daal.algorithms.stump import training, prediction
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 nFeatures = 20
39 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'stump_train.csv')
40 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'stump_test.csv')
41 
42 trainingResult = None
43 predictionResult = None
44 testGroundTruth = None
45 
46 
47 def trainModel():
48  global trainingResult
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 
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 stump model
65  algorithm = training.Batch()
66 
67  # Pass a 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  # Compute and retrieve the algorithm results
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,
81  DataSourceIface.notAllocateNumericTable,
82  DataSourceIface.doDictionaryFromContext
83  )
84 
85  # Create Numeric Tables for training data and labels
86  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
87  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
88  mergedData = MergedNumericTable(testData, testGroundTruth)
89 
90  # Retrieve the data from the input file
91  testDataSource.loadDataBlock(mergedData)
92 
93  # Create an algorithm object to train the stump model
94  algorithm = prediction.Batch()
95 
96  # Pass a training data set and dependent values to the algorithm
97  algorithm.input.setTable(classifier.prediction.data, testData)
98  algorithm.input.setModel(classifier.prediction.model,
99  trainingResult.get(classifier.training.model))
100 
101  # Compute and retrieve the algorithm Result class from classifier.prediction
102  predictionResult = algorithm.compute()
103 
104 
105 def printResults():
106  printNumericTables(
107  testGroundTruth,
108  predictionResult.get(classifier.prediction.prediction),
109  "Ground truth", "Classification results",
110  "Stump classification results (first 20 observations):", 20, flt64=False)
111 
112 if __name__ == "__main__":
113  trainModel()
114  testModel()
115  printResults()

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