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

📄 owpolyvizgraph.py

📁 orange源码 数据挖掘技术
💻 PY
📖 第 1 页 / 共 4 页
字号:
                    marker = None
                    if self.tooltipValue == TOOLTIPS_SHOW_DATA:
                        marker = self.addMarker(str(self.rawdata[index][self.shownAttributes[i]]), (x_i + xAnchors[i])/2.0, (y_i + yAnchors[i])/2.0, Qt.AlignVCenter + Qt.AlignHCenter, bold = 1)
                    elif self.tooltipValue == TOOLTIPS_SHOW_SPRINGS:
                        marker = self.addMarker("%.3f" % (self.scaledData[self.attributeNameIndex[self.shownAttributes[i]]][index]), (x_i + xAnchors[i])/2.0, (y_i + yAnchors[i])/2.0, Qt.AlignVCenter + Qt.AlignHCenter, bold = 1)
                    font = self.markerFont(marker)
                    font.setPointSize(12)
                    self.setMarkerFont(marker, font)
                    self.tooltipMarkers.append(marker)
                    
            elif self.tooltipKind == VISIBLE_ATTRIBUTES or self.tooltipKind == ALL_ATTRIBUTES:
                if self.tooltipKind == VISIBLE_ATTRIBUTES: labels = self.shownAttributes
                else:                                      labels = self.attributeNames

                text = self.getExampleTooltipText(self.rawdata, self.rawdata[index], labels)
                self.showTip(self.transform(QwtPlot.xBottom, x_i), self.transform(QwtPlot.yLeft, y_i), text)

        OWGraph.onMouseMoved(self, e)
        self.update()
        

    def generateAttrReverseLists(self, attrList, fullAttribList, tempList):
        if attrList == []: return tempList
        tempList2 = deepcopy(tempList)
        index = fullAttribList.index(attrList[0])
        for list in tempList2: list[index] = 1
        return self.generateAttrReverseLists(attrList[1:], fullAttribList, tempList + tempList2)

   
    # save projection (xAttr, yAttr, classVal) into a filename fileName
    def saveProjectionAsTabData(self, fileName, attrList):
        orange.saveTabDelimited(fileName, self.createProjectionAsExampleTable([self.attributeNameIndex[i] for i in attrList], settingsDict = {}))


    # ####################################
    # send 2 example tables. in first is the data that is inside selected rects (polygons), in the second is unselected data
    def getSelectionsAsExampleTables(self, attrList, addProjectedPositions = 0):
        if not self.rawdata: return (None, None)
        if addProjectedPositions == 0 and not self.selectionCurveKeyList: return (None, self.rawdata)       # if no selections exist

        xAttr = orange.FloatVariable("X Positions")
        yAttr = orange.FloatVariable("Y Positions")
        if addProjectedPositions == 1:
            domain=orange.Domain([xAttr,yAttr] + [v for v in self.rawdata.domain.variables])
        elif addProjectedPositions == 2:
            domain=orange.Domain(self.rawdata.domain)
            domain.addmeta(orange.newmetaid(), xAttr)
            domain.addmeta(orange.newmetaid(), yAttr)
        else:
            domain = orange.Domain(self.rawdata.domain)

        domain.addmetas(self.rawdata.domain.getmetas())

        attrIndices = [self.attributeNameIndex[attr] for attr in attrList]
        validData = self.getValidList(attrIndices)
        
        array = self.createProjectionAsNumericArray(attrIndices, settingsDict = {"validData": validData, "scaleFactor": self.scaleFactor, "removeMissingData": 0})
        selIndices, unselIndices = self.getSelectionsAsIndices(attrList, validData)
                 
        if addProjectedPositions:
            selected = orange.ExampleTable(domain, self.rawdata.selectref(selIndices))
            unselected = orange.ExampleTable(domain, self.rawdata.selectref(unselIndices))
            selIndex = 0; unselIndex = 0
            for i in range(len(selIndices)):
                if selIndices[i]:
                    selected[selIndex][xAttr] = array[i][0]
                    selected[selIndex][yAttr] = array[i][1]
                    selIndex += 1
                else:
                    unselected[unselIndex][xAttr] = array[i][0]
                    unselected[unselIndex][yAttr] = array[i][1]
                    unselIndex += 1
        else:
            selected = self.rawdata.selectref(selIndices)
            unselected = self.rawdata.selectref(unselIndices)

        if len(selected) == 0: selected = None
        if len(unselected) == 0: unselected = None
        return (selected, unselected)
    

    def getSelectionsAsIndices(self, attrList, validData = None):
        if not self.rawdata: return [], []

        attrIndices = [self.attributeNameIndex[attr] for attr in attrList]
        if not validData: validData = self.getValidList(attrIndices)
        
        array = self.createProjectionAsNumericArray(attrIndices, settingsDict = {"validData": validData, "scaleFactor": self.scaleFactor, "removeMissingData": 0})
        array = Numeric.transpose(array)
        return self.getSelectedPoints(array[0], array[1], validData)
    


    def createCombinations(self, attrList, count):
        if count > len(attrList): return []
        answer = []
        indices = range(count)
        indices[-1] = indices[-1] - 1
        while 1:
            limit = len(attrList) - 1
            i = count - 1
            while i >= 0 and indices[i] == limit:
                i = i - 1
                limit = limit - 1
            if i < 0: break

            val = indices[i]
            for i in xrange( i, count ):
                val = val + 1

                indices[i] = val
            temp = []
            for i in indices:
                temp.append( attrList[i] )
            answer.append( temp )
        return answer
    

    def createAttrReverseList(self, attrLen):
        res = [[]]
        for i in range(attrLen):
            res2 = deepcopy(res)
            for l in res: l.append(0)
            for l in res2: l.append(1)
            res += res2
        return res

    # #######################################
    # try to find the optimal attribute order by trying all diferent circular permutations
    # and calculating a variation of mean K nearest neighbours to evaluate the permutation
    #def getOptimalSeparation(self, attrListLength, attrReverseDict, projections, addResultFunct):
    def getOptimalSeparation(self, attributes, minLength, maxLength, attrReverseDict, addResultFunct):
        dataSize = len(self.rawdata)
        self.triedPossibilities = 0
        startTime = time.time()
        self.polyvizWidget.progressBarInit()

        domain = orange.Domain([orange.FloatVariable("xVar"), orange.FloatVariable("yVar"), self.rawdata.domain.classVar])
        classListFull = Numeric.transpose(self.rawdata.toNumeric("c")[0])[0]
        allAttrReverse = {}    # dictionary where keys are the number of attributes and the values are dictionaries with all reverse orders for this number of attributes
        anchorList = [(self.createXAnchors(i), self.createYAnchors(i)) for i in range(minLength, maxLength+1)]

        for z in range(minLength-1, len(attributes)):
            for u in range(minLength-1, maxLength):
                attrListLength = u+1

                combinations = self.createCombinations(attributes[:z], u)

                if attrReverseDict == None:
                    if not allAttrReverse.has_key(attrListLength):
                        allAttrReverse[attrListLength] = self.createAttrReverseList(attrListLength)
                    attrReverse = allAttrReverse[attrListLength]

                XAnchors = anchorList[u+1-minLength][0]
                YAnchors = anchorList[u+1-minLength][1]
                
                for attrList in combinations:
                    attrs = attrList + [attributes[z]] # remove the value of this attribute subset
                    indices = [self.attributeNameIndex[attr] for attr in attrs]

                    indPermutations = {}
                    getPermutationList(indices, [], indPermutations, attrReverseDict == None)

                    if attrReverseDict != None: # if we received a dictionary, then we don't reverse attributes 
                        attrReverse = [[attrReverseDict[attr] for attr in attrs]]

                    permutationIndex = 0 # current permutation index
                    totalPermutations = len(indPermutations.values())*len(attrReverse)

                    validData = self.getValidList(indices)
                    classList = Numeric.compress(validData, classListFull)
                    selectedData = Numeric.compress(validData, Numeric.take(self.noJitteringScaledData, indices))
                    sum_i = self._getSum_i(selectedData)

                    tempList = []

                    # for every permutation compute how good it separates different classes            
                    for permutation in indPermutations.values():
                        for attrOrder in attrReverse:
                            if self.kNNOptimization.isOptimizationCanceled():
                                secs = time.time() - startTime
                                self.kNNOptimization.setStatusBarText("Evaluation stopped (evaluated %s projections in %d min, %d sec)" % (orngVisFuncts.createStringFromNumber(self.triedPossibilities), secs/60, secs%60))
                                self.polyvizWidget.progressBarFinished()
                                return
                            permutationIndex += 1

                            table = self.createProjectionAsExampleTable(permutation, settingsDict = {"reverse": attrOrder, "validData": validData, "classList": classList, "sum_i": sum_i, "XAnchors": XAnchors, "YAnchors": YAnchors, "domain": domain})
                            accuracy, other_results = self.kNNOptimization.kNNComputeAccuracy(table)
                        
                            # save the permutation
                            if not self.onlyOnePerSubset:
                                addResultFunct(accuracy, other_results, len(table), [self.attributeNames[i] for i in permutation], self.triedPossibilities, generalDict = {"reverse": attrOrder})
                            else:
                                tempList.append((accuracy, other_results, len(table), [self.attributeNames[val] for val in permutation], attrOrder))
                                
                            self.triedPossibilities += 1
                            self.polyvizWidget.progressBarSet(100.0*self.triedPossibilities/float(self.totalPossibilities))
                            self.kNNOptimization.setStatusBarText("Evaluated %s projections..." % (orngVisFuncts.createStringFromNumber(self.triedPossibilities)))

                    if self.onlyOnePerSubset:
                        (acc, other_results, lenTable, attrList, order) = self.kNNOptimization.getMaxFunct()(tempList)
                        addResultFunct(acc, other_results, lenTable, attrList, self.triedPossibilities, generalDict = {"reverse": order})

        secs = time.time() - startTime
        self.kNNOptimization.setStatusBarText("Finished evaluation (evaluated %s projections in %d min, %d sec)" % (orngVisFuncts.createStringFromNumber(self.triedPossibilities), secs/60, secs%60))
        self.polyvizWidget.progressBarFinished()


    def getOptimalSeparationUsingHeuristicSearch(self, attributes, attrsByClass, minLength, maxLength, attrReverseDict, addResultFunct):
        # variables and domain for the table

⌨️ 快捷键说明

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