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

dt_cls_traverse_model.py

1 # file: dt_cls_traverse_model.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 # ! Content:
20 # ! Python example of decision tree classification model traversal.
21 # !
22 # ! The program trains the decision tree classification model on a training
23 # ! datasetFileName and prints the trained model by its depth-first traversing.
24 # !*****************************************************************************
25 
26 #
27 
28 
29 #
30 from __future__ import print_function
31 
32 from daal.algorithms import classifier
33 from daal.algorithms import decision_tree
34 import daal.algorithms.decision_tree.classification
35 import daal.algorithms.decision_tree.classification.training
36 
37 from daal.data_management import (
38  DataSourceIface, NumericTableIface, HomogenNumericTable, MergedNumericTable, FileDataSource
39 )
40 
41 # Input data set parameters
42 trainDatasetFileName = "../data/batch/decision_tree_train.csv"
43 pruneDatasetFileName = "../data/batch/decision_tree_prune.csv"
44 
45 nFeatures = 5
46 nClasses = 5
47 
48 
49 def trainModel():
50 
51  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
52  trainDataSource = FileDataSource(
53  trainDatasetFileName, DataSourceIface.notAllocateNumericTable, DataSourceIface.doDictionaryFromContext
54  )
55 
56  # Create Numeric Tables for training data and labels
57  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
58  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
59  mergedData = MergedNumericTable(trainData, trainGroundTruth)
60 
61  # Retrieve the data from the input file
62  trainDataSource.loadDataBlock(mergedData)
63 
64  # Initialize FileDataSource<CSVFeatureManager> to retrieve the pruning input data from a .csv file
65  pruneDataSource = FileDataSource(
66  pruneDatasetFileName, DataSourceIface.notAllocateNumericTable, DataSourceIface.doDictionaryFromContext
67  )
68 
69  # Create Numeric Tables for pruning data and labels
70  pruneData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
71  pruneGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
72  pruneMergedData = MergedNumericTable(pruneData, pruneGroundTruth)
73 
74  # Retrieve the data from the pruning input file
75  pruneDataSource.loadDataBlock(pruneMergedData)
76 
77  # Create an algorithm object to train the Decision tree model
78  algorithm = decision_tree.classification.training.Batch(nClasses)
79 
80  # Pass the training data set, labels, and pruning dataset with labels to the algorithm
81  algorithm.input.set(classifier.training.data, trainData)
82  algorithm.input.set(classifier.training.labels, trainGroundTruth)
83  algorithm.input.set(decision_tree.classification.training.dataForPruning, pruneData)
84  algorithm.input.set(decision_tree.classification.training.labelsForPruning, pruneGroundTruth)
85 
86  # Train the Decision tree model and retrieve the results
87  return algorithm.compute()
88 
89 
90 # Visitor class implementing NodeVisitor interface, prints out tree nodes of the
91 # model when it is called back by model traversal method
92 class PrintNodeVisitor(classifier.TreeNodeVisitor):
93 
94  def __init__(self):
95  super(PrintNodeVisitor, self).__init__()
96 
97  def onLeafNode(self, level, response):
98 
99  for i in range(level):
100  print(" ", end='')
101  print("Level {}, leaf node. Response value = {}".format(level, response))
102 
103  return True
104 
105  def onSplitNode(self, level, featureIndex, featureValue):
106 
107  for i in range(level):
108  print(" ", end='')
109  print("Level {}, split node. Feature index = {}, feature value = {:.4g}".format(level, featureIndex, featureValue))
110 
111  return True
112 
113 
114 def printModel(m):
115  visitor = PrintNodeVisitor()
116  m.traverseDF(visitor)
117 
118 
119 if __name__ == "__main__":
120 
121  trainingResult = trainModel()
122  printModel(trainingResult.get(classifier.training.model))

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