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

📄 owlinprojgraph.py

📁 orange源码 数据挖掘技术
💻 PY
📖 第 1 页 / 共 4 页
字号:
                shortData = self.createProjectionAsExampleTable([self.attributeNameIndex[attr] for attr in labels], settingsDict = {"useAnchorData": 1})
                predictions, probabilities = self.widget.vizrank.kNNClassifyData(shortData)
                if self.showKNN == 2: insideData, stringData = [1.0 - val for val in predictions], "Probability of wrong classification = %.2f%%"
                else:                 insideData, stringData = predictions, "Probability of correct classification = %.2f%%"
                
            if self.rawdata.domain.classVar.varType == orange.VarTypes.Continuous:  classColors = self.contPalette
            else:                                                                   classColors = self.discPalette

            if len(insideData) != len(self.rawdata):
                #print "Warning: The information that was supposed to be used for coloring of points is not of the same size as the original data. Numer of data examples: %d, number of color data: %d" % (len(self.rawdata), len(self.insideColors))
                j = 0
                for i in range(len(self.rawdata)):
                    if not validData[i]: continue
                    fillColor = classColors.getColor(classValueIndices[self.rawdata[i].getclass().value], 255*insideData[j])
                    edgeColor = classColors.getColor(classValueIndices[self.rawdata[i].getclass().value])
                    key = self.addCurve(str(i), fillColor, edgeColor, self.pointWidth, xData = [x_positions[i]], yData = [y_positions[i]])
                    self.addTooltipKey(x_positions[i], y_positions[i], edgeColor, i, stringData % (100*insideData[j]))
                    j+= 1
            else:
                for i in range(len(self.rawdata)):
                    if not validData[i]: continue
                    fillColor = classColors.getColor(classValueIndices[self.rawdata[i].getclass().value], 255*insideData[i])
                    edgeColor = classColors.getColor(classValueIndices[self.rawdata[i].getclass().value])
                    key = self.addCurve(str(i), fillColor, edgeColor, self.pointWidth, xData = [x_positions[i]], yData = [y_positions[i]])
                    self.addTooltipKey(x_positions[i], y_positions[i], edgeColor, i, stringData % (100*insideData[i]))

        # ############################################################## 
        # do we have a subset data to show?
        # ##############################################################
        elif haveSubsetData:
            shownSubsetCount = 0
            subsetReferencesToDraw = [example.reference() for example in self.subsetData]
            
            # draw the rawdata data set. examples that exist also in the subset data draw full, other empty
            for i in range(dataSize):
                showFilled = self.rawdata[i].reference() in subsetReferencesToDraw
                if showFilled:
                    shownSubsetCount += 1
                    subsetReferencesToDraw.remove(self.rawdata[i].reference())
                
                if not validData[i]: continue
                if self.rawdata.domain.classVar.varType == orange.VarTypes.Discrete:
                    newColor = self.discPalette[classValueIndices[self.rawdata[i].getclass().value]]
                else:
                    newColor = self.contPalette[self.noJitteringScaledData[classNameIndex][i]]

                if self.useDifferentSymbols: curveSymbol = self.curveSymbols[classValueIndices[self.rawdata[i].getclass().value]]
                else: curveSymbol = self.curveSymbols[0]
                
                key = self.addCurve(str(i), newColor, newColor, self.pointWidth, symbol = curveSymbol, xData = [x_positions[i]], yData = [y_positions[i]], showFilledSymbols = showFilled)
                self.addTooltipKey(x_positions[i], y_positions[i], newColor, i)

            # if we have a data subset that contains examples that don't exist in the original dataset we show them here
            if shownSubsetCount < len(self.subsetData):
                XAnchors = Numeric.array([val[0] for val in self.anchorData])
                YAnchors = Numeric.array([val[1] for val in self.anchorData])
                anchorRadius = Numeric.sqrt(XAnchors*XAnchors + YAnchors*YAnchors)
                
                for i in range(len(self.subsetData)):
                    if not self.subsetData[i].reference() in subsetReferencesToDraw: continue
                    subsetReferencesToDraw.remove(self.subsetData[i].reference())

                    # check if has missing values
                    if 1 in [self.subsetData[i][ind].isSpecial() for ind in indices]: continue
                    
                    # scale data values for example i
                    dataVals = [self.scaleExampleValue(self.subsetData[i], ind) for ind in indices]

                    [x,y] = self.getProjectedPointPosition(indices, dataVals, settingsDict = {"useAnchorData": 1, "anchorRadius" : anchorRadius})  # compute position of the point
            
                    if not self.subsetData[i].getclass().isSpecial():
                        if self.subsetData.domain.classVar.varType == orange.VarTypes.Discrete:
                            newColor = self.discPalette[classValueIndices[self.subsetData[i].getclass().value]]
                        else:
                            newColor = self.contPalette[self.scaleExampleValue(self.subsetData[i], classNameIndex)]
                    else:
                        newColor = QColor(0,0,0)

                    if self.useDifferentSymbols: curveSymbol = self.curveSymbols[classValueIndices[self.subsetData[i].getclass().value]]
                    else: curveSymbol = self.curveSymbols[0]
                    self.addCurve("", newColor, newColor, self.pointWidth, symbol = curveSymbol, xData = [x], yData = [y], showFilledSymbols = 1)
                
        # ############################################################## 
        # CONTINUOUS class
        # ############################################################## 
        elif self.rawdata.domain.classVar.varType == orange.VarTypes.Continuous:
            for i in range(dataSize):
                if not validData[i]: continue
                newColor = self.contPalette[self.noJitteringScaledData[classNameIndex][i]]
                key = self.addCurve(str(i), newColor, newColor, self.pointWidth, symbol = QwtSymbol.Ellipse, xData = [x_positions[i]], yData = [y_positions[i]])
                self.addTooltipKey(x_positions[i], y_positions[i], newColor, i)

        # ############################################################## 
        # DISCRETE class + optimize drawing
        # ############################################################## 
        elif self.optimizedDrawing:
            pos = [[ [] , [], [] ] for i in range(valLen)]
            for i in range(dataSize):
                if not validData[i]: continue
                index = classValueIndices[self.rawdata[i].getclass().value]
                pos[index][0].append(x_positions[i])
                pos[index][1].append(y_positions[i])
                pos[index][2].append(i)

            for i in range(valLen):
                if self.useDifferentColors: newColor = self.discPalette[i]
                else:                       newColor = QColor(0,0,0)
                
                if self.useDifferentSymbols: curveSymbol = self.curveSymbols[i]
                else: curveSymbol = self.curveSymbols[0]

                key = self.addCurve(str(i), newColor, newColor, self.pointWidth, symbol = curveSymbol, xData = pos[i][0], yData = pos[i][1])
                for k in range(len(pos[i][0])):
                    self.addTooltipKey(pos[i][0][k], pos[i][1][k], newColor, pos[i][2][k])

        # ############################################################## 
        # DISCRETE class
        # ############################################################## 
        elif self.rawdata.domain.classVar.varType == orange.VarTypes.Discrete:
            for i in range(dataSize):
                if not validData[i]: continue
                if self.useDifferentColors: newColor = self.discPalette[classValueIndices[self.rawdata[i].getclass().value]]
                else:                       newColor = QColor(0,0,0)
                if self.useDifferentSymbols: curveSymbol = self.curveSymbols[classValueIndices[self.rawdata[i].getclass().value]]
                else:                        curveSymbol = self.curveSymbols[0]
                self.addCurve(str(i), newColor, newColor, self.pointWidth, symbol = curveSymbol, xData = [x_positions[i]], yData = [y_positions[i]])
                self.addTooltipKey(x_positions[i], y_positions[i], newColor, i)
                    
        # ############################################################## 
        # draw the legend
        # ############################################################## 
        if self.showLegend:
            # show legend for discrete class
            if self.rawdata.domain.classVar.varType == orange.VarTypes.Discrete:
                self.addMarker(self.rawdata.domain.classVar.name, 0.87, 1.06, Qt.AlignLeft)
                    
                classVariableValues = getVariableValuesSorted(self.rawdata, self.rawdata.domain.classVar.name)
                for index in range(len(classVariableValues)):
                    if self.useDifferentColors: color = self.discPalette[index]
                    else:                       color = QColor(0,0,0)
                    y = 1.0 - index * 0.05

                    if not self.useDifferentSymbols:  curveSymbol = self.curveSymbols[0]
                    else:                             curveSymbol = self.curveSymbols[index]

                    self.addCurve(str(index), color, color, self.pointWidth, symbol = curveSymbol, xData = [0.95], yData = [y])
                    self.addMarker(classVariableValues[index], 0.90, y, Qt.AlignLeft + Qt.AlignVCenter)
            # show legend for continuous class
            else:
                xs = [1.15, 1.20, 1.20, 1.15]
                count = 200
                height = 2 / float(count)
                for i in range(count):
                    y = -1.0 + i*2.0/float(count)
                    col = self.contPalette[i/float(count)]
                    curve = PolygonCurve(self, QPen(col), QBrush(col))
                    newCurveKey = self.insertCurve(curve)
                    self.setCurveData(newCurveKey, xs, [y,y, y+height, y+height])

                # add markers for min and max value of color attribute
                [minVal, maxVal] = self.attrValues[self.rawdata.domain.classVar.name]
                self.addMarker("%s = %%.%df" % (self.rawdata.domain.classVar.name, self.rawdata.domain.classVar.numberOfDecimals) % (minVal), xs[0] - 0.02, -1.0 + 0.04, Qt.AlignLeft)
                self.addMarker("%s = %%.%df" % (self.rawdata.domain.classVar.name, self.rawdata.domain.classVar.numberOfDecimals) % (maxVal), xs[0] - 0.02, +1.0 - 0.04, Qt.AlignLeft)

        self.repaint()  # we have to repaint to update scale to get right coordinates for tooltip rectangles
        self.updateLayout()
        

    # ############################################################## 
    # create a dictionary value for the data point
    # this will enable to show tooltips faster and to make selection of examples available
    def addTooltipKey(self, x, y, color, index, extraString = None):
        dictValue = "%.1f-%.1f"%(x, y)
        if not self.dataMap.has_key(dictValue): self.dataMap[dictValue] = []
        self.dataMap[dictValue].append((x, y, color, index, extraString))

    def showClusterLines(self, attributeIndices, validData, width = 1):
        if self.rawdata.domain.classVar.varType == orange.VarTypes.Continuous: return
        shortData = self.createProjectionAsExampleTable(attributeIndices, settingsDict = {"validData": validData, "scaleFactor": self.scaleFactor})
        classIndices = getVariableValueIndices(self.rawdata, self.attributeNameIndex[self.rawdata.domain.classVar.name])

        (closure, enlargedClosure, classValue) = self.clusterClosure

        if type(closure) == dict:
            for key in closure.keys():
                clusterLines = closure[key]
                colorIndex = classIndices[self.rawdata.domain.classVar[classValue[key]].value]
                for (p1, p2) in clusterLines:
                    self.addCurve("", self.discPalette[colorIndex], self.discPalette[colorIndex], 1, QwtCurve.Lines, QwtSymbol.None, xData = [shortData[p1][0].value, shortData[p2][0].value], yData = [shortData[p1][1].value, shortData[p2][1].value], lineWidth = width)
        else:
            colorIndex = classIndices[self.rawdata.domain.classVar[classValue].value]
            for (p1, p2) in closure:
                self.addCurve("", self.discPalette[colorIndex], self.discPalette[colorIndex], 1, QwtCurve.Lines, QwtSymbol.None, xData = [shortData[p1][0].value, shortData[p2][0].value], yData = [shortData[p1][1].value, shortData[p2][1].value], lineWidth = width)


    def onMousePressed(self, e):
        if self.manualPositioning:
            self.mouseCurrentlyPressed = 1
            if self.anchorsAsVectors:
                key, dist = self.closestMarker(e.x(), e.y())
                if dist < 15:
                    self.selectedAnchorIndex = self.shownLabels.index(self.marker(key).label())
            else:
                (key, dist, foo1, foo2, index) = self.closestCurve(e.x(), e.y())
                if dist < 5 and str(self.curve(key).title()) == "dots":
                    self.selectedAnchorIndex = index
                else:
                    self.selectedAnchorIndex = None
        else:

⌨️ 快捷键说明

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