22 from daal.algorithms.multinomial_naive_bayes
import prediction, training
23 from daal.algorithms
import classifier
24 from daal.data_management
import (
25 FileDataSource, DataSourceIface, HomogenNumericTable, MergedNumericTable, NumericTableIface
28 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
29 if utils_folder
not in sys.path:
30 sys.path.insert(0, utils_folder)
31 from utils
import printNumericTables
33 DAAL_PREFIX = os.path.join(
'..',
'data')
36 trainDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'naivebayes_train_dense.csv')
37 testDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'naivebayes_test_dense.csv')
40 nTrainVectorsInBlock = 2000
44 predictionResult =
None
45 testGroundTruth =
None
52 trainDataSource = FileDataSource(
53 trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
54 DataSourceIface.doDictionaryFromContext
58 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
59 trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
60 mergedData = MergedNumericTable(trainData, trainGroundTruth)
63 algorithm = training.Online(nClasses)
65 while(trainDataSource.loadDataBlock(nTrainVectorsInBlock, mergedData) == nTrainVectorsInBlock):
67 algorithm.input.set(classifier.training.data, trainData)
68 algorithm.input.set(classifier.training.labels, trainGroundTruth)
74 trainingResult = algorithm.finalizeCompute()
78 global predictionResult, testGroundTruth
81 testDataSource = FileDataSource(
82 testDatasetFileName, DataSourceIface.notAllocateNumericTable,
83 DataSourceIface.doDictionaryFromContext
87 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
88 testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
89 mergedData = MergedNumericTable(testData, testGroundTruth)
92 testDataSource.loadDataBlock(mergedData)
95 algorithm = prediction.Batch(nClasses)
98 algorithm.input.setTable(classifier.prediction.data, testData)
99 algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
102 predictionResult = algorithm.compute()
108 testGroundTruth, predictionResult.get(classifier.prediction.prediction),
109 "Ground truth",
"Classification results",
110 "NaiveBayes classification results (first 20 observations):", 20, flt64=
False
113 if __name__ ==
"__main__":