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

mn_naive_bayes_csr_batch.py

1 # file: mn_naive_bayes_csr_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.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 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_csr.csv')
37 trainGroundTruthFileName = os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_labels.csv')
38 
39 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_test_csr.csv')
40 testGroundTruthFileName = os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_test_labels.csv')
41 
42 nTrainObservations = 8000
43 nTestObservations = 2000
44 nClasses = 20
45 
46 trainingResult = None
47 predictionResult = None
48 
49 
50 def trainModel():
51  global trainingResult
52 
53  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
54  trainGroundTruthSource = FileDataSource(
55  trainGroundTruthFileName,
56  DataSourceIface.doAllocateNumericTable,
57  DataSourceIface.doDictionaryFromContext
58  )
59 
60  # Retrieve the data from input files
61  trainData = createSparseTable(trainDatasetFileName)
62  trainGroundTruthSource.loadDataBlock(nTrainObservations)
63 
64  # Create an algorithm object to train the Naive Bayes model
65  algorithm = training.Batch(nClasses, method=training.fastCSR)
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, trainGroundTruthSource.getNumericTable())
70 
71  # Build the Naive Bayes model and retrieve the algorithm results
72  trainingResult = algorithm.compute()
73 
74 
75 def testModel():
76  global predictionResult
77 
78  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
79  testData = createSparseTable(testDatasetFileName)
80 
81  # Create an algorithm object to predict Naive Bayes values
82  algorithm = prediction.Batch(nClasses, method=prediction.fastCSR)
83 
84  # Pass a testing data set and the trained model to the algorithm
85  algorithm.input.setTable(classifier.prediction.data, testData)
86  algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
87 
88  # Predict Naive Bayes values and retrieve the algorithm results (Result class from classifier.prediction)
89  predictionResult = algorithm.compute()
90 
91 
92 def printResults():
93 
94  testGroundTruth = FileDataSource(
95  testGroundTruthFileName,
96  DataSourceIface.doAllocateNumericTable,
97  DataSourceIface.doDictionaryFromContext
98  )
99 
100  testGroundTruth.loadDataBlock(nTestObservations)
101 
102  printNumericTables(
103  testGroundTruth.getNumericTable(),
104  predictionResult.get(classifier.prediction.prediction),
105  "Ground truth", "Classification results",
106  "NaiveBayes classification results (first 20 observations):", 20, 15, flt64=False
107  )
108 
109 if __name__ == "__main__":
110 
111  trainModel()
112  testModel()
113  printResults()

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