📄 boundarypanel.java
字号:
break abortPlot; } pixelMidX = getMidX(j); double sumOfWeights = 0; double [] sumOfProbs = new double [m_trainingData.classAttribute().numValues()]; for (int z = 0; z < samplesPerPixel; z++) { weightingAttsValues[m_xAttribute] = pixelMidX; weightingAttsValues[m_yAttribute] = pixelMidY; m_dataGenerator.setWeightingValues(weightingAttsValues); Instance newInst = m_dataGenerator.generateInstanceFast(); sumOfWeights += newInst.weight(); int index = 0; for (int k = 0; k < predInst.numAttributes(); k++) { if (k != m_trainingData.classIndex()) { vals[k] = newInst.value(index); index++; } } // classify the instance dist = m_classifier.distributionForInstance(predInst); for (int k = 0; k < sumOfProbs.length; k++) { sumOfProbs[k] += (dist[k] * newInst.weight()); } } // average Utils.normalize(sumOfProbs, sumOfWeights); // plot the point Graphics osg = m_osi.getGraphics(); Graphics g = m_plotPanel.getGraphics(); float [] colVal = new float[3]; for (int k = 0; k < 3; k++) { if (k < sumOfProbs.length) { if (m_rgbClassValues[k] != -1) { colVal[k] = (float)sumOfProbs[m_rgbClassValues[k]]; } } if (colVal[k] < 0) { colVal[k] = 0; } if (colVal[k] > 1) { colVal[k] = 1; } } osg.setColor(new Color(colVal[0], colVal[1], colVal[2])); osg.drawLine(j,i,j,i); if (j == 0) { g.drawImage(m_osi,0,0,m_plotPanel); } } } } catch (Exception ex) { ex.printStackTrace(); } finally { m_plotThread = null; // notify any listeners that we are finished Vector l; ActionEvent e = new ActionEvent(this, 0, ""); synchronized(this) { l = (Vector)m_listeners.clone(); } for (int i = 0; i < l.size(); i++) { ActionListener al = (ActionListener)l.elementAt(i); al.actionPerformed(e); } } } }; m_plotThread.setPriority(Thread.MIN_PRIORITY); m_plotThread.start(); } } /** * Set how many samples to take from each generating model for use in * computing the colour of a pixel * * @param ns the number of samples to use from each generating model */ public void setNumberOfSamplesFromEachGeneratingModel(int ns) { if (ns >= 1) { m_numberOfSamplesFromEachGeneratingModel = ns; } } /** * Set the training data to use * * @param trainingData the training data * @exception Exception if an error occurs */ public void setTrainingData(Instances trainingData) throws Exception { m_trainingData = trainingData; if (m_trainingData.classIndex() < 0) { throw new Exception("No class attribute set (BoundaryPanel)"); } m_classIndex = m_trainingData.classIndex(); } /** * Register a listener to be notified when plotting completes * * @param newListener the listener to add */ public void addActionListener(ActionListener newListener) { m_listeners.add(newListener); } /** * Remove a listener * * @param removeListener the listener to remove */ public void removeActionListener(ActionListener removeListener) { m_listeners.removeElement(removeListener); } /** * Set the classifier to use. * * @param classifier the classifier to use */ public void setClassifier(DistributionClassifier classifier) { m_classifier = classifier; } /** * Set the data generator to use for generating new instances * * @param dataGenerator the data generator to use */ public void setDataGenerator(DataGenerator dataGenerator) { m_dataGenerator = dataGenerator; } /** * Set the class value index for the red colour * * @param classVal an <code>int</code> value * @exception Exception if an error occurs */ public void setRedClassValue(int classVal) throws Exception { setClassValue(0, classVal); } /** * Set the class value index for the green colour * * @param classVal an <code>int</code> value * @exception Exception if an error occurs */ public void setGreenClassValue(int classVal) throws Exception { setClassValue(1, classVal); } /** * Set the class value index for the blue colour * * @param classVal an <code>int</code> value * @exception Exception if an error occurs */ public void setBlueClassValue(int classVal) throws Exception { setClassValue(2, classVal); } /** * Set a class value for a particular colour (RGB) * * @param index the colour - 0 = red, 1 = green, 2 = blue * @param classVal the class value index to associate with the colour * @exception Exception if an error occurs */ private void setClassValue(int index, int classVal) throws Exception { if (m_trainingData == null) { throw new Exception("No training data set (BoundaryPanel)"); } if (classVal < 0 || classVal > m_trainingData.classAttribute().numValues()) { throw new Exception("Class value out of range (BoundaryPanel)"); } m_rgbClassValues[index] = classVal; } /** * Set the x attribute index * * @param xatt index of the attribute to use on the x axis * @exception Exception if an error occurs */ public void setXAttribute(int xatt) throws Exception { if (m_trainingData == null) { throw new Exception("No training data set (BoundaryPanel)"); } if (xatt < 0 || xatt > m_trainingData.numAttributes()) { throw new Exception("X attribute out of range (BoundaryPanel)"); } if (m_trainingData.attribute(xatt).isNominal()) { throw new Exception("Visualization dimensions must be numeric " +"(BoundaryPanel)"); } if (m_trainingData.numDistinctValues(xatt) < 2) { throw new Exception("Too few distinct values for X attribute " +"(BoundaryPanel)"); } m_xAttribute = xatt; } /** * Set the y attribute index * * @param yatt index of the attribute to use on the y axis * @exception Exception if an error occurs */ public void setYAttribute(int yatt) throws Exception { if (m_trainingData == null) { throw new Exception("No training data set (BoundaryPanel)"); } if (yatt < 0 || yatt > m_trainingData.numAttributes()) { throw new Exception("X attribute out of range (BoundaryPanel)"); } if (m_trainingData.attribute(yatt).isNominal()) { throw new Exception("Visualization dimensions must be numeric " +"(BoundaryPanel)"); } if (m_trainingData.numDistinctValues(yatt) < 2) { throw new Exception("Too few distinct values for Y attribute " +"(BoundaryPanel)"); } m_yAttribute = yatt; } /** * Main method for testing this class * * @param args a <code>String[]</code> value */ public static void main (String [] args) { try { if (args.length < 7) { System.err.println("Usage : BoundaryPanel <dataset> " +"<class col> <Red classVal(index)> " +"<Green classVal(index)> " +"<Blue classVal(index)> <xAtt> <yAtt>"); System.exit(1); } final javax.swing.JFrame jf = new javax.swing.JFrame("Weka classification boundary visualizer"); jf.getContentPane().setLayout(new BorderLayout()); final BoundaryPanel bv = new BoundaryPanel(200,200); jf.getContentPane().add(bv, BorderLayout.CENTER); jf.setSize(bv.getMinimumSize()); // jf.setSize(200,200); jf.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { jf.dispose(); System.exit(0); } }); jf.pack(); jf.setVisible(true); // bv.initialize(); bv.repaint(); System.err.println("Loading instances from : "+args[0]); java.io.Reader r = new java.io.BufferedReader( new java.io.FileReader(args[0])); Instances i = new Instances(r); i.setClassIndex(Integer.parseInt(args[1])); bv.setTrainingData(i); bv.setClassifier(new Logistic()); bv.setDataGenerator(new KDDataGenerator()); bv.setRedClassValue(Integer.parseInt(args[2])); bv.setGreenClassValue(Integer.parseInt(args[3])); bv.setBlueClassValue(Integer.parseInt(args[4])); bv.setXAttribute(Integer.parseInt(args[5])); bv.setYAttribute(Integer.parseInt(args[6])); bv.start(); } catch (Exception ex) { ex.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -