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

mn_naive_bayes_csr_distr.py

1 # file: mn_naive_bayes_csr_distr.py
2 #===============================================================================
3 # Copyright 2014-2018 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_CSR_DISTRIBUTED"></a>
17 ## \example mn_naive_bayes_csr_distr.py
18 
19 import os
20 import sys
21 
22 from daal import step1Local, step2Master
23 from daal.algorithms import classifier
24 from daal.algorithms.multinomial_naive_bayes import training, prediction
25 from daal.data_management import FileDataSource, DataSourceIface
26 
27 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
28 if utils_folder not in sys.path:
29  sys.path.insert(0, utils_folder)
30 from utils import printNumericTables, createSparseTable
31 
32 DAAL_PREFIX = os.path.join('..', 'data')
33 
34 # Input data set parameters
35 trainDatasetFileNames = [
36  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_csr.csv'),
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 ]
41 
42 trainGroundTruthFileNames = [
43  os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_labels.csv'),
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 ]
48 
49 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_test_csr.csv')
50 testGroundTruthFileName = os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_test_labels.csv')
51 
52 nClasses = 20
53 nBlocks = 4
54 nTrainVectorsInBlock = 8000
55 nTestObservations = 2000
56 
57 trainingResult = None
58 predictionResult = None
59 trainData = [0] * nBlocks
60 testData = None
61 
62 
63 def trainModel():
64  global trainData, trainingResult
65 
66  masterAlgorithm = training.Distributed(step2Master, nClasses, method=training.fastCSR)
67 
68  for i in range(nBlocks):
69  # Read trainDatasetFileNames and create a numeric table to store the input data
70  trainData[i] = createSparseTable(trainDatasetFileNames[i])
71 
72  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
73  trainLabelsSource = FileDataSource(
74  trainGroundTruthFileNames[i], DataSourceIface.doAllocateNumericTable,
75  DataSourceIface.doDictionaryFromContext
76  )
77 
78  # Retrieve the data from an input file
79  trainLabelsSource.loadDataBlock(nTrainVectorsInBlock)
80 
81  # Create an algorithm object to train the Naive Bayes model on the local-node data
82  localAlgorithm = training.Distributed(step1Local, nClasses, method=training.fastCSR)
83 
84  # Pass a training data set and dependent values to the algorithm
85  localAlgorithm.input.set(classifier.training.data, trainData[i])
86  localAlgorithm.input.set(classifier.training.labels, trainLabelsSource.getNumericTable())
87 
88  # Build the Naive Bayes model on the local node
89  # Set the local Naive Bayes model as input for the master-node algorithm
90  masterAlgorithm.input.add(training.partialModels, localAlgorithm.compute())
91 
92  # Merge and finalize the Naive Bayes model on the master node
93  masterAlgorithm.compute()
94  trainingResult = masterAlgorithm.finalizeCompute() # Retrieve the algorithm results
95 
96 
97 def testModel():
98  global predictionResult, testData
99 
100  # Read testDatasetFileName and create a numeric table to store the input data
101  testData = createSparseTable(testDatasetFileName)
102 
103  # Create an algorithm object to predict Naive Bayes values
104  algorithm = prediction.Batch(nClasses, method=prediction.fastCSR)
105 
106  # Pass a testing data set and the trained model to the algorithm
107  algorithm.input.setTable(classifier.prediction.data, testData)
108  algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
109 
110  # Predict Naive Bayes values (Result class from classifier.prediction)
111  predictionResult = algorithm.compute() # Retrieve the algorithm results
112 
113 
114 def printResults():
115 
116  testGroundTruth = FileDataSource(
117  testGroundTruthFileName, DataSourceIface.doAllocateNumericTable,
118  DataSourceIface.doDictionaryFromContext
119  )
120  testGroundTruth.loadDataBlock(nTestObservations)
121 
122  printNumericTables(
123  testGroundTruth.getNumericTable(),
124  predictionResult.get(classifier.prediction.prediction),
125  "Ground truth", "Classification results",
126  "NaiveBayes classification results (first 20 observations):", 20, 15, flt64=False
127  )
128 
129 if __name__ == "__main__":
130 
131  trainModel()
132  testModel()
133  printResults()

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