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

dt_cls_dense_batch.py

1 # file: dt_cls_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.decision_tree.classification import prediction, training
25 from daal.algorithms import classifier
26 from daal.data_management import (
27  FileDataSource, DataSourceIface, NumericTableIface, HomogenNumericTable, MergedNumericTable
28 )
29 utils_folder = os.path.realpath(os.path.abspath(os.path.dirname(os.path.dirname(__file__))))
30 if utils_folder not in sys.path:
31  sys.path.insert(0, utils_folder)
32 from utils import printNumericTables
33 
34 DAAL_PREFIX = os.path.join('..', 'data')
35 
36 # Input data set parameters
37 trainDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'decision_tree_train.csv')
38 pruneDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'decision_tree_prune.csv')
39 testDatasetFileName = os.path.join(DAAL_PREFIX, 'batch', 'decision_tree_test.csv')
40 
41 nFeatures = 5
42 nClasses = 5
43 
44 # Model object for the decision tree classification algorithm
45 model = None
46 predictionResult = None
47 testGroundTruth = None
48 
49 
50 def trainModel():
51  global model
52 
53  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
54  trainDataSource = FileDataSource(
55  trainDatasetFileName,
56  DataSourceIface.notAllocateNumericTable,
57  DataSourceIface.doDictionaryFromContext
58  )
59 
60  # Create Numeric Tables for training data and labels
61  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
62  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
63  mergedData = MergedNumericTable(trainData, trainGroundTruth)
64 
65  # Retrieve the data from the input file
66  trainDataSource.loadDataBlock(mergedData)
67 
68  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
69  pruneDataSource = FileDataSource(
70  pruneDatasetFileName,
71  DataSourceIface.notAllocateNumericTable,
72  DataSourceIface.doDictionaryFromContext
73  )
74 
75  # Create Numeric Tables for pruning data and labels
76  pruneData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
77  pruneGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
78  pruneMergedData = MergedNumericTable(pruneData, pruneGroundTruth)
79 
80  # Retrieve the data from the input file
81  pruneDataSource.loadDataBlock(pruneMergedData)
82 
83  # Create an algorithm object to train the decision tree classification model
84  algorithm = training.Batch(nClasses)
85 
86  # Pass the training data set and dependent values to the algorithm
87  algorithm.input.set(classifier.training.data, trainData)
88  algorithm.input.set(classifier.training.labels, trainGroundTruth)
89  algorithm.input.setTable(training.dataForPruning, pruneData)
90  algorithm.input.setTable(training.labelsForPruning, pruneGroundTruth)
91 
92  # Train the decision tree classification model and retrieve the results of the training algorithm
93  trainingResult = algorithm.compute()
94  model = trainingResult.get(classifier.training.model)
95 
96 def testModel():
97  global testGroundTruth, predictionResult
98 
99  # Initialize FileDataSource<CSVFeatureManager> to retrieve the test data from a .csv file
100  testDataSource = FileDataSource(
101  testDatasetFileName,
102  DataSourceIface.notAllocateNumericTable,
103  DataSourceIface.doDictionaryFromContext
104  )
105 
106  # Create Numeric Tables for testing data and labels
107  testData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
108  testGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
109  mergedData = MergedNumericTable(testData, testGroundTruth)
110 
111  # Retrieve the data from input file
112  testDataSource.loadDataBlock(mergedData)
113 
114  # Create algorithm objects for decision tree classification prediction with the default method
115  algorithm = prediction.Batch()
116 
117  # Pass the testing data set and trained model to the algorithm
118  #print("Number of columns: {}".format(testData.getNumberOfColumns()))
119  algorithm.input.setTable(classifier.prediction.data, testData)
120  algorithm.input.setModel(classifier.prediction.model, model)
121 
122  # Compute prediction results and retrieve algorithm results
123  # (Result class from classifier.prediction)
124  predictionResult = algorithm.compute()
125 
126 
127 def printResults():
128 
129  printNumericTables(
130  testGroundTruth,
131  predictionResult.get(classifier.prediction.prediction),
132  "Ground truth", "Classification results",
133  "Decision tree classification results (first 20 observations):",
134  20, flt64=False
135  )
136 
137 if __name__ == "__main__":
138 
139  trainModel()
140  testModel()
141  printResults()

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