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

📄 clustererpanel.java

📁 :<<数据挖掘--实用机器学习技术及java实现>>一书的配套源程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		}	      }	      if (m_ClassesToClustersBut.isSelected()) {		selected[m_ClassCombo.getSelectedIndex()] = false;	      }	      for (int i = 0; i < inst.numAttributes(); i++) {		if (selected[i]) {		  outBuff.append("              " + inst.attribute(i).name()				 + '\n');		}	      }	      if (!m_ignoreKeyList.isSelectionEmpty() 		  || m_ClassesToClustersBut.isSelected()) {		outBuff.append("Ingored:\n");		for (int i = 0; i < inst.numAttributes(); i++) {		  if (!selected[i]) {		    outBuff.append("              " + inst.attribute(i).name()				   + '\n');		  }		}	      }	    } else {	      outBuff.append("              [list of attributes omitted]\n");	    }	    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));	    outBuff.append("\n=== Clustering model (full training set) ===\n\n");	    outBuff.append(clusterer.toString() + '\n');	    m_History.updateResult(name);	    	    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("=== Evaluation on training set ===\n\n");	      break;	      case 2: // Percent split	      m_Log.statusMessage("Randomizing instances...");	      inst.randomize(new Random(42));	      trainInst.randomize(new Random(42));	      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("=== 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("=== 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) {	    m_Log.logMessage(ex.getMessage());	    m_Log.statusMessage("See error log");	    ex.printStackTrace();	  } 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);		m_CurrentVis.setXIndex(m_visXIndex); 		m_CurrentVis.setYIndex(m_visYIndex);	      } catch (Exception ex) {		System.err.println(ex);	      }	      m_CurrentVis.addActionListener(new ActionListener() {		  public void actionPerformed(ActionEvent e) {		    if (m_CurrentVis.getInstances().			relationName().			compareTo(m_Instances.relationName()) == 0) {		      setXY_VisualizeIndexes(m_CurrentVis.getXIndex(), 					     m_CurrentVis.getYIndex());		    }		  }		});	      if (saveVis) {		FastVector vv = new FastVector();		vv.addElement(m_CurrentVis);		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) {    AttributeFilter af = new AttributeFilter();    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();    AttributeFilter af = new AttributeFilter();    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;  }  /**   * 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);  }  /**   * 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");    visMainBuffer.addActionListener(new ActionListener() {	public void actionPerformed(ActionEvent e) {	  m_History.setSingle(selectedName);	}      });    resultListMenu.add(visMainBuffer);    JMenuItem visSepBuffer = new JMenuItem("View in separate window");    visSepBuffer.addActionListener(new ActionListener() {	public void actionPerformed(ActionEvent e) {	  m_History.openFrame(selectedName);	}      });    resultListMenu.add(visSepBuffer);    JMenuItem saveOutput = new JMenuItem("Save result buffer");    saveOutput.addActionListener(new ActionListener() {	public void actionPerformed(ActionEvent e) {	  saveBuffer(selectedName);	}      });    resultListMenu.add(saveOutput);    resultListMenu.addSeparator();    FastVector o = (FastVector)m_History.getNamedObject(selectedName);    if (o != null) {      VisualizePanel temp_vp = null;      String temp_grph = null;           for (int i = 0; i < o.size(); i++) {	Object temp = o.elementAt(i);	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;      JMenuItem visClusts = new JMenuItem("Visualize cluster assignments");      if (vp != null) {	visClusts.addActionListener(new ActionListener() {	    public void actionPerformed(ActionEvent e) {	      visualizeClusterAssignments(vp);	    }	  });	resultListMenu.add(visClusts);      }      JMenuItem visTree = new JMenuItem("Visualize tree");      if (grph != null) {	visTree.addActionListener(new ActionListener() {	    public void actionPerformed(ActionEvent e) {	      visualizeTree(grph,vp.getName());	    }	  });	resultListMenu.add(visTree);      }          }    resultListMenu.show(m_History.getList(), x, y);  }  /**   * Save the currently selected clusterer output to a file.   * @param name the name of the buffer to save   */  protected void saveBuffer(String name) {    StringBuffer sb = m_History.getNamedBuffer(name);    if (sb != null) {      if (m_SaveOut.save(sb)) {	m_Log.logMessage("Save succesful.");      }    }  }  private void setIgnoreColumns() {    ListSelectorDialog jd = new ListSelectorDialog(null, m_ignoreKeyList);    // Open the dialog    int result = jd.showDialog();        if (result != ListSelectorDialog.APPROVE_OPTION) {      // clear selected indices      m_ignoreKeyList.clearSelection();    }  }  /**   * Tests out the clusterer panel from the command line.   *   * @param args may optionally contain the name of a dataset to load.   */  public static void main(String [] args) {    try {      final javax.swing.JFrame jf =	new javax.swing.JFrame("Weka Knowledge Explorer: Cluster");      jf.getContentPane().setLayout(new BorderLayout());      final ClustererPanel sp = new ClustererPanel();      jf.getContentPane().add(sp, BorderLayout.CENTER);      weka.gui.LogPanel lp = new weka.gui.LogPanel();      sp.setLog(lp);      jf.getContentPane().add(lp, BorderLayout.SOUTH);      jf.addWindowListener(new java.awt.event.WindowAdapter() {	public void windowClosing(java.awt.event.WindowEvent e) {	  jf.dispose();	  System.exit(0);	}      });      jf.pack();      jf.setSize(800, 600);      jf.setVisible(true);      if (args.length == 1) {	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);	sp.setInstances(i);      }    } catch (Exception ex) {      ex.printStackTrace();      System.err.println(ex.getMessage());    }  }}

⌨️ 快捷键说明

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