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

mn_naive_bayes_dense_online.py

1 # file: mn_naive_bayes_dense_online.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.multinomial_naive_bayes 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', 'naivebayes_train_dense.csv')
39 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_test_dense.csv')
40 
41 nFeatures = 20
42 nTrainVectorsInBlock = 2000
43 nClasses = 20
44 
45 trainingResult = None
46 predictionResult = None
47 testGroundTruth = None
48 
49 
50 def trainModel():
51  global trainingResult
52 
53  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
54  trainDataSource = FileDataSource(
55  trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
56  DataSourceIface.doDictionaryFromContext
57  )
58 
59  # Create Numeric Tables for training data and labels
60  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
61  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
62  mergedData = MergedNumericTable(trainData, trainGroundTruth)
63 
64  # Create an algorithm object to train the Naive Bayes model
65  algorithm = training.Online(nClasses)
66 
67  while(trainDataSource.loadDataBlock(nTrainVectorsInBlock, mergedData) == nTrainVectorsInBlock):
68  # Pass a training data set and dependent values to the algorithm
69  algorithm.input.set(classifier.training.data, trainData)
70  algorithm.input.set(classifier.training.labels, trainGroundTruth)
71 
72  # Build the Naive Bayes model
73  algorithm.compute()
74 
75  # Finalize the Naive Bayes model
76  trainingResult = algorithm.finalizeCompute() # Retrieve the algorithm results
77 
78 
79 def testModel():
80  global predictionResult, testGroundTruth
81 
82  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
83  testDataSource = FileDataSource(
84  testDatasetFileName, DataSourceIface.notAllocateNumericTable,
85  DataSourceIface.doDictionaryFromContext
86  )
87 
88  # Create Numeric Tables for testing data and labels
89  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
90  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
91  mergedData = MergedNumericTable(testData, testGroundTruth)
92 
93  # Retrieve the data from input file
94  testDataSource.loadDataBlock(mergedData)
95 
96  # Create an algorithm object to predict Naive Bayes values
97  algorithm = prediction.Batch(nClasses)
98 
99  # Pass a testing data set and the trained model to the algorithm
100  algorithm.input.setTable(classifier.prediction.data, testData)
101  algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
102 
103  # Predict Naive Bayes values (Result class from classifier.prediction)
104  predictionResult = algorithm.compute() # Retrieve the algorithm results
105 
106 
107 def printResults():
108 
109  printNumericTables(
110  testGroundTruth, predictionResult.get(classifier.prediction.prediction),
111  "Ground truth", "Classification results",
112  "NaiveBayes classification results (first 20 observations):", 20, flt64=False
113  )
114 
115 if __name__ == "__main__":
116 
117  trainModel()
118  testModel()
119  printResults()

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