source: orange/orange/OrangeWidgets/Classify/OWLogisticRegression.py @ 9546:2b6cc6f397fe

Revision 9546:2b6cc6f397fe, 5.9 KB checked in by ales_erjavec <ales.erjavec@…>, 2 years ago (diff)

Renamed widget channel names in line with the new naming rules/convention.
Added backwards compatibility in orngDoc loadDocument to enable loading of schemas saved before the change.

Line 
1"""
2<name>Logistic Regression</name>
3<description>Logistic regression learner/classifier.</description>
4<icon>icons/LogisticRegression.png</icon>
5<contact>Martin Mozina (martin.mozina(@at@)fri.uni-lj.si)</contact>
6<priority>15</priority>
7"""
8from OWWidget import *
9from orngLR import *
10import OWGUI
11
12from orngWrap import PreprocessedLearner
13
14class OWLogisticRegression(OWWidget):
15    settingsList = ["univariate", "name", "stepwiseLR", "addCrit", "removeCrit", "numAttr", "zeroPoint", "imputation", "limitNumAttr"]
16
17    def __init__ (self, parent=None, signalManager = None, name = "Logistic regression"):
18        OWWidget.__init__(self, parent, signalManager, name, wantMainArea = 0, resizingEnabled = 0)
19
20        self.inputs = [("Data", ExampleTable, self.setData), ("Preprocess", PreprocessedLearner, self.setPreprocessor)]
21        self.outputs = [("Learner", orange.Learner), ("Classifier", orange.Classifier), ("Features", list)]
22
23        from orngTree import TreeLearner
24        imputeByModel = orange.ImputerConstructor_model()
25        imputeByModel.learnerDiscrete = TreeLearner(measure = "infoGain", minSubset = 50)
26        imputeByModel.learnerContinuous = TreeLearner(measure = "retis", minSubset = 50)
27        self.imputationMethods = [imputeByModel, orange.ImputerConstructor_average(), orange.ImputerConstructor_minimal(), orange.ImputerConstructor_maximal(), None]
28        self.imputationMethodsStr = ["Classification/Regression trees", "Average values", "Minimal value", "Maximal value", "None (skip examples)"]
29
30        self.name = "Logistic regression"
31        self.univariate = 0
32        self.stepwiseLR = 0
33        self.addCrit = 10
34        self.removeCrit = 10
35        self.numAttr = 10
36        self.limitNumAttr = False
37        self.zeroPoint = 0
38        self.imputation = 1
39
40        self.data = None
41        self.preprocessor = None
42
43        self.loadSettings()
44
45        OWGUI.lineEdit(self.controlArea, self, 'name', box='Learner/Classifier Name', tooltip='Name to be used by other widgets to identify your learner/classifier.')
46        OWGUI.separator(self.controlArea)
47
48        box = OWGUI.widgetBox(self.controlArea, "Attribute selection")
49
50        stepwiseCb = OWGUI.checkBox(box, self, "stepwiseLR", "Stepwise attribute selection")
51        ibox = OWGUI.indentedBox(box, sep=OWGUI.checkButtonOffsetHint(stepwiseCb))
52        addCritSpin = OWGUI.spin(ibox, self, "addCrit", 1, 50, label="Add threshold [%]", labelWidth=155, tooltip="Requested significance for adding an attribute")
53        remCritSpin = OWGUI.spin(ibox, self, "removeCrit", 1, 50, label="Remove threshold [%]", labelWidth=155, tooltip="Requested significance for removing an attribute")
54        limitAttSpin = OWGUI.checkWithSpin(ibox, self, "Limit number of attributes to ", 1, 100, "limitNumAttr", "numAttr", step=1, labelWidth=155, tooltip="Maximum number of attributes. Algorithm stops when it selects specified number of attributes.")
55        stepwiseCb.disables += [addCritSpin, remCritSpin, limitAttSpin]
56        stepwiseCb.makeConsistent()
57       
58        OWGUI.separator(self.controlArea)
59
60        self.imputationCombo = OWGUI.comboBox(self.controlArea, self, "imputation", box="Imputation of unknown values", items=self.imputationMethodsStr)
61        OWGUI.separator(self.controlArea)
62
63        applyButton = OWGUI.button(self.controlArea, self, "&Apply", callback=self.applyLearner, default=True)
64
65        OWGUI.rubber(self.controlArea)
66        #self.adjustSize()
67
68        self.applyLearner()
69
70    def sendReport(self):
71        if self.stepwiseLR:
72            step = "add at %i%%, remove at %i%%" % (self.addCrit, self.removeCrit)
73            if self.limitNumAttr:
74                step += "; allow up to %i attributes" % self.numAttr
75        else:
76            step = "No"
77        self.reportSettings("Learning parameters",
78                            [("Stepwise attribute selection", step),
79                             ("Imputation of unknown values", self.imputationMethodsStr[self.imputation])])
80        self.reportData(self.data)
81       
82
83    def applyLearner(self):
84        imputer = self.imputationMethods[self.imputation]
85        removeMissing = not imputer
86
87        if self.univariate:
88            self.learner = Univariate_LogRegLearner()
89        else:
90            self.learner = LogRegLearner(removeSingular = True, imputer = imputer, removeMissing = removeMissing,
91                                         stepwiseLR = self.stepwiseLR, addCrit = self.addCrit/100., removeCrit = self.removeCrit/100.,
92                                         numAttr = self.limitNumAttr and float(self.numAttr) or -1.0)
93
94        if self.preprocessor:
95            self.learner = self.preprocessor.wrapLearner(self.learner)
96        self.learner.name = self.name
97        self.send("Learner", self.learner)
98        self.applyData()
99
100    def applyData(self):
101        classifier = None
102
103        if self.data:
104            if self.zeroPoint:
105                classifier, betas_ap = LogRegLearner_getPriors(self.data)
106                self.error()
107                classifier.setattr("betas_ap", betas_ap)
108            else:
109                try:
110                    classifier = self.learner(self.data)
111                    self.error()
112                except orange.KernelException, (errValue):
113                    self.error("LogRegFitter error:"+ str(errValue))
114
115            if classifier:
116                classifier.setattr("data", self.data)
117                classifier.setattr("betas_ap", None)
118                classifier.name = self.name
119
120        self.send("Classifier", classifier)
121
122
123    def setData(self, data):
124        self.data = self.isDataWithClass(data, orange.VarTypes.Discrete, checkMissing=True) and data or None
125        self.applyData()
126       
127    def setPreprocessor(self, pp):
128        self.preprocessor = pp
129        self.applyLearner()
130
131
132if __name__=="__main__":
133    a=QApplication(sys.argv)
134    ow=OWLogisticRegression()
135
136    #dataset = orange.ExampleTable(r'..\..\doc\datasets\heart_disease')
137    #ow.setData(dataset)
138
139    ow.show()
140    a.exec_()
141    ow.saveSettings()
Note: See TracBrowser for help on using the repository browser.