Python* API Reference for Intel® Data Analytics Acceleration Library 2019 Update 4

kdtree_knn_dense_batch.py

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.

1 # file: kdtree_knn_dense_batch.py
2 #===============================================================================
3 # Copyright 2014-2019 Intel Corporation.
4 #
5 # This software and the related documents are Intel copyrighted materials, and
6 # your use of them is governed by the express license under which they were
7 # provided to you (License). Unless the License provides otherwise, you may not
8 # use, modify, copy, publish, distribute, disclose or transmit this software or
9 # the related documents without Intel's prior written permission.
10 #
11 # This software and the related documents are provided as is, with no express
12 # or implied warranties, other than those that are expressly stated in the
13 # License.
14 #===============================================================================
15 
16 ## <a name="DAAL-EXAMPLE-PY-KDTREE_KNN_DENSE_BATCH"></a>
17 ## \example kdtree_knn_dense_batch.py
18 
19 import os
20 import sys
21 
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
26 )
27 
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
32 
33 DAAL_PREFIX = os.path.join('..', 'data')
34 
35 # Input data set parameters
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')
38 
39 nFeatures = 5
40 
41 trainingResult = None
42 predictionResult = None
43 
44 
45 def trainModel():
46  global trainingResult
47 
48  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
49  trainDataSource = FileDataSource(
50  trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
51  DataSourceIface.doDictionaryFromContext
52  )
53 
54  # Create Numeric Tables for training data and dependent variables
55  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
56  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
57  mergedData = MergedNumericTable(trainData, trainGroundTruth)
58 
59  # Retrieve the data from input file
60  trainDataSource.loadDataBlock(mergedData)
61 
62  # Create an algorithm object to train the KD-tree based kNN model
63  algorithm = training.Batch()
64 
65  # Pass a training data set and dependent values to the algorithm
66  algorithm.input.set(classifier.training.data, trainData)
67  algorithm.input.set(classifier.training.labels, trainGroundTruth)
68 
69  # Train the KD-tree based kNN model
70  trainingResult = algorithm.compute()
71 
72 
73 def testModel():
74  global trainingResult, predictionResult
75 
76  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
77  testDataSource = FileDataSource(
78  testDatasetFileName, DataSourceIface.doAllocateNumericTable,
79  DataSourceIface.doDictionaryFromContext
80  )
81 
82  # Create Numeric Tables for testing data and ground truth values
83  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
84  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
85  mergedData = MergedNumericTable(testData, testGroundTruth)
86 
87  # Load the data from the data file
88  testDataSource.loadDataBlock(mergedData)
89 
90  # Create algorithm objects for KD-tree based kNN prediction with the default method
91  algorithm = prediction.Batch()
92 
93  # Pass the testing data set and trained model to the algorithm
94  algorithm.input.setTable(classifier.prediction.data, testData)
95  algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
96 
97  # Compute prediction results
98  predictionResult = algorithm.compute()
99  printNumericTables(
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
103  )
104 
105 if __name__ == "__main__":
106 
107  trainModel()
108  testModel()

For more complete information about compiler optimizations, see our Optimization Notice.