Python* API Reference for Intel® Data Analytics Acceleration Library 2018 Update 2

mn_naive_bayes_csr_batch.py

1 # file: mn_naive_bayes_csr_batch.py
2 #===============================================================================
3 # Copyright 2014-2018 Intel Corporation
4 # All Rights Reserved.
5 #
6 # If this software was obtained under the Intel Simplified Software License,
7 # the following terms apply:
8 #
9 # The source code, information and material ("Material") contained herein is
10 # owned by Intel Corporation or its suppliers or licensors, and title to such
11 # Material remains with Intel Corporation or its suppliers or licensors. The
12 # Material contains proprietary information of Intel or its suppliers and
13 # licensors. The Material is protected by worldwide copyright laws and treaty
14 # provisions. No part of the Material may be used, copied, reproduced,
15 # modified, published, uploaded, posted, transmitted, distributed or disclosed
16 # in any way without Intel's prior express written permission. No license under
17 # any patent, copyright or other intellectual property rights in the Material
18 # is granted to or conferred upon you, either expressly, by implication,
19 # inducement, estoppel or otherwise. Any license under such intellectual
20 # property rights must be express and approved by Intel in writing.
21 #
22 # Unless otherwise agreed by Intel in writing, you may not remove or alter this
23 # notice or any other notice embedded in Materials by Intel or Intel's
24 # suppliers or licensors in any way.
25 #
26 #
27 # If this software was obtained under the Apache License, Version 2.0 (the
28 # "License"), the following terms apply:
29 #
30 # You may not use this file except in compliance with the License. You may
31 # obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
32 #
33 #
34 # Unless required by applicable law or agreed to in writing, software
35 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 #
38 # See the License for the specific language governing permissions and
39 # limitations under the License.
40 #===============================================================================
41 
42 
43 
44 
45 import os
46 import sys
47 
48 from daal.algorithms.multinomial_naive_bayes import prediction, training
49 from daal.algorithms import classifier
50 from daal.data_management import FileDataSource, DataSourceIface
51 
52 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
53 if utils_folder not in sys.path:
54  sys.path.insert(0, utils_folder)
55 from utils import printNumericTables, createSparseTable
56 
57 DAAL_PREFIX = os.path.join('..', 'data')
58 
59 # Input data set parameters
60 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_csr.csv')
61 trainGroundTruthFileName = os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_train_labels.csv')
62 
63 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_test_csr.csv')
64 testGroundTruthFileName = os.path.join(DAAL_PREFIX, 'batch', 'naivebayes_test_labels.csv')
65 
66 nTrainObservations = 8000
67 nTestObservations = 2000
68 nClasses = 20
69 
70 trainingResult = None
71 predictionResult = None
72 
73 
74 def trainModel():
75  global trainingResult
76 
77  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
78  trainGroundTruthSource = FileDataSource(
79  trainGroundTruthFileName,
80  DataSourceIface.doAllocateNumericTable,
81  DataSourceIface.doDictionaryFromContext
82  )
83 
84  # Retrieve the data from input files
85  trainData = createSparseTable(trainDatasetFileName)
86  trainGroundTruthSource.loadDataBlock(nTrainObservations)
87 
88  # Create an algorithm object to train the Naive Bayes model
89  algorithm = training.Batch(nClasses, method=training.fastCSR)
90 
91  # Pass a training data set and dependent values to the algorithm
92  algorithm.input.set(classifier.training.data, trainData)
93  algorithm.input.set(classifier.training.labels, trainGroundTruthSource.getNumericTable())
94 
95  # Build the Naive Bayes model and retrieve the algorithm results
96  trainingResult = algorithm.compute()
97 
98 
99 def testModel():
100  global predictionResult
101 
102  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
103  testData = createSparseTable(testDatasetFileName)
104 
105  # Create an algorithm object to predict Naive Bayes values
106  algorithm = prediction.Batch(nClasses, method=prediction.fastCSR)
107 
108  # Pass a testing data set and the trained model to the algorithm
109  algorithm.input.setTable(classifier.prediction.data, testData)
110  algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
111 
112  # Predict Naive Bayes values and retrieve the algorithm results (Result class from classifier.prediction)
113  predictionResult = algorithm.compute()
114 
115 
116 def printResults():
117 
118  testGroundTruth = FileDataSource(
119  testGroundTruthFileName,
120  DataSourceIface.doAllocateNumericTable,
121  DataSourceIface.doDictionaryFromContext
122  )
123 
124  testGroundTruth.loadDataBlock(nTestObservations)
125 
126  printNumericTables(
127  testGroundTruth.getNumericTable(),
128  predictionResult.get(classifier.prediction.prediction),
129  "Ground truth", "Classification results",
130  "NaiveBayes classification results (first 20 observations):", 20, 15, flt64=False
131  )
132 
133 if __name__ == "__main__":
134 
135  trainModel()
136  testModel()
137  printResults()

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