47 from daal.algorithms.multinomial_naive_bayes
import prediction, training
48 from daal.algorithms
import classifier
49 from daal.data_management
import (
50 FileDataSource, DataSourceIface, HomogenNumericTable, MergedNumericTable, NumericTableIface
53 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
54 if utils_folder
not in sys.path:
55 sys.path.insert(0, utils_folder)
56 from utils
import printNumericTables
58 DAAL_PREFIX = os.path.join(
'..',
'data')
61 trainDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'naivebayes_train_dense.csv')
62 testDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'naivebayes_test_dense.csv')
65 nTrainVectorsInBlock = 2000
69 predictionResult =
None
70 testGroundTruth =
None
77 trainDataSource = FileDataSource(
78 trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
79 DataSourceIface.doDictionaryFromContext
83 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
84 trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
85 mergedData = MergedNumericTable(trainData, trainGroundTruth)
88 algorithm = training.Online(nClasses)
90 while(trainDataSource.loadDataBlock(nTrainVectorsInBlock, mergedData) == nTrainVectorsInBlock):
92 algorithm.input.set(classifier.training.data, trainData)
93 algorithm.input.set(classifier.training.labels, trainGroundTruth)
99 trainingResult = algorithm.finalizeCompute()
103 global predictionResult, testGroundTruth
106 testDataSource = FileDataSource(
107 testDatasetFileName, DataSourceIface.notAllocateNumericTable,
108 DataSourceIface.doDictionaryFromContext
112 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
113 testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
114 mergedData = MergedNumericTable(testData, testGroundTruth)
117 testDataSource.loadDataBlock(mergedData)
120 algorithm = prediction.Batch(nClasses)
123 algorithm.input.setTable(classifier.prediction.data, testData)
124 algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
127 predictionResult = algorithm.compute()
133 testGroundTruth, predictionResult.get(classifier.prediction.prediction),
134 "Ground truth",
"Classification results",
135 "NaiveBayes classification results (first 20 observations):", 20, flt64=
False
138 if __name__ ==
"__main__":