Python* API Reference for Intel® Data Analytics Acceleration Library 2020 Update 1

kdtree_knn_dense_batch.py

1 # file: kdtree_knn_dense_batch.py
2 #===============================================================================
3 # Copyright 2014-2020 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #===============================================================================
17 
18 
19 
20 
21 import os
22 import sys
23 
24 from daal.algorithms.kdtree_knn_classification import training, prediction
25 from daal.algorithms import classifier
26 from daal.data_management import (
27  DataSourceIface, FileDataSource, HomogenNumericTable, MergedNumericTable, NumericTableIface
28 )
29 
30 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
31 if utils_folder not in sys.path:
32  sys.path.insert(0, utils_folder)
33 from utils import printNumericTables
34 
35 DAAL_PREFIX = os.path.join('..', 'data')
36 
37 # Input data set parameters
38 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'k_nearest_neighbors_train.csv')
39 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'k_nearest_neighbors_test.csv')
40 
41 nFeatures = 5
42 nClasses = 5
43 
44 trainingResult = None
45 predictionResult = None
46 
47 
48 def trainModel():
49  global trainingResult
50 
51  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
52  trainDataSource = FileDataSource(
53  trainDatasetFileName, DataSourceIface.notAllocateNumericTable,
54  DataSourceIface.doDictionaryFromContext
55  )
56 
57  # Create Numeric Tables for training data and dependent variables
58  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
59  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
60  mergedData = MergedNumericTable(trainData, trainGroundTruth)
61 
62  # Retrieve the data from input file
63  trainDataSource.loadDataBlock(mergedData)
64 
65  # Create an algorithm object to train the KD-tree based kNN model
66  algorithm = training.Batch()
67 
68  # Pass a training data set and dependent values to the algorithm
69  algorithm.input.set(classifier.training.data, trainData)
70  algorithm.input.set(classifier.training.labels, trainGroundTruth)
71  algorithm.parameter.nClasses = nClasses
72 
73  # Train the KD-tree based kNN model
74  trainingResult = algorithm.compute()
75 
76 
77 def testModel():
78  global trainingResult, predictionResult
79 
80  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
81  testDataSource = FileDataSource(
82  testDatasetFileName, DataSourceIface.doAllocateNumericTable,
83  DataSourceIface.doDictionaryFromContext
84  )
85 
86  # Create Numeric Tables for testing data and ground truth values
87  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.doNotAllocate)
88  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.doNotAllocate)
89  mergedData = MergedNumericTable(testData, testGroundTruth)
90 
91  # Load the data from the data file
92  testDataSource.loadDataBlock(mergedData)
93 
94  # Create algorithm objects for KD-tree based kNN prediction with the default method
95  algorithm = prediction.Batch()
96 
97  # Pass the testing data set and trained model to the algorithm
98  algorithm.input.setTable(classifier.prediction.data, testData)
99  algorithm.input.setModel(classifier.prediction.model, trainingResult.get(classifier.training.model))
100 
101  # Compute prediction results
102  predictionResult = algorithm.compute()
103  printNumericTables(
104  testGroundTruth, predictionResult.get(classifier.prediction.prediction),
105  "Ground truth", "Classification results",
106  "KD-tree based kNN classification results (first 20 observations):", 20, flt64=False
107  )
108 
109 if __name__ == "__main__":
110 
111  trainModel()
112  testModel()

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