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

dt_reg_traverse_model.py

1 # file: dt_reg_traverse_model.py
2 #===============================================================================
3 # Copyright 2014-2017 Intel Corporation All Rights Reserved.
4 #
5 # The source code, information and material ("Material") contained herein is
6 # owned by Intel Corporation or its suppliers or licensors, and title to such
7 # Material remains with Intel Corporation or its suppliers or licensors. The
8 # Material contains proprietary information of Intel or its suppliers and
9 # licensors. The Material is protected by worldwide copyright laws and treaty
10 # provisions. No part of the Material may be used, copied, reproduced,
11 # modified, published, uploaded, posted, transmitted, distributed or disclosed
12 # in any way without Intel's prior express written permission. No license under
13 # any patent, copyright or other intellectual property rights in the Material
14 # is granted to or conferred upon you, either expressly, by implication,
15 # inducement, estoppel or otherwise. Any license under such intellectual
16 # property rights must be express and approved by Intel in writing.
17 #
18 # Unless otherwise agreed by Intel in writing, you may not remove or alter this
19 # notice or any other notice embedded in Materials by Intel or Intel's
20 # suppliers or licensors in any way.
21 #===============================================================================
22 
23 #
24 # ! Content:
25 # ! C++ example of decision tree classification model traversal.
26 # !
27 # ! The program trains the decision tree classification model on a training
28 # ! datasetFileName and prints the trained model by its depth-first traversing.
29 # !*****************************************************************************
30 
31 #
32 ## <a name = "DAAL-EXAMPLE-PY-DT_REG_TRAVERSE_MODEL"></a>
33 ## \example dt_reg_traverse_model.py
34 #
35 
36 from __future__ import print_function
37 
38 from daal.algorithms import regression
39 from daal.algorithms import decision_tree
40 import daal.algorithms.decision_tree.regression
41 import daal.algorithms.decision_tree.regression.training
42 
43 from daal.data_management import FileDataSource, DataSourceIface, NumericTableIface, HomogenNumericTable, MergedNumericTable
44 
45 # Input data set parameters
46 trainDatasetFileName = "../data/batch/decision_tree_train.csv"
47 pruneDatasetFileName = "../data/batch/decision_tree_prune.csv"
48 
49 nFeatures = 5 # Number of features in training and testing data sets
50 
51 
52 def trainModel():
53 
54  # Initialize FileDataSource to retrieve the input data from a .csv file
55  trainDataSource = FileDataSource(
56  trainDatasetFileName, DataSourceIface.notAllocateNumericTable, DataSourceIface.doDictionaryFromContext
57  )
58 
59  # Create Numeric Tables for training data and dependent variables
60  trainData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
61  trainGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
62  mergedData = MergedNumericTable(trainData, trainGroundTruth)
63 
64  # Retrieve the data from the input file
65  trainDataSource.loadDataBlock(mergedData)
66 
67  # Initialize FileDataSource<CSVFeatureManager> to retrieve the pruning input data from a .csv file
68  pruneDataSource = FileDataSource(
69  pruneDatasetFileName, DataSourceIface.notAllocateNumericTable, DataSourceIface.doDictionaryFromContext
70  )
71 
72  # Create Numeric Tables for pruning data and dependent variables
73  pruneData = HomogenNumericTable(nFeatures, 0, NumericTableIface.notAllocate)
74  pruneGroundTruth = HomogenNumericTable(1, 0, NumericTableIface.notAllocate)
75  pruneMergedData = MergedNumericTable(pruneData, pruneGroundTruth)
76 
77  # Retrieve the data from the pruning input file
78  pruneDataSource.loadDataBlock(pruneMergedData)
79 
80  # Create an algorithm object to train the Decision tree model
81  algorithm = decision_tree.regression.training.Batch()
82 
83  # Pass the training data set, dependent variables, and pruning dataset with dependent variables to the algorithm
84  algorithm.input.set(decision_tree.regression.training.data, trainData)
85  algorithm.input.set(decision_tree.regression.training.dependentVariables, trainGroundTruth)
86  algorithm.input.set(decision_tree.regression.training.dataForPruning, pruneData)
87  algorithm.input.set(decision_tree.regression.training.dependentVariablesForPruning, pruneGroundTruth)
88 
89  # Train the Decision tree model and return the results
90  return algorithm.compute()
91 
92 
93 # Visitor class implementing NodeVisitor interface, prints out tree nodes of the model when it is called back by model traversal method
94 class PrintNodeVisitor(regression.TreeNodeVisitor):
95 
96  def __init__(self):
97  super(PrintNodeVisitor, self).__init__()
98 
99  def onLeafNode(self, level, response):
100 
101  for i in range(level):
102  print(" ", end='')
103  print("Level {}, leaf node. Response value = {:.4g}".format(level, response))
104  return True
105 
106 
107  def onSplitNode(self, level, featureIndex, featureValue):
108 
109  for i in range(level):
110  print(" ", end='')
111  print("Level {}, split node. Feature index = {}, feature value = {:.4g}".format(level, featureIndex, featureValue))
112  return True
113 
114 
115 def printModel(m):
116  visitor = PrintNodeVisitor()
117  m.traverseDF(visitor)
118 
119 if __name__ == "__main__":
120 
121  trainingResult = trainModel()
122  printModel(trainingResult.get(decision_tree.regression.training.model))

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