source: orange/Orange/OrangeWidgets/Regression/OWRegressionTree.py @ 9671:a7b056375472

Revision 9671:a7b056375472, 4.8 KB checked in by anze <anze.staric@…>, 2 years ago (diff)

Moved orange to Orange (part 2)

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