⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 owlogisticregression.py

📁 orange源码 数据挖掘技术
💻 PY
字号:
"""
<name>Logistic Regression</name>
<description>Logistic regression learner/classifier.</description>
<icon>icons/LogisticRegression.png</icon>
<contact>Martin Mozina (martin.mozina(@at@)fri.uni-lj.si)</contact> 
<priority>15</priority>
"""

from OWWidget import *
from orngLR import *
import OWGUI

class OWLogisticRegression(OWWidget):
    settingsList = ["removeSingular", "univariate", "name", "stepwiseLR", "addCrit", "removeCrit", "numAttr", "zeroPoint", "imputation", "limitNumAttr"]

    def __init__ (self, parent=None, signalManager = None, name = "Logistic regression"):    
        OWWidget.__init__(self, parent, signalManager, name)

        self.inputs = [("Examples", ExampleTable, self.cdata)]
        self.outputs = [("Learner", orange.Learner),("Classifier", orange.Classifier),("Attributes", list)]

        from orngTree import TreeLearner
        imputeByModel = orange.ImputerConstructor_model()
        imputeByModel.learnerDiscrete = TreeLearner(measure = "infoGain", minSubset = 50)
        imputeByModel.learnerContinuous = TreeLearner(measure = "retis", minSubset = 50)
        self.imputationMethods = [imputeByModel, orange.ImputerConstructor_average(), orange.ImputerConstructor_minimal(), orange.ImputerConstructor_maximal(), None]
        self.imputationMethodsStr = ["Classification/Regression trees", "Average values", "Minimal value", "Maximal value", "None (skip examples)"]

        self.name = "Logistic regression"
        self.removeSingular = 1
        self.univariate = 0
        self.stepwiseLR = 0
        self.addCrit = 10
        self.removeCrit = 10
        self.numAttr = 10
        self.limitNumAttr = False
        self.zeroPoint = 0
        self.imputation = 1

        self.data = None
        self.preprocessor = None

        self.loadSettings()

        OWGUI.lineEdit(self.controlArea, self, 'name', box='Learner/Classifier Name', tooltip='Name to be used by other widgets to identify your learner/classifier.')
        OWGUI.separator(self.controlArea)

        box = OWGUI.widgetBox(self.controlArea, "Attribute selection", addSpace=True)        

        OWGUI.checkBox(box, self, "removeSingular", "Remove singular attributes", tooltip="Remove constant attributes and attributes causing singularities")

        stepwiseCb = OWGUI.checkBox(box, self, "stepwiseLR", "Stepwise attribute selection")
        ibox = OWGUI.indentedBox(box)
        addCritSpin = OWGUI.spin(ibox, self, "addCrit", 1, 50, label="Add criteria [%]", labelWidth=155, tooltip="Requested significance of attribute to be added in common model.")
        remCritSpin = OWGUI.spin(ibox, self, "removeCrit", 1, 50, label="Remove criteria [%]", labelWidth=155, tooltip="Requested significance of attribute to be removed from common model.")
        limitAttSpin = OWGUI.checkWithSpin(ibox, self, "Limit number of attributes to ", 1, 100, "limitNumAttr", "numAttr", step=1, labelWidth=155, tooltip="Maximum number of selected attributes. Algorithm stops when it selects specified number of attributes.")
        stepwiseCb.disables += [addCritSpin, remCritSpin, limitAttSpin]
        stepwiseCb.makeConsistent()

        self.imputationCombo = OWGUI.comboBox(self.controlArea, self, "imputation", box="Imputation of unknown values", items=self.imputationMethodsStr)
        OWGUI.separator(self.controlArea)

        applyButton = OWGUI.button(self.controlArea, self, "&Apply", callback = self.apply)

        OWGUI.rubber(self.controlArea)
        self.adjustSize()

        self.apply()

    def apply(self):
        imputer = self.imputationMethods[self.imputation]
        removeMissing = not imputer

        if self.univariate:
            self.learner = Univariate_LogRegLearner()
        else:            
            self.learner = LogRegLearner(removeSingular = self.removeSingular, imputer = imputer, removeMissing = removeMissing,
                                         stepwiseLR = self.stepwiseLR, addCrit = self.addCrit/100., removeCrit = self.removeCrit/100.,
                                         numAttr = self.limitNumAttr and float(self.numAttr) or -1.0)

        self.learner.name = self.name
        self.send("Learner", self.learner)        

        if self.data:
            if self.zeroPoint:
                self.classifier, betas_ap = LogRegLearner_getPriors(self.data)
                self.classifier.setattr("betas_ap", betas_ap)
            else:
                try:
                    self.classifier = self.learner(self.data)
                    self.classifier.setattr("data", self.data)
                except orange.KernelException, (errValue):
                    self.classifier = None
                    self.error("LogRegFitter error:"+ str(errValue))
                    return
            self.classifier.setattr("betas_ap", None)
                    
            self.classifier.name = self.name
            self.send("Classifier", self.classifier)
        self.error()

        
    def cdata(self, data):
        self.data = data
        self.apply()

        
    # Possible attribute setting :
    #    * stepwise logistic regression (addCrit, deleteCrit, number_of_attributes)
    #    * remove singular attributes

    # adding a trace windows would work perhaps --> better option
    # processing and a "slide" window at the bottom ? --> this couldn't work

##############################################################################
# Test the widget, run from DOS prompt

if __name__=="__main__":
    a=QApplication(sys.argv)
    ow=OWLogisticRegression()
    a.setMainWidget(ow)

    dataset = orange.ExampleTable(r'..\..\doc\datasets\heart_disease')
    ow.cdata(dataset)

    ow.show()
    a.exec_loop()
    ow.saveSettings() 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -