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

dt_reg_traverse_model.py

1 # file: dt_reg_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 # ! C++ 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 
54 
55 from __future__ import print_function
56 
57 from daal.algorithms import regression
58 from daal.algorithms import decision_tree
59 import daal.algorithms.decision_tree.regression
60 import daal.algorithms.decision_tree.regression.training
61 
62 from daal.data_management import FileDataSource, DataSourceIface, NumericTableIface, HomogenNumericTable, MergedNumericTable
63 
64 # Input data set parameters
65 trainDatasetFileName = "../data/batch/decision_tree_train.csv"
66 pruneDatasetFileName = "../data/batch/decision_tree_prune.csv"
67 
68 nFeatures = 5 # Number of features in training and testing data sets
69 
70 
71 def trainModel():
72 
73  # Initialize FileDataSource to retrieve the input data from a .csv file
74  trainDataSource = FileDataSource(
75  trainDatasetFileName, DataSourceIface.notAllocateNumericTable, DataSourceIface.doDictionaryFromContext
76  )
77 
78  # Create Numeric Tables for training data and dependent variables
79  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
80  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
81  mergedData = MergedNumericTable(trainData, trainGroundTruth)
82 
83  # Retrieve the data from the input file
84  trainDataSource.loadDataBlock(mergedData)
85 
86  # Initialize FileDataSource<CSVFeatureManager> to retrieve the pruning input data from a .csv file
87  pruneDataSource = FileDataSource(
88  pruneDatasetFileName, DataSourceIface.notAllocateNumericTable, DataSourceIface.doDictionaryFromContext
89  )
90 
91  # Create Numeric Tables for pruning data and dependent variables
92  pruneData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
93  pruneGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
94  pruneMergedData = MergedNumericTable(pruneData, pruneGroundTruth)
95 
96  # Retrieve the data from the pruning input file
97  pruneDataSource.loadDataBlock(pruneMergedData)
98 
99  # Create an algorithm object to train the Decision tree model
100  algorithm = decision_tree.regression.training.Batch()
101 
102  # Pass the training data set, dependent variables, and pruning dataset with dependent variables to the algorithm
103  algorithm.input.set(decision_tree.regression.training.data, trainData)
104  algorithm.input.set(decision_tree.regression.training.dependentVariables, trainGroundTruth)
105  algorithm.input.set(decision_tree.regression.training.dataForPruning, pruneData)
106  algorithm.input.set(decision_tree.regression.training.dependentVariablesForPruning, pruneGroundTruth)
107 
108  # Train the Decision tree model and return the results
109  return algorithm.compute()
110 
111 
112 # Visitor class implementing NodeVisitor interface, prints out tree nodes of the model when it is called back by model traversal method
113 class PrintNodeVisitor(regression.TreeNodeVisitor):
114 
115  def __init__(self):
116  super(PrintNodeVisitor, self).__init__()
117 
118  def onLeafNode(self, level, response):
119 
120  for i in range(level):
121  print(" ", end='')
122  print("Level {}, leaf node. Response value = {:.4g}".format(level, response))
123  return True
124 
125 
126  def onSplitNode(self, level, featureIndex, featureValue):
127 
128  for i in range(level):
129  print(" ", end='')
130  print("Level {}, split node. Feature index = {}, feature value = {:.4g}".format(level, featureIndex, featureValue))
131  return True
132 
133 
134 def printModel(m):
135  visitor = PrintNodeVisitor()
136  m.traverseDF(visitor)
137 
138 if __name__ == "__main__":
139 
140  trainingResult = trainModel()
141  printModel(trainingResult.get(decision_tree.regression.training.model))

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