Python* API Reference for Intel® Data Analytics Acceleration Library 2018 Update 2

dt_cls_traverse_model.py

1 # file: dt_cls_traverse_model.py
2 #===============================================================================
3 # Copyright 2014-2018 Intel Corporation
4 # All Rights Reserved.
5 #
6 # If this software was obtained under the Intel Simplified Software License,
7 # the following terms apply:
8 #
9 # The source code, information and material ("Material") contained herein is
10 # owned by Intel Corporation or its suppliers or licensors, and title to such
11 # Material remains with Intel Corporation or its suppliers or licensors. The
12 # Material contains proprietary information of Intel or its suppliers and
13 # licensors. The Material is protected by worldwide copyright laws and treaty
14 # provisions. No part of the Material may be used, copied, reproduced,
15 # modified, published, uploaded, posted, transmitted, distributed or disclosed
16 # in any way without Intel's prior express written permission. No license under
17 # any patent, copyright or other intellectual property rights in the Material
18 # is granted to or conferred upon you, either expressly, by implication,
19 # inducement, estoppel or otherwise. Any license under such intellectual
20 # property rights must be express and approved by Intel in writing.
21 #
22 # Unless otherwise agreed by Intel in writing, you may not remove or alter this
23 # notice or any other notice embedded in Materials by Intel or Intel's
24 # suppliers or licensors in any way.
25 #
26 #
27 # If this software was obtained under the Apache License, Version 2.0 (the
28 # "License"), the following terms apply:
29 #
30 # You may not use this file except in compliance with the License. You may
31 # obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
32 #
33 #
34 # Unless required by applicable law or agreed to in writing, software
35 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 #
38 # See the License for the specific language governing permissions and
39 # limitations under the License.
40 #===============================================================================
41 
42 #
43 # ! Content:
44 # ! Python example of decision tree classification model traversal.
45 # !
46 # ! The program trains the decision tree classification model on a training
47 # ! datasetFileName and prints the trained model by its depth-first traversing.
48 # !*****************************************************************************
49 
50 #
51 
52 
53 #
54 from __future__ import print_function
55 
56 from daal.algorithms import classifier
57 from daal.algorithms import decision_tree
58 import daal.algorithms.decision_tree.classification
59 import daal.algorithms.decision_tree.classification.training
60 
61 from daal.data_management import (
62  DataSourceIface, NumericTableIface, HomogenNumericTable, MergedNumericTable, FileDataSource
63 )
64 
65 # Input data set parameters
66 trainDatasetFileName = "../data/batch/decision_tree_train.csv"
67 pruneDatasetFileName = "../data/batch/decision_tree_prune.csv"
68 
69 nFeatures = 5
70 nClasses = 5
71 
72 
73 def trainModel():
74 
75  # Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file
76  trainDataSource = FileDataSource(
77  trainDatasetFileName, DataSourceIface.notAllocateNumericTable, DataSourceIface.doDictionaryFromContext
78  )
79 
80  # Create Numeric Tables for training data and labels
81  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
82  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
83  mergedData = MergedNumericTable(trainData, trainGroundTruth)
84 
85  # Retrieve the data from the input file
86  trainDataSource.loadDataBlock(mergedData)
87 
88  # Initialize FileDataSource<CSVFeatureManager> to retrieve the pruning input data from a .csv file
89  pruneDataSource = FileDataSource(
90  pruneDatasetFileName, DataSourceIface.notAllocateNumericTable, DataSourceIface.doDictionaryFromContext
91  )
92 
93  # Create Numeric Tables for pruning data and labels
94  pruneData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
95  pruneGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
96  pruneMergedData = MergedNumericTable(pruneData, pruneGroundTruth)
97 
98  # Retrieve the data from the pruning input file
99  pruneDataSource.loadDataBlock(pruneMergedData)
100 
101  # Create an algorithm object to train the Decision tree model
102  algorithm = decision_tree.classification.training.Batch(nClasses)
103 
104  # Pass the training data set, labels, and pruning dataset with labels to the algorithm
105  algorithm.input.set(classifier.training.data, trainData)
106  algorithm.input.set(classifier.training.labels, trainGroundTruth)
107  algorithm.input.set(decision_tree.classification.training.dataForPruning, pruneData)
108  algorithm.input.set(decision_tree.classification.training.labelsForPruning, pruneGroundTruth)
109 
110  # Train the Decision tree model and retrieve the results
111  return algorithm.compute()
112 
113 
114 # Visitor class implementing NodeVisitor interface, prints out tree nodes of the
115 # model when it is called back by model traversal method
116 class PrintNodeVisitor(classifier.TreeNodeVisitor):
117 
118  def __init__(self):
119  super(PrintNodeVisitor, self).__init__()
120 
121  def onLeafNode(self, level, response):
122 
123  for i in range(level):
124  print(" ", end='')
125  print("Level {}, leaf node. Response value = {}".format(level, response))
126 
127  return True
128 
129  def onSplitNode(self, level, featureIndex, featureValue):
130 
131  for i in range(level):
132  print(" ", end='')
133  print("Level {}, split node. Feature index = {}, feature value = {:.4g}".format(level, featureIndex, featureValue))
134 
135  return True
136 
137 
138 def printModel(m):
139  visitor = PrintNodeVisitor()
140  m.traverseDF(visitor)
141 
142 
143 if __name__ == "__main__":
144 
145  trainingResult = trainModel()
146  printModel(trainingResult.get(classifier.training.model))

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