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

mn_naive_bayes_dense_batch.py

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

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