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

mn_naive_bayes_dense_distr.py

1 # file: mn_naive_bayes_dense_distr.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 import step1Local, step2Master
25 from daal.algorithms.multinomial_naive_bayes import prediction, training
26 from daal.algorithms import classifier
27 from daal.data_management import (
28  FileDataSource, DataSourceIface, NumericTableIface, HomogenNumericTable, MergedNumericTable
29 )
30 
31 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
32 if utils_folder not in sys.path:
33  sys.path.insert(0, utils_folder)
34 from utils import printNumericTables
35 
36 DAAL_PREFIX = os.path.join('..', 'data')
37 
38 # Input data set parameters
39 trainDatasetFileNames = [
40  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_dense.csv'),
41  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_dense.csv'),
42  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_dense.csv'),
43  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_dense.csv')
44 ]
45 
46 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_test_dense.csv')
47 
48 nFeatures = 20
49 nClasses = 20
50 nBlocks = 4
51 
52 trainingResult = None
53 predictionResult = None
54 testGroundTruth = None
55 
56 
57 def trainModel():
58  global trainingResult
59 
60  masterAlgorithm = training.Distributed(step2Master, nClasses)
61 
62  for i in range(nBlocks):
63  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
64  trainDataSource = FileDataSource(
65  trainDatasetFileNames[i], DataSourceIface.notAllocateNumericTable,
66  DataSourceIface.doDictionaryFromContext
67  )
68  # Create Numeric Tables for training data and labels
69  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
70  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
71  mergedData = MergedNumericTable(trainData, trainGroundTruth)
72 
73  # Retrieve the data from the input file
74  trainDataSource.loadDataBlock(mergedData)
75 
76  # Create an algorithm object to train the Naive Bayes model on the local-node data
77  localAlgorithm = training.Distributed(step1Local, nClasses)
78 
79  # Pass a training data set and dependent values to the algorithm
80  localAlgorithm.input.set(classifier.training.data, trainData)
81  localAlgorithm.input.set(classifier.training.labels, trainGroundTruth)
82 
83  # Build the Naive Bayes model on the local node and
84  # Set the local Naive Bayes model as input for the master-node algorithm
85  masterAlgorithm.input.add(training.partialModels, localAlgorithm.compute())
86 
87  # Merge and finalize the Naive Bayes model on the master node
88  masterAlgorithm.compute()
89  trainingResult = masterAlgorithm.finalizeCompute() # Retrieve the algorithm results
90 
91 
92 def testModel():
93  global predictionResult, testGroundTruth
94 
95  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
96  testDataSource = FileDataSource(
97  testDatasetFileName, DataSourceIface.notAllocateNumericTable,
98  DataSourceIface.doDictionaryFromContext
99  )
100 
101  # Create Numeric Tables for testing data and labels
102  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
103  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
104  mergedData = MergedNumericTable(testData, testGroundTruth)
105 
106  # Retrieve the data from input file
107  testDataSource.loadDataBlock(mergedData)
108 
109  # Create an algorithm object to predict Naive Bayes values
110  algorithm = prediction.Batch(nClasses)
111 
112  # Pass a testing data set and the trained model to the algorithm
113  algorithm.input.setTable(classifier.prediction.data, testData)
114  algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
115 
116  # Predict Naive Bayes values (Result class from classifier.prediction)
117  predictionResult = algorithm.compute() # Retrieve the algorithm results
118 
119 
120 def printResults():
121  printNumericTables(
122  testGroundTruth, predictionResult.get(classifier.prediction.prediction),
123  "Ground truth", "Classification results",
124  "NaiveBayes classification results (first 20 observations):", 20, flt64=False
125  )
126 
127 if __name__ == "__main__":
128 
129  trainModel()
130  testModel()
131  printResults()

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