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

📄 owpolyvizgraph.py

📁 orange源码 数据挖掘技术
💻 PY
📖 第 1 页 / 共 4 页
字号:
        xVar = orange.FloatVariable("xVar")
        yVar = orange.FloatVariable("yVar")
        domain = orange.Domain([xVar, yVar, self.rawdata.domain.classVar])
        self.triedPossibilities = 0

        # replace attribute names with indices in domain - faster searching
        attributes = [self.attributeNameIndex[name] for name in attributes]
        attrsByClass = [[self.attributeNameIndex[name] for name in arr] for arr in attrsByClass]
        if attrReverseDict != None:
            d = {}
            for key in attrReverseDict.keys():
                d[self.attributeNameIndex[key]] = attrReverseDict[key]
            attrReverseDict = d

        numClasses = len(self.rawdata.domain.classVar.values)
        anchorList = [(self.createXAnchors(i), self.createYAnchors(i)) for i in range(minLength, maxLength+1)]
        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
        startTime = time.time()

        for z in range(minLength-1, len(attributes)):
            for u in range(minLength-1, maxLength):
                projs = orngVisFuncts.createProjections(numClasses, u+1)
                attrListLength = u+1
                
                combinations = orngVisFuncts.combinations(range(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 comb in combinations:
                    comb = comb + [z]  # remove the value of this attribute subset
                    counts = [0 for i in range(numClasses)]
                    for v in comb: counts[v%numClasses] += 1
                    if min(counts) < (u+1) / numClasses: continue   # ignore combinations that don't have good attributes for all class values

                    attrList = [[] for i in range(numClasses)]
                    for v in comb:
                        attrList[v%numClasses].append(attributes[v])

                    attrs = [attributes[c] for c in comb]

                    indPermutations = {}
                    getPermutationList(attrs, [], 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(attrs)
                    classList = Numeric.compress(validData, classListFull)
                    selectedData = Numeric.compress(validData, Numeric.take(self.noJitteringScaledData, attrs))
                    sum_i = self._getSum_i(selectedData)

                    tempList = []

                    # for every permutation compute how good it separates different classes
                    for proj in projs:
                        try:
                            permutation = [attrList[i][j] for (i,j) in proj]

                            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))
                                    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.kNNOptimization.setStatusBarText("Evaluated %s projections (%d attributes)..." % (orngVisFuncts.createStringFromNumber(self.triedPossibilities), z))
                        except:
                            pass
                            
                    if self.onlyOnePerSubset and tempList:
                        (acc, other_results, lenTable, attrList, attrOrder) = self.kNNOptimization.getMaxFunct()(tempList)
                        addResultFunct(acc, other_results, lenTable, attrList, self.triedPossibilities, generalDict = {"reverse": attrOrder})
               
        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 optimizeGivenProjection(self, projection, attrReverseList, accuracy, attributes, addResultFunct, restartWhenImproved = 0, maxProjectionLen = -1):
        dataSize = len(self.rawdata)
        classIndex = self.attributeNameIndex[self.rawdata.domain.classVar.name]
        self.triedPossibilities = 0

        # replace attribute names with indices in domain - faster searching
        attributes = [self.attributeNameIndex[name] for name in attributes]
        lenOfAttributes = len(attributes)
        projection = [self.attributeNameIndex[name] for name in projection]

        # variables and domain for the table
        domain = orange.Domain([orange.FloatVariable("xVar"), orange.FloatVariable("yVar"), self.rawdata.domain.classVar])
        anchorList = [(self.createXAnchors(i), self.createYAnchors(i)) for i in range(3, 50)]
        classListFull = Numeric.transpose(self.rawdata.toNumeric("c")[0])[0]
        allAttrReverse = {}
        
        optimizedProjection = 1
        while optimizedProjection:
            optimizedProjection = 0
            significantImprovement = 0
            
            # in the first step try to find a better projection by substituting an existent attribute with a new one
            # in the second step try to find a better projection by adding a new attribute to the circle
            for iteration in range(2):
                if (maxProjectionLen != -1 and len(projection) + iteration > maxProjectionLen): continue    
                if iteration == 1 and optimizedProjection: continue # if we already found a better projection with replacing an attribute then don't try to add a new atribute
                strTotalAtts = orngVisFuncts.createStringFromNumber(lenOfAttributes)
                listOfCanditates = []

                if attrReverseList == None:
                    if not allAttrReverse.has_key(len(projection) + iteration):
                        allAttrReverse[len(projection) + iteration] = self.createAttrReverseList(len(projection) + iteration)
                    attrReverse = allAttrReverse[len(projection) + iteration]
                    
                for (attrIndex, attr) in enumerate(attributes):
                    if attr in projection: continue
                    if significantImprovement and restartWhenImproved: break        # if we found a projection that is significantly better than the currently best projection then restart the search with this projection

                    if attrReverseList != None:
                        projections = [(copy(projection), copy(attrReverseList)) for i in range(len(projection))]
                    else:
                        rev = [0 for i in range(len(projection))]
                        projections = [(copy(projection), rev) for i in range(len(projection))]
                        
                    if iteration == 0:  # replace one attribute in each projection with attribute attr
                        count = len(projection)
                        for i in range(count): projections[i][0][i] = attr
                    elif iteration == 1:
                        count = len(projection) + 1
                        for i in range(count-1):
                            projections[i][0].insert(i, attr)
                            projections[i][1].insert(i, 0)

                    if attrReverseList != None:
                        projections2 = deepcopy(projections)
                        for i in range(len(projections2)):
                            projections2[i][1][i] = 1 - projections2[i][1][i]
                        projections += projections2

                    if len(anchorList) < count-3: anchorList.append((self.createXAnchors(count), self.createYAnchors(count)))

                    XAnchors = anchorList[count-3][0]
                    YAnchors = anchorList[count-3][1]
                    validData = self.getValidList(projections[0][0])
                    classList = Numeric.compress(validData, classListFull)
                    
                    tempList = []
                    for (testProj, reverse) in projections:
                        if self.kNNOptimization.isOptimizationCanceled(): return

                        table = self.createProjectionAsExampleTable(testProj, settingsDict = {"reverse": reverse, "validData": validData, "classList": classList, "XAnchors": XAnchors, "YAnchors": YAnchors, "domain": domain})
                        acc, other_results = self.kNNOptimization.kNNComputeAccuracy(table)
                        
                        # save the permutation
                        tempList.append((acc, other_results, len(table), testProj, reverse))

                        self.triedPossibilities += 1
                        qApp.processEvents()        # allow processing of other events
                        if self.kNNOptimization.isOptimizationCanceled(): return
                        

                    # return only the best attribute placements
                    (acc, other_results, lenTable, attrList, reverse) = self.kNNOptimization.getMaxFunct()(tempList)
                    if self.kNNOptimization.getMaxFunct()(acc, accuracy) == acc:
                        addResultFunct(acc, other_results, lenTable, [self.attributeNames[i] for i in attrList], 0, generalDict = {"reverse": reverse})
                        self.kNNOptimization.setStatusBarText("Found a better projection with accuracy: %2.2f%%" % (acc))
                        optimizedProjection = 1
                        listOfCanditates.append((acc, attrList, reverse))
                        if max(acc, accuracy)/min(acc, accuracy) > 1.01: significantImprovement = 1
                    else:
                        self.kNNOptimization.setStatusBarText("Evaluated %s projections (attribute %s/%s). Last accuracy was: %2.2f%%" % (orngVisFuncts.createStringFromNumber(self.triedPossibilities), orngVisFuncts.createStringFromNumber(attrIndex), strTotalAtts, acc))
                        if min(acc, accuracy)/max(acc, accuracy) > 0.98:  # if the found projection is at least 98% as good as the one optimized, add it to the list of projections anyway
                            addResultFunct(acc, other_results, lenTable, [self.attributeNames[i] for i in attrList], 1, generalDict = {"reverse": reverse})


                # select the best new projection and say this is now our new projection to optimize    
                if len(listOfCanditates) > 0:
                    (accuracy, projection, reverse) = self.kNNOptimization.getMaxFunct()(listOfCanditates)
                    if attrReverseList != None: attrReverseList = reverse
                    self.kNNOptimization.setStatusBarText("Increased accuracy to %2.2f%%" % (accuracy))


    
if __name__== "__main__":
    #Draw a simple graph
    a = QApplication(sys.argv)        
    c = OWPolyvizGraph()
        
    a.setMainWidget(c)
    c.show()
    a.exec_loop()

⌨️ 快捷键说明

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