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

📄 clustererpanel.java

📁 代码是一个分类器的实现,其中使用了部分weka的源代码。可以将项目导入eclipse运行
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
		ignoredAtts[0] = m_ClassCombo.getSelectedIndex();	      } else {		int[] newIgnoredAtts = new int[ignoredAtts.length+1];		System.arraycopy(ignoredAtts, 0, newIgnoredAtts, 0, ignoredAtts.length);		newIgnoredAtts[ignoredAtts.length] = m_ClassCombo.getSelectedIndex();		ignoredAtts = newIgnoredAtts;	      }	    }	    outBuff.append("Test mode:    ");	    switch (testMode) {	      case 3: // Test on training	      outBuff.append("evaluate on training data\n");	      break;	      case 2: // Percent split	      outBuff.append("split " + percent			       + "% train, remainder test\n");	      break;	      case 4: // Test on user split	      outBuff.append("user supplied test set: "			     + userTest.numInstances() + " instances\n");	      break;	    case 5: // Classes to clusters evaluation on training	      outBuff.append("Classes to clusters evaluation on training data");	      	      break;	    }	    outBuff.append("\n");	    m_History.addResult(name, outBuff);	    m_History.setSingle(name);	    	    // Build the model and output it.	    m_Log.statusMessage("Building model on training data...");	    // remove the class attribute (if set) and build the clusterer	    clusterer.buildClusterer(removeClass(trainInst));	    	    if (testMode == 2) {	      outBuff.append("\n=== Clustering model (full training set) ===\n\n");	    	      outBuff.append(clusterer.toString() + '\n');	    }	    m_History.updateResult(name);	    if (clusterer instanceof Drawable) {	      try {		grph = ((Drawable)clusterer).graph();	      } catch (Exception ex) {	      }	    }	    // copy full model for output	    SerializedObject so = new SerializedObject(clusterer);	    fullClusterer = (Clusterer) so.getObject();	    	    ClusterEvaluation eval = new ClusterEvaluation();	    eval.setClusterer(clusterer);	    switch (testMode) {	      case 3: case 5: // Test on training	      m_Log.statusMessage("Clustering training data...");	      eval.evaluateClusterer(trainInst);	      predData = setUpVisualizableInstances(inst,eval);	      outBuff.append("=== Model and evaluation on training set ===\n\n");	      break;	      case 2: // Percent split	      m_Log.statusMessage("Randomizing instances...");	      inst.randomize(new Random(1));	      trainInst.randomize(new Random(1));	      int trainSize = trainInst.numInstances() * percent / 100;	      int testSize = trainInst.numInstances() - trainSize;	      Instances train = new Instances(trainInst, 0, trainSize);	      Instances test = new Instances(trainInst, trainSize, testSize);	      Instances testVis = new Instances(inst, trainSize, testSize);	      m_Log.statusMessage("Building model on training split...");	      clusterer.buildClusterer(train);	      m_Log.statusMessage("Evaluating on test split...");	      eval.evaluateClusterer(test);	      predData = setUpVisualizableInstances(testVis, eval);	      outBuff.append("=== Model and evaluation on test split ===\n");	      break;			      case 4: // Test on user split	      m_Log.statusMessage("Evaluating on test data...");	      Instances userTestT = new Instances(userTest);	      if (!m_ignoreKeyList.isSelectionEmpty()) {		userTestT = removeIgnoreCols(userTestT);	      }	      eval.evaluateClusterer(userTestT);	      predData = setUpVisualizableInstances(userTest, eval);	      outBuff.append("=== Model and evaluation on test set ===\n");	      break;	      default:	      throw new Exception("Test mode not implemented");	    }	    outBuff.append(eval.clusterResultsToString());	    outBuff.append("\n");	    m_History.updateResult(name);	    m_Log.logMessage("Finished " + cname);	    m_Log.statusMessage("OK");	  } catch (Exception ex) {	    ex.printStackTrace();	    m_Log.logMessage(ex.getMessage());	    JOptionPane.showMessageDialog(ClustererPanel.this,					  "Problem evaluating clusterer:\n"					  + ex.getMessage(),					  "Evaluate clusterer",					  JOptionPane.ERROR_MESSAGE);	    m_Log.statusMessage("Problem evaluating clusterer");	  } finally {	    if (predData != null) {	      m_CurrentVis = new VisualizePanel();	      m_CurrentVis.setName(name+" ("+inst.relationName()+")");	      m_CurrentVis.setLog(m_Log);	      predData.setPlotName(name+" ("+inst.relationName()+")");	      	      try {		m_CurrentVis.addPlot(predData);	      } catch (Exception ex) {		System.err.println(ex);	      }	      FastVector vv = new FastVector();	      vv.addElement(fullClusterer);	      Instances trainHeader = new Instances(m_Instances, 0);	      vv.addElement(trainHeader);	      if (ignoredAtts != null) vv.addElement(ignoredAtts);	      if (saveVis) {		vv.addElement(m_CurrentVis);		if (grph != null) {		  vv.addElement(grph);		}			      }	      m_History.addObject(name, vv);	    }	    if (isInterrupted()) {	      m_Log.logMessage("Interrupted " + cname);	      m_Log.statusMessage("See error log");	    }	    m_RunThread = null;	    m_StartBut.setEnabled(true);	    m_StopBut.setEnabled(false);	    m_ignoreBut.setEnabled(true);	    if (m_Log instanceof TaskLogger) {	      ((TaskLogger)m_Log).taskFinished();	    }	  }	}      };      m_RunThread.setPriority(Thread.MIN_PRIORITY);      m_RunThread.start();    }  }  private Instances removeClass(Instances inst) {    Remove af = new Remove();    Instances retI = null;        try {      if (inst.classIndex() < 0) {	retI = inst;      } else {	af.setAttributeIndices(""+(inst.classIndex()+1));	af.setInvertSelection(false);	af.setInputFormat(inst);	retI = Filter.useFilter(inst, af);      }    } catch (Exception e) {      e.printStackTrace();    }    return retI;  }  private Instances removeIgnoreCols(Instances inst) {        // If the user is doing classes to clusters evaluation and    // they have opted to ignore the class, then unselect the class in    // the ignore list    if (m_ClassesToClustersBut.isSelected()) {      int classIndex = m_ClassCombo.getSelectedIndex();      if (m_ignoreKeyList.isSelectedIndex(classIndex)) {	m_ignoreKeyList.removeSelectionInterval(classIndex, classIndex);      }    }    int [] selected = m_ignoreKeyList.getSelectedIndices();    Remove af = new Remove();    Instances retI = null;    try {      af.setAttributeIndicesArray(selected);      af.setInvertSelection(false);      af.setInputFormat(inst);      retI = Filter.useFilter(inst, af);    } catch (Exception e) {      e.printStackTrace();    }        return retI;  }  private Instances removeIgnoreCols(Instances inst, int[] toIgnore) {    Remove af = new Remove();    Instances retI = null;    try {      af.setAttributeIndicesArray(toIgnore);      af.setInvertSelection(false);      af.setInputFormat(inst);      retI = Filter.useFilter(inst, af);    } catch (Exception e) {      e.printStackTrace();    }        return retI;  }  /**   * Stops the currently running clusterer (if any).   */  protected void stopClusterer() {    if (m_RunThread != null) {      m_RunThread.interrupt();            // This is deprecated (and theoretically the interrupt should do).      m_RunThread.stop();          }  }  /**   * Pops up a TreeVisualizer for the clusterer from the currently   * selected item in the results list   * @param dottyString the description of the tree in dotty format   * @param treeName the title to assign to the display   */  protected void visualizeTree(String dottyString, String treeName) {    final javax.swing.JFrame jf =       new javax.swing.JFrame("Weka Classifier Tree Visualizer: "+treeName);    jf.setSize(500,400);    jf.getContentPane().setLayout(new BorderLayout());    TreeVisualizer tv = new TreeVisualizer(null,					   dottyString,					   new PlaceNode2());    jf.getContentPane().add(tv, BorderLayout.CENTER);    jf.addWindowListener(new java.awt.event.WindowAdapter() {	public void windowClosing(java.awt.event.WindowEvent e) {	  jf.dispose();	}      });        jf.setVisible(true);    tv.fitToScreen();  }  /**   * Pops up a visualize panel to display cluster assignments   * @param sp the visualize panel to display   */  protected void visualizeClusterAssignments(VisualizePanel sp) {    if (sp != null) {      String plotName = sp.getName();      final javax.swing.JFrame jf = 	new javax.swing.JFrame("Weka Clusterer Visualize: "+plotName);      jf.setSize(500,400);      jf.getContentPane().setLayout(new BorderLayout());      jf.getContentPane().add(sp, BorderLayout.CENTER);      jf.addWindowListener(new java.awt.event.WindowAdapter() {	  public void windowClosing(java.awt.event.WindowEvent e) {	    jf.dispose();	  }	});      jf.setVisible(true);    }  }  /**   * Handles constructing a popup menu with visualization options   * @param name the name of the result history list entry clicked on by   * the user   * @param x the x coordinate for popping up the menu   * @param y the y coordinate for popping up the menu   */  protected void visualizeClusterer(String name, int x, int y) {    final String selectedName = name;    JPopupMenu resultListMenu = new JPopupMenu();    JMenuItem visMainBuffer = new JMenuItem("View in main window");    if (selectedName != null) {      visMainBuffer.addActionListener(new ActionListener() {	  public void actionPerformed(ActionEvent e) {	    m_History.setSingle(selectedName);	  }	});    } else {      visMainBuffer.setEnabled(false);    }    resultListMenu.add(visMainBuffer);    JMenuItem visSepBuffer = new JMenuItem("View in separate window");    if (selectedName != null) {    visSepBuffer.addActionListener(new ActionListener() {	public void actionPerformed(ActionEvent e) {	  m_History.openFrame(selectedName);	}      });    } else {      visSepBuffer.setEnabled(false);    }    resultListMenu.add(visSepBuffer);    JMenuItem saveOutput = new JMenuItem("Save result buffer");    if (selectedName != null) {      saveOutput.addActionListener(new ActionListener() {	  public void actionPerformed(ActionEvent e) {	    saveBuffer(selectedName);	  }	});    } else {      saveOutput.setEnabled(false);    }    resultListMenu.add(saveOutput);        JMenuItem deleteOutput = new JMenuItem("Delete result buffer");    if (selectedName != null) {      deleteOutput.addActionListener(new ActionListener() {	public void actionPerformed(ActionEvent e) {	  m_History.removeResult(selectedName);	}      });    } else {      deleteOutput.setEnabled(false);    }    resultListMenu.add(deleteOutput);    resultListMenu.addSeparator();    JMenuItem loadModel = new JMenuItem("Load model");    loadModel.addActionListener(new ActionListener() {	public void actionPerformed(ActionEvent e) {	  loadClusterer();	}      });    resultListMenu.add(loadModel);    FastVector o = null;    if (selectedName != null) {      o = (FastVector)m_History.getNamedObject(selectedName);    }    VisualizePanel temp_vp = null;    String temp_grph = null;    Clusterer temp_clusterer = null;    Instances temp_trainHeader = null;    int[] temp_ignoreAtts = null;        if (o != null) {      for (int i = 0; i < o.size(); i++) {	Object temp = o.elementAt(i);	if (temp instanceof Clusterer) {	  temp_clusterer = (Clusterer)temp;	} else if (temp instanceof Instances) { // training header	  temp_trainHeader = (Instances)temp;	} else if (temp instanceof int[]) { // ignored attributes	  temp_ignoreAtts = (int[])temp;	} else if (temp instanceof VisualizePanel) { // normal errors	  temp_vp = (VisualizePanel)temp;	} else if (temp instanceof String) { // graphable output	  temp_grph = (String)temp;	}      }     }          final VisualizePanel vp = temp_vp;    final String grph = temp_grph;    final Clusterer clusterer = temp_clusterer;    final Instances trainHeader = temp_trainHeader;    final int[] ignoreAtts = temp_ignoreAtts;        JMenuItem saveModel = new JMenuItem("Save model");    if (clusterer != null) {      saveModel.addActionListener(new ActionListener() {	  public void actionPerformed(ActionEvent e) {	    saveClusterer(selectedName, clusterer, trainHeader, ignoreAtts);	  }	});    } else {      saveModel.setEnabled(false);    }    resultListMenu.add(saveModel);        JMenuItem reEvaluate =      new JMenuItem("Re-evaluate model on current test set");    if (clusterer != null && m_TestInstances != null) {      reEvaluate.addActionListener(new ActionListener() {	  public void actionPerformed(ActionEvent e) {	    reevaluateModel(selectedName, clusterer, trainHeader, ignoreAtts);	  }	});     } else {      reEvaluate.setEnabled(false);    }    resultListMenu.add(reEvaluate);        resultListMenu.addSeparator();

⌨️ 快捷键说明

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