Python* API Reference for Intel® Data Analytics Acceleration Library 2019 Update 5

mn_naive_bayes_dense_batch.py

1 # file: mn_naive_bayes_dense_batch.py
2 #===============================================================================
3 # Copyright 2014-2019 Intel Corporation.
4 #
5 # This software and the related documents are Intel copyrighted materials, and
6 # your use of them is governed by the express license under which they were
7 # provided to you (License). Unless the License provides otherwise, you may not
8 # use, modify, copy, publish, distribute, disclose or transmit this software or
9 # the related documents without Intel's prior written permission.
10 #
11 # This software and the related documents are provided as is, with no express
12 # or implied warranties, other than those that are expressly stated in the
13 # License.
14 #===============================================================================
15 
16 
17 
18 
19 import os
20 import sys
21 
22 from daal.algorithms.multinomial_naive_bayes import prediction, training
23 from daal.algorithms import classifier
24 from daal.data_management import (
25  FileDataSource, HomogenNumericTable, MergedNumericTable, DataSourceIface, NumericTableIface
26 )
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
32 
33 DAAL_PREFIX = os.path.join('..', 'data')
34 
35 # Input data set parameters
36 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_dense.csv')
37 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_test_dense.csv')
38 
39 nFeatures = 20
40 nClasses = 20
41 
42 trainingResult = None
43 predictionResult = None
44 testGroundTruth = None
45 
46 
47 def trainModel():
48  global trainingResult
49 
50  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
51  trainDataSource = FileDataSource(
52  trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
53  DataSourceIface.doDictionaryFromContext
54  )
55 
56  # Create Numeric Tables for training data and labels
57  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
58  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
59  mergedData = MergedNumericTable(trainData, trainGroundTruth)
60 
61  # Retrieve the data from the input file
62  trainDataSource.loadDataBlock(mergedData)
63 
64  # Create an algorithm object to train the Naive Bayes model
65  algorithm = training.Batch(nClasses)
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, trainGroundTruth)
70 
71  # Build the Naive Bayes model and retrieve the algorithm results
72  trainingResult = algorithm.compute()
73 
74 
75 def testModel():
76  global predictionResult, testGroundTruth
77 
78  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
79  testDataSource = FileDataSource(
80  testDatasetFileName, DataSourceIface.notAllocateNumericTable,
81  DataSourceIface.doDictionaryFromContext
82  )
83 
84  # Create Numeric Tables for testing data and labels
85  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
86  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
87  mergedData = MergedNumericTable(testData, testGroundTruth)
88 
89  # Retrieve the data from input file
90  testDataSource.loadDataBlock(mergedData)
91 
92  # Create an algorithm object to predict Naive Bayes values
93  algorithm = prediction.Batch(nClasses)
94 
95  # Pass a testing data set and the trained model to the algorithm
96  algorithm.input.setTable(classifier.prediction.data, testData)
97  algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
98 
99  # Predict Naive Bayes values (Result class from classifier.prediction)
100  predictionResult = algorithm.compute() # Retrieve the algorithm results
101 
102 def printResults():
103  printNumericTables(
104  testGroundTruth, predictionResult.get(classifier.prediction.prediction),
105  "Ground truth", "Classification results",
106  "NaiveBayes classification results (first 20 observations):", 20, 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.