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

📄 owclassificationtree.py

📁 orange源码 数据挖掘技术
💻 PY
字号:
"""
<name>Classification Tree</name>
<description>Classification tree learner/classifier.</description>
<icon>icons/ClassificationTree.png</icon>
<contact>Janez Demsar (janez.demsar(@at@)fri.uni-lj.si)</contact> 
<priority>30</priority>
"""

from OWWidget import *
import orngTree, OWGUI
from exceptions import Exception

class OWClassificationTree(OWWidget):
    settingsList = ["name",
                    "estim", "relK", "relM",
                    "bin", "subset",
                    "preLeafInst", "preNodeInst", "preNodeMaj",
                    "preLeafInstP", "preNodeInstP", "preNodeMajP",
                    "postMaj", "postMPruning", "postM"]

    measures = (("Information Gain", "infoGain"), ("Gain Ratio", "gainRatio"), ("Gini Index", "gini"), ("ReliefF", "relief"))
    
    def __init__(self, parent=None, signalManager = None, name='Classification Tree'):
        OWWidget.__init__(self, parent, signalManager, name)

        self.inputs = [("Classified Examples", ExampleTableWithClass, self.dataset)]
        self.outputs = [("Learner", orange.TreeLearner),("Classification Tree", orange.TreeClassifier)]

        self.name = 'Classification Tree'
        self.estim = 0; self.relK = 5; self.relM = 100; self.limitRef = True
        self.bin = 0; self.subset = 0
        self.preLeafInstP = 2; self.preNodeInstP = 5; self.preNodeMajP = 95
        self.preLeafInst = 1; self.preNodeInst = 0; self.preNodeMaj = 0
        self.postMaj = 1; self.postMPruning = 1; self.postM = 2.0
        
        self.loadSettings()
        
        self.data = None
        self.preprocessor = None
        self.setLearner()
        
        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)
        
        qBox = QVGroupBox(self.controlArea)
        qBox.setTitle('Attribute selection criterion')

        self.qMea = OWGUI.comboBox(qBox, self, "estim", items = [m[0] for m in self.measures], callback = self.measureChanged)
        
        b1 = QHBox(qBox)
        OWGUI.separator(b1, 16, 0)
        b2 = QVBox(b1)
        self.cbLimitRef, self.hbxRel1 = OWGUI.checkWithSpin(b2, self, "Limit the number of reference examples to ", 1, 1000, "limitRef", "relM")
        OWGUI.separator(b2)
        self.hbxRel2 = OWGUI.spin(b2, self, "relK", 1, 50, orientation="horizontal", label="Number of neighbours in ReliefF  ")
        OWGUI.separator(self.controlArea)
        
        OWGUI.checkBox(self.controlArea, self, 'bin', 'Binarization', box='Tree Structure')
        OWGUI.separator(self.controlArea)

        self.measureChanged()

        self.pBox = QVGroupBox(self.controlArea)
        self.pBox.setTitle('Pre-Pruning')

        self.preLeafInstBox, self.preLeafInstPBox = OWGUI.checkWithSpin(self.pBox, self, "Min. instances in leaves: ", 1, 1000, "preLeafInst", "preLeafInstP")
        self.preNodeInstBox, self.preNodeInstPBox = OWGUI.checkWithSpin(self.pBox, self, "Stop splitting nodes with ", 1, 1000, "preNodeInst", "preNodeInstP", " or fewer instances")
        self.preNodeMajBox, self.preNodeMajPBox = OWGUI.checkWithSpin(self.pBox, self, "Stop splitting nodes with ", 1, 100, "preNodeMaj", "preNodeMajP", "% of majority class")
        
        OWGUI.separator(self.controlArea)
        self.mBox = QVGroupBox(self.controlArea)

        self.mBox.setTitle('Post-Pruning')
        OWGUI.checkBox(self.mBox, self, 'postMaj', 'Recursively merge leaves with same majority class')
        self.postMPruningBox, self.postMPruningPBox = OWGUI.checkWithSpin(self.mBox, self, "Pruning with m-estimate, m=", 0, 1000, 'postMPruning', 'postM')

        OWGUI.separator(self.controlArea)
        self.btnApply = OWGUI.button(self.controlArea, self, "&Apply Changes", callback = self.setLearner, disabled=0)

        self.resize(100,400)

    def setLearner(self):
        if hasattr(self, "btnApply"):
            self.btnApply.setFocus()
        self.learner = orngTree.TreeLearner(measure = self.measures[self.estim][1],
            reliefK = self.relK, reliefM = self.limitRef and self.relM or -1,
            binarization = self.bin,
            minExamples = self.preNodeInst and self.preNodeInstP,
            minSubset = self.preLeafInst and self.preLeafInstP,
            maxMajority = self.preNodeMaj and self.preNodeMajP/100.0 or 1.0,
            sameMajorityPruning = self.postMaj,
            mForPruning = self.postMPruning and self.postM,
            storeExamples = 1).instance()
                                   
        self.learner.name = self.name
        self.send("Learner", self.learner)

        self.error()
        if self.data:
            if not self.data.domain.classVar:
                self.error("This data set has no class.")
                self.classifier = None
            elif self.data.domain.classVar.varType != orange.VarTypes.Discrete:
                self.error("This algorithm only works with discrete classes.\nThere is another algorithm for regression classes.")
                self.classifier = None
            else:
                try:
                    self.classifier = self.learner(self.data)
                    self.classifier.name = self.name
                except Exception, (errValue):
                    self.error(str(errValue))
                    self.classifier = None
        else:
            self.classifier = None

        self.send("Classification Tree", self.classifier)


    def measureChanged(self):
        relief = self.estim == 3
        self.hbxRel1.setEnabled(relief and self.limitRef)
        self.hbxRel2.setEnabled(relief)
        self.cbLimitRef.setEnabled(relief)
        
    def dataset(self,data):
        self.data = data
        if self.data:
            self.setLearner()
        else:
            self.send("Classification Tree", None)

##############################################################################
# Test the widget, run from DOS prompt
# > python OWDataTable.py)
# Make sure that a sample data set (adult_sample.tab) is in the directory

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

    d = orange.ExampleTable('adult_sample')
    ow.dataset(d)

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

⌨️ 快捷键说明

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