24 from daal.algorithms.multinomial_naive_bayes
import prediction, training
25 from daal.algorithms
import classifier
26 from daal.data_management
import (
27 FileDataSource, DataSourceIface, HomogenNumericTable, MergedNumericTable, NumericTableIface
30 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
31 if utils_folder
not in sys.path:
32 sys.path.insert(0, utils_folder)
33 from utils
import printNumericTables
35 DAAL_PREFIX = os.path.join(
'..',
'data')
38 trainDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'naivebayes_train_dense.csv')
39 testDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'naivebayes_test_dense.csv')
42 nTrainVectorsInBlock = 2000
46 predictionResult =
None
47 testGroundTruth =
None
54 trainDataSource = FileDataSource(
55 trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
56 DataSourceIface.doDictionaryFromContext
60 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
61 trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
62 mergedData = MergedNumericTable(trainData, trainGroundTruth)
65 algorithm = training.Online(nClasses)
67 while(trainDataSource.loadDataBlock(nTrainVectorsInBlock, mergedData) == nTrainVectorsInBlock):
69 algorithm.input.set(classifier.training.data, trainData)
70 algorithm.input.set(classifier.training.labels, trainGroundTruth)
76 trainingResult = algorithm.finalizeCompute()
80 global predictionResult, testGroundTruth
83 testDataSource = FileDataSource(
84 testDatasetFileName, DataSourceIface.notAllocateNumericTable,
85 DataSourceIface.doDictionaryFromContext
89 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
90 testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
91 mergedData = MergedNumericTable(testData, testGroundTruth)
94 testDataSource.loadDataBlock(mergedData)
97 algorithm = prediction.Batch(nClasses)
100 algorithm.input.setTable(classifier.prediction.data, testData)
101 algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
104 predictionResult = algorithm.compute()
110 testGroundTruth, predictionResult.get(classifier.prediction.prediction),
111 "Ground truth",
"Classification results",
112 "NaiveBayes classification results (first 20 observations):", 20, flt64=
False
115 if __name__ ==
"__main__":