Deprecation Notice: With the introduction of daal4py, a package that supersedes PyDAAL, Intel is deprecating PyDAAL and will discontinue support starting with Intel® DAAL 2021 and Intel® Distribution for Python 2021. Until then Intel will continue to provide compatible pyDAAL pip and conda packages for newer releases of Intel DAAL and make it available in open source. However, Intel will not add the new features of Intel DAAL to pyDAAL. Intel recommends developers switch to and use daal4py.
Note: To find daal4py examples, refer to daal4py documentation or browse github repository.
22 from daal.algorithms.kdtree_knn_classification
import training, prediction
23 from daal.algorithms
import classifier
24 from daal.data_management
import (
25 DataSourceIface, FileDataSource, 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',
'k_nearest_neighbors_train.csv')
37 testDatasetFileName = os.path.join(DAAL_PREFIX,
'batch',
'k_nearest_neighbors_test.csv')
42 predictionResult =
None
49 trainDataSource = FileDataSource(
50 trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
51 DataSourceIface.doDictionaryFromContext
55 trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
56 trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
57 mergedData = MergedNumericTable(trainData, trainGroundTruth)
60 trainDataSource.loadDataBlock(mergedData)
63 algorithm = training.Batch()
66 algorithm.input.set(classifier.training.data, trainData)
67 algorithm.input.set(classifier.training.labels, trainGroundTruth)
70 trainingResult = algorithm.compute()
74 global trainingResult, predictionResult
77 testDataSource = FileDataSource(
78 testDatasetFileName, DataSourceIface.doAllocateNumericTable,
79 DataSourceIface.doDictionaryFromContext
83 testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
84 testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
85 mergedData = MergedNumericTable(testData, testGroundTruth)
88 testDataSource.loadDataBlock(mergedData)
91 algorithm = prediction.Batch()
94 algorithm.input.setTable(classifier.prediction.data, testData)
95 algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
98 predictionResult = algorithm.compute()
100 testGroundTruth, predictionResult.get(classifier.prediction.prediction),
101 "Ground truth",
"Classification results",
102 "KD-tree based kNN classification results (first 20 observations):", 20, flt64=
False
105 if __name__ ==
"__main__":