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

📄 owassociationrulesviewer.py

📁 orange源码 数据挖掘技术
💻 PY
📖 第 1 页 / 共 2 页
字号:
            self.grid.addWidget(cb.parentWidget(), i % 2, i / 2)

        OWGUI.separator(mainRight, 0, 4)
        
        self.edtRules = QMultiLineEdit(mainRight)
        self.edtRules.setReadOnly(True)
        self.edtRules.setSizePolicy(QSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding))

        bottom = QWidget(mainRight)
        bottomGrid = QGridLayout(bottom, 2, 2, 5, 5)

        self.saveButton = OWGUI.button(bottom, self, "Save Rules", callback = self.saveRules)
        commitButton = OWGUI.button(bottom, self, "Send Rules", callback = self.sendRules)        
        autoSend = OWGUI.checkBox(bottom, self, "autoSend", "Send rules automatically", disables=[(-1, commitButton)])
        autoSend.makeConsistent()

        bottomGrid.addWidget(self.saveButton, 1, 0)
        bottomGrid.addWidget(autoSend.parentWidget(), 0, 1)
        bottomGrid.addWidget(commitButton, 1, 1)


        self.controlArea.setFixedSize(0, 0)
        self.resize(800, 380)

    
    def checkScale(self):
        if self.supp_min == self.supp_max:
            self.supp_max += 0.01
        if self.conf_max == self.conf_min:
            self.conf_max += 0.01
        self.suppInCell = (self.supp_max - self.supp_min) / self.numcols
        self.confInCell = (self.conf_max - self.conf_min) / self.numrows


    def unselect(self):
        self.sel_colmin = self.sel_colmax = self.sel_rowmin = self.sel_rowmax = -1

        self.selectedRules = []
        for row in self.ingrid:
            for cell in row:
                for rule in cell:
                    self.selectedRules.append(rule)

        self.displayRules()                
        if hasattr(self, "selConfidence"):
            self.updateConfSupp()

        if hasattr(self, "ruleCanvas"):
            self.ruleCanvas.unselect()
            self.ruleCanvas.draw()

        self.sendIfAuto()            


    def updateConfSupp(self):
        if self.sel_colmin >= 0:
            smin, cmin = self.coordToSuppConf(self.sel_colmin, self.sel_rowmin)
            smax, cmax = self.coordToSuppConf(self.sel_colmax+1, self.sel_rowmax+1)
        else:
            smin, cmin = self.supp_min, self.conf_min
            smax, cmax = self.supp_max, self.conf_max
            
        self.selConfidence.setText("%3i%% - %3i%%" % (round(100*cmin), round(100*cmax)))
        self.selSupport.setText("%3i%% - %3i%%" % (round(100*smin), round(100*smax)))
        self.selRules.setText("%3i" % len(self.selectedRules))

    # This function doesn't send anything to output! (Shouldn't because it's called by the mouse move event)            
    def updateRuleList(self):
        self.selectedRules = []
        for row in self.ingrid[self.sel_rowmin : self.sel_rowmax+1]:
            for cell in row[self.sel_colmin : self.sel_colmax+1]:
                for rule in cell:
                    self.selectedRules.append(rule)

        self.displayRules()
        self.updateConfSupp()
        self.saveButton.setEnabled(len(self.selectedRules) > 0)

    def displayRules(self):
        if hasattr(self, "edtRules"):
            edtRules = self.edtRules
            edtRules.clear()

            toWrite = [m for m in self.measures if getattr(self, m[2])]
            if toWrite:
                edtRules.append("\t".join([m[1] for m in toWrite]))
            for rule in self.selectedRules:
                self.edtRules.append("\t".join(["%.3f" % getattr(rule, m[2]) for m in toWrite] + [`rule`.replace(" ", "  ")]))


    def saveRules(self):
        fileName = QFileDialog.getSaveFileName( "myRules.txt", "Textfiles (*.txt)", self );
        if not fileName.isNull() :
            f = open(str(fileName), 'w')
            if self.selectedRules:
                toWrite = [m for m in self.measures if getattr(self, m[2])]
                if toWrite:
                    f.write("\t".join([m[1] for m in toWrite]) + "\n")
                for rule in self.selectedRules:
                    f.write("\t".join(["%.3f" % getattr(rule, m[2]) for m in toWrite] + [`rule`.replace(" ", "  ")]) + "\n")


    def setIngrid(self):
        smin, sic, cmin, cic = self.supp_min, self.suppInCell, self.conf_min, self.confInCell
        self.ingrid = [[[] for x in range(self.numcols)] for y in range(self.numrows)]
        if self.rules:
            for r in self.rules:
                self.ingrid[min(self.numrows-1, int((r.confidence - cmin) / cic))][min(self.numcols-1, int((r.support - smin) / sic))].append(r)


    def coordToSuppConf(self, col, row):
        return self.supp_min + col * self.suppInCell, self.conf_min + row * self.confInCell
    
    def zoomButton(self):
        if self.sel_rowmin >= 0:
            # have to compute both at ones!
            self.supp_min, self.conf_min, self.supp_max, self.conf_max = self.coordToSuppConf(self.sel_colmin, self.sel_rowmin) + self.coordToSuppConf(self.sel_colmax+1, self.sel_rowmax+1)
            self.checkScale()

            smin, sic, cmin, cic = self.supp_min, self.suppInCell, self.conf_min, self.confInCell
            newingrid = [[[] for x in range(self.numcols)] for y in range(self.numrows)]
            for row in self.ingrid[self.sel_rowmin : self.sel_rowmax+1]:
                for cell in row[self.sel_colmin : self.sel_colmax+1]:
                    for rule in cell:
                        inrow = (rule.confidence - cmin) / cic
                        if inrow >= 0 and inrow < self.numrows + 1e-3:
                            incol = (rule.support - smin) / sic
                            if incol >= 0 and incol < self.numcols + 1e-3:
                                newingrid[min(int(inrow), self.numrows-1)][min(int(incol), self.numcols-1)].append(rule)
            self.ingrid = newingrid

            self.unselect()
            self.ruleCanvas.draw()
            self.sendIfAuto()


    def rezoom(self, smi, sma, cmi, cma):
        self.supp_min, self.supp_max, self.conf_min, self.conf_max = smi, sma, cmi, cma
        self.checkScale() # to set the inCell
        self.setIngrid()
        self.unselect()
        if hasattr(self, "ruleCanvas"):
            self.ruleCanvas.draw()
        self.sendIfAuto()
        
    def showAllButton(self):
        self.rezoom(self.supp_allmin, self.supp_allmax, self.conf_allmin, self.conf_allmax)

    def noZoomButton(self):
        self.rezoom(0., 1., 0., 1.)
        
    def sendIfAuto(self):
        if self.autoSend:
            self.sendRules()
        
    def sendRules(self):
        self.send("Association Rules", orange.AssociationRules(self.selectedRules))
    
    def arules(self,rules):
        self.rules = rules
        if self.rules:
            self.supp_min = self.conf_min = 1
            self.supp_max = self.conf_max = 0
            for rule in self.rules:
                self.conf_min = min(self.conf_min, rule.confidence)
                self.conf_max = max(self.conf_max, rule.confidence)
                self.supp_min = min(self.supp_min, rule.support)
                self.supp_max = max(self.supp_max, rule.support)
            self.checkScale()
        else:
            self.supp_min, self.supp_max = self.conf_min, self.conf_max = 0., 1.

        self.supp_allmin, self.supp_allmax, self.conf_allmin, self.conf_allmax = self.supp_min, self.supp_max, self.conf_min, self.conf_max
        self.rezoom(self.supp_allmin, self.supp_allmax, self.conf_allmin, self.conf_allmax)


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


    dataset = orange.ExampleTable('../../doc/datasets/car.tab')
    rules=orange.AssociationRulesInducer(dataset, minSupport = 0.3, maxItemSets=15000)
    ow.arules(rules)

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

⌨️ 快捷键说明

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