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

mn_naive_bayes_csr_batch.py

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

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