source: orange/Orange/OrangeWidgets/Regression/OWRegressionTree.py @ 10552:c0028bc3f865

Revision 10552:c0028bc3f865, 4.9 KB checked in by Ales Erjavec <ales.erjavec@…>, 2 years ago (diff)

Replaced orange.TreeClassifier with Orange.classification.tree.TreeClassifier in channel types (needed for dynamic signals to work when connecting to Tree Graph/Viewer widgets).

Line 
1"""
2<name>Regression Tree</name>
3<description>Constructs a tree regression learner and given data a regression tree classifier</description>
4<icon>RegressionTree.png</icon>
5<contact>Ales Erjavec (ales.erjavec(@at@)fri.uni-lj.si)</contact>
6<priority>110</priority>
7
8"""
9
10import orange
11import orngTree
12import OWGUI
13from OWWidget import *
14import sys
15
16from orngWrap import PreprocessedLearner
17
18import Orange
19
20class OWRegressionTree(OWWidget):
21    settingsList=["Name","MinInstCheck", "MinInstVal", "MinNodeCheck", "MinNodeVal,"
22                  "MaxMajCheck", "MaxMajVal", "PostMaj", "PostMPCheck", "PostMPVal", "Bin"]
23    callbackDeposit=[]
24    def __init__(self, parent=None, signalManager=None):
25        OWWidget.__init__(self, parent, signalManager, "Regression Tree", wantMainArea = 0, resizingEnabled = 0)
26        self.Name="Regression Tree"
27        self.MinInstCheck=1
28        self.MinInstVal=5
29        self.MinNodeCheck=1
30        self.MinNodeVal=10
31        self.MaxMajCheck=1
32        self.MaxMajVal=70
33        self.PostMaj=1
34        self.PostMPCheck=1
35        self.PostMPVal=5
36        self.Bin=1
37        self.loadSettings()
38
39        self.data=None
40        self.preprocessor = None
41
42        self.inputs=[("Data",ExampleTable,self.dataset),
43                     ("Preprocess", PreprocessedLearner, self.setPreprocessor)]
44       
45        self.outputs=[("Learner", orange.Learner),
46                      ("Regressor", orange.Classifier),
47                      ("Regression Tree", Orange.regression.tree.TreeClassifier)]
48
49        ##
50        #GUI
51        ##
52        OWGUI.lineEdit(self.controlArea, self, "Name", box="Learner/Classifier name")
53
54        OWGUI.separator(self.controlArea)
55        OWGUI.checkBox(self.controlArea, self, "Bin", label="Binarization", box ="Tree structure")
56
57        OWGUI.separator(self.controlArea)
58        self.prePBox=OWGUI.widgetBox(self.controlArea, "Pre-Pruning")
59
60        #OWGUI.checkWithSpin(self.prePBox, self, "Min. instances in leaves: ", 1, 1000,
61        #                    "MinInstCheck", "MinInstVal")
62
63        OWGUI.checkWithSpin(self.prePBox, self, "Do not split nodes with less instances than", 1, 1000,
64                            "MinNodeCheck", "MinNodeVal")
65
66        #OWGUI.checkWithSpin(self.prePBox, self, "Stop splitting nodes with ", 1, 100,
67        #                    "MaxMajCheck", "MaxMajVal", "% of majority class")
68
69        #OWGUI.checkBox(self.postPBox, self, 'PostMaj', 'Recursively merge leaves with same majority class')
70
71        OWGUI.separator(self.controlArea)
72        self.postPBox=OWGUI.widgetBox(self.controlArea, "Post-Pruning")
73        OWGUI.checkWithSpin(self.postPBox, self, "Pruning with m-estimate, m:", 0, 1000, 'PostMPCheck', 'PostMPVal')
74
75        OWGUI.button(self.controlArea, self, "&Apply settings", callback=self.setLearner, default=True)
76       
77        OWGUI.rubber(self.controlArea)
78        self.setLearner()
79        self.resize(100,100)
80
81    def sendReport(self):
82        self.reportSettings("Learning parameters",
83                            [("Binarization", OWGUI.YesNo[self.Bin]),
84                             ("Minimal number of instances in internal nodes", self.MinNodeVal if self.MinNodeCheck else "Not limited"),
85                             ("Pruning with m-estimate", "m=%i" % self.PostMPVal if self.PostMPCheck else "No")])
86        self.reportData(self.data)
87       
88
89    def setLearner(self):
90        learner=orngTree.TreeLearner(measure="retis",
91                         binarization=self.Bin,
92                         mForPruning=self.PostMPCheck and self.PostMPVal,
93                         minExamples=self.MinNodeCheck and self.MinNodeVal,
94                         storeExamples=1)
95        if self.preprocessor:
96            learner = self.preprocessor.wrapLearner(learner) 
97        learner.name=self.Name
98        self.send("Learner",learner)
99        self.error()
100
101        classifier = None
102       
103        if self.data:
104            try:
105                classifier=learner(self.data)
106                classifier.name=self.Name
107            except orange.KernelException, (errValue):
108                self.error(str(errValue))
109                classifier = None
110       
111        self.send("Regressor", classifier)
112        self.send("Regression Tree", classifier)
113        #orngTree.printTxt(classifier)
114
115    def dataset(self, data):
116        self.data = self.isDataWithClass(data, orange.VarTypes.Continuous, checkMissing=True) and data or None
117#        if data and self.isDataWithClass(data, orange.VarTypes.Continuous, checkMissing=True):
118        self.setLearner()
119#        else:
120#            self.data = None
121#            self.send("Learner",None)
122#            self.send("Regressor",None)
123#            self.send("Regression Tree", None)
124
125    def setPreprocessor(self, pp):
126        self.preprocessor = pp
127        self.setLearner()
128
129if __name__=="__main__":
130    app=QApplication(sys.argv)
131    w=OWRegressionTree()
132    #data=orange.ExampleTable("../../doc/datasets/housing.tab")
133    #w.dataset(data)
134    w.show()
135    app.exec_()
Note: See TracBrowser for help on using the repository browser.