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

mn_naive_bayes_csr_online.py

1 # file: mn_naive_bayes_csr_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 FileDataSource, DataSourceIface
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, createSparseTable
32 
33 DAAL_PREFIX = os.path.join('..', 'data')
34 
35 # Input data set parameters
36 trainDatasetFileNames = [
37  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_csr.csv'),
38  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_csr.csv'),
39  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_csr.csv'),
40  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_csr.csv')
41 ]
42 
43 trainGroundTruthFileNames = [
44  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_labels.csv'),
45  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_labels.csv'),
46  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_labels.csv'),
47  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_labels.csv')
48 ]
49 
50 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_test_csr.csv')
51 testGroundTruthFileName = os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_test_labels.csv')
52 
53 nTrainVectorsInBlock = 8000
54 nTestObservations = 2000
55 nClasses = 20
56 nBlocks = 4
57 
58 trainingResult = None
59 predictionResult = None
60 trainData = [0] * nBlocks
61 testData = None
62 
63 
64 def trainModel():
65  global trainData, trainingResult
66 
67  # Create an algorithm object to train the Naive Bayes model
68  algorithm = training.Online(nClasses, method=training.fastCSR)
69 
70  for i in range(nBlocks):
71  # Read trainDatasetFileNames and create a numeric table to store the input data
72  trainData[i] = createSparseTable(trainDatasetFileNames[i])
73  trainLabelsSource = FileDataSource(
74  trainGroundTruthFileNames[i], DataSourceIface.doAllocateNumericTable,
75  DataSourceIface.doDictionaryFromContext
76  )
77 
78  trainLabelsSource.loadDataBlock(nTrainVectorsInBlock)
79 
80  # Pass a training data set and dependent values to the algorithm
81  algorithm.input.set(classifier.training.data, trainData[i])
82  algorithm.input.set(classifier.training.labels, trainLabelsSource.getNumericTable())
83 
84  # Build the Naive Bayes model
85  algorithm.compute()
86 
87  # Finalize the Naive Bayes model and retrieve the algorithm results
88  trainingResult = algorithm.finalizeCompute()
89 
90 
91 def testModel():
92  global predictionResult, testData
93 
94  # Read testDatasetFileName and create a numeric table to store the input data
95  testData = createSparseTable(testDatasetFileName)
96 
97  # Create an algorithm object to predict Naive Bayes values
98  algorithm = prediction.Batch(nClasses, method=prediction.fastCSR)
99 
100  # Pass a testing data set and the trained model to the algorithm
101  algorithm.input.setTable(classifier.prediction.data, testData)
102  algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
103 
104  # Predict Naive Bayes values (Result class from classifier.prediction)
105  predictionResult = algorithm.compute() # Retrieve the algorithm results
106 
107 
108 def printResults():
109 
110  testGroundTruth = FileDataSource(
111  testGroundTruthFileName, DataSourceIface.doAllocateNumericTable,
112  DataSourceIface.doDictionaryFromContext
113  )
114  testGroundTruth.loadDataBlock(nTestObservations)
115 
116  printNumericTables(
117  testGroundTruth.getNumericTable(),
118  predictionResult.get(classifier.prediction.prediction),
119  "Ground truth", "Classification results",
120  "NaiveBayes classification results (first 20 observations):", 20, 15, 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.