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

mn_naive_bayes_dense_distr.py

Deprecation Notice: With the introduction of daal4py, a package that supersedes PyDAAL, Intel is deprecating PyDAAL and will discontinue support starting with Intel® DAAL 2021 and Intel® Distribution for Python 2021. Until then Intel will continue to provide compatible pyDAAL pip and conda packages for newer releases of Intel DAAL and make it available in open source. However, Intel will not add the new features of Intel DAAL to pyDAAL. Intel recommends developers switch to and use daal4py.

Note: To find daal4py examples, refer to daal4py documentation or browse github repository.

1 # file: mn_naive_bayes_dense_distr.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 ## <a name="DAAL-EXAMPLE-PY-MULTINOMIAL_NAIVE_BAYES_DENSE_DISTRIBUTED"></a>
17 ## \example mn_naive_bayes_dense_distr.py
18 
19 import os
20 import sys
21 
22 from daal import step1Local, step2Master
23 from daal.algorithms.multinomial_naive_bayes import prediction, training
24 from daal.algorithms import classifier
25 from daal.data_management import (
26  FileDataSource, DataSourceIface, NumericTableIface, HomogenNumericTable, MergedNumericTable
27 )
28 
29 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
30 if utils_folder not in sys.path:
31  sys.path.insert(0, utils_folder)
32 from utils import printNumericTables
33 
34 DAAL_PREFIX = os.path.join('..', 'data')
35 
36 # Input data set parameters
37 trainDatasetFileNames = [
38  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_dense.csv'),
39  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_dense.csv'),
40  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_dense.csv'),
41  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_dense.csv')
42 ]
43 
44 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_test_dense.csv')
45 
46 nFeatures = 20
47 nClasses = 20
48 nBlocks = 4
49 
50 trainingResult = None
51 predictionResult = None
52 testGroundTruth = None
53 
54 
55 def trainModel():
56  global trainingResult
57 
58  masterAlgorithm = training.Distributed(step2Master, nClasses)
59 
60  for i in range(nBlocks):
61  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
62  trainDataSource = FileDataSource(
63  trainDatasetFileNames[i], DataSourceIface.notAllocateNumericTable,
64  DataSourceIface.doDictionaryFromContext
65  )
66  # Create Numeric Tables for training data and labels
67  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
68  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
69  mergedData = MergedNumericTable(trainData, trainGroundTruth)
70 
71  # Retrieve the data from the input file
72  trainDataSource.loadDataBlock(mergedData)
73 
74  # Create an algorithm object to train the Naive Bayes model on the local-node data
75  localAlgorithm = training.Distributed(step1Local, nClasses)
76 
77  # Pass a training data set and dependent values to the algorithm
78  localAlgorithm.input.set(classifier.training.data, trainData)
79  localAlgorithm.input.set(classifier.training.labels, trainGroundTruth)
80 
81  # Build the Naive Bayes model on the local node and
82  # Set the local Naive Bayes model as input for the master-node algorithm
83  masterAlgorithm.input.add(training.partialModels, localAlgorithm.compute())
84 
85  # Merge and finalize the Naive Bayes model on the master node
86  masterAlgorithm.compute()
87  trainingResult = masterAlgorithm.finalizeCompute() # Retrieve the algorithm results
88 
89 
90 def testModel():
91  global predictionResult, testGroundTruth
92 
93  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
94  testDataSource = FileDataSource(
95  testDatasetFileName, DataSourceIface.notAllocateNumericTable,
96  DataSourceIface.doDictionaryFromContext
97  )
98 
99  # Create Numeric Tables for testing data and labels
100  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
101  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
102  mergedData = MergedNumericTable(testData, testGroundTruth)
103 
104  # Retrieve the data from input file
105  testDataSource.loadDataBlock(mergedData)
106 
107  # Create an algorithm object to predict Naive Bayes values
108  algorithm = prediction.Batch(nClasses)
109 
110  # Pass a testing data set and the trained model to the algorithm
111  algorithm.input.setTable(classifier.prediction.data, testData)
112  algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
113 
114  # Predict Naive Bayes values (Result class from classifier.prediction)
115  predictionResult = algorithm.compute() # Retrieve the algorithm results
116 
117 
118 def printResults():
119  printNumericTables(
120  testGroundTruth, predictionResult.get(classifier.prediction.prediction),
121  "Ground truth", "Classification results",
122  "NaiveBayes classification results (first 20 observations):", 20, flt64=False
123  )
124 
125 if __name__ == "__main__":
126 
127  trainModel()
128  testModel()
129  printResults()

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