48 from daal.algorithms.multinomial_naive_bayes
import prediction, training
49 from daal.algorithms
import classifier
50 from daal.data_management
import (
51 FileDataSource, DataSourceIface, HomogenNumericTable, MergedNumericTable, NumericTableIface
54 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
55 if utils_folder
not in sys.path:
56 sys.path.insert(0, utils_folder)
57 from utils
import printNumericTables
59 DAAL_PREFIX = os.path.join(
'..',
'data')
62 trainDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'naivebayes_train_dense.csv')
63 testDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'naivebayes_test_dense.csv')
66 nTrainVectorsInBlock = 2000
70 predictionResult =
None 71 testGroundTruth =
None 78 trainDataSource = FileDataSource(
79 trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
80 DataSourceIface.doDictionaryFromContext
84 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
85 trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
86 mergedData = MergedNumericTable(trainData, trainGroundTruth)
89 algorithm = training.Online(nClasses)
91 while(trainDataSource.loadDataBlock(nTrainVectorsInBlock, mergedData) == nTrainVectorsInBlock):
93 algorithm.input.set(classifier.training.data, trainData)
94 algorithm.input.set(classifier.training.labels, trainGroundTruth)
100 trainingResult = algorithm.finalizeCompute()
104 global predictionResult, testGroundTruth
107 testDataSource = FileDataSource(
108 testDatasetFileName, DataSourceIface.notAllocateNumericTable,
109 DataSourceIface.doDictionaryFromContext
113 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
114 testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
115 mergedData = MergedNumericTable(testData, testGroundTruth)
118 testDataSource.loadDataBlock(mergedData)
121 algorithm = prediction.Batch(nClasses)
124 algorithm.input.setTable(classifier.prediction.data, testData)
125 algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
128 predictionResult = algorithm.compute()
134 testGroundTruth, predictionResult.get(classifier.prediction.prediction),
135 "Ground truth",
"Classification results",
136 "NaiveBayes classification results (first 20 observations):", 20, flt64=
False 139 if __name__ ==
"__main__":