📄 attributeselectionpanel.java
字号:
inst.stratify(numFolds); } for (int fold = 0; fold < numFolds;fold++) { m_Log.statusMessage("Creating splits for fold " + (fold + 1) + "..."); Instances train = inst.trainCV(numFolds, fold, random); m_Log.statusMessage("Selecting attributes using all but fold " + (fold + 1) + "..."); eval.selectAttributesCVSplit(train); } break; default: throw new Exception("Test mode not implemented"); } if (testMode == 0) { outBuff.append(eval.toResultsString()); } else { outBuff.append(eval.CVResultsString()); } outBuff.append("\n"); m_History.updateResult(name); m_Log.logMessage("Finished " + ename+" "+sname); m_Log.statusMessage("OK"); } catch (Exception ex) { m_Log.logMessage(ex.getMessage()); m_Log.statusMessage("See error log"); } finally { if (evaluator instanceof AttributeTransformer) { try { Instances transformed = ((AttributeTransformer)evaluator).transformedData(); transformed. setRelationName("AT: "+transformed.relationName()); FastVector vv = new FastVector(); vv.addElement(transformed); m_History.addObject(name, vv); } catch (Exception ex) { System.err.println(ex); ex.printStackTrace(); } } else if (testMode == 0) { try { Instances reducedInst = eval.reduceDimensionality(inst); FastVector vv = new FastVector(); vv.addElement(reducedInst); m_History.addObject(name, vv); } catch (Exception ex) { ex.printStackTrace(); } } if (isInterrupted()) { m_Log.logMessage("Interrupted " + ename+" "+sname); m_Log.statusMessage("See error log"); } m_RunThread = null; m_StartBut.setEnabled(true); m_StopBut.setEnabled(false); if (m_Log instanceof TaskLogger) { ((TaskLogger)m_Log).taskFinished(); } } } }; m_RunThread.setPriority(Thread.MIN_PRIORITY); m_RunThread.start(); } } /** * Stops the currently running attribute selection (if any). */ protected void stopAttributeSelection() { if (m_RunThread != null) { m_RunThread.interrupt(); // This is deprecated (and theoretically the interrupt should do). m_RunThread.stop(); } } /** * Save the named buffer to a file. * @param name the name of the buffer to be saved. */ protected void saveBuffer(String name) { StringBuffer sb = m_History.getNamedBuffer(name); if (sb != null) { if (m_SaveOut.save(sb)) { m_Log.logMessage("Save succesful."); } } } /** * Popup a visualize panel for viewing transformed data * * @param ti the Instances to display */ protected void visualizeTransformedData(Instances ti) { if (ti != null) { MatrixPanel mp = new MatrixPanel(); mp.setInstances(ti); String plotName = ti.relationName(); final javax.swing.JFrame jf = new javax.swing.JFrame("Weka Attribute Selection Visualize: " +plotName); jf.setSize(800,600); jf.getContentPane().setLayout(new BorderLayout()); jf.getContentPane().add(mp, BorderLayout.CENTER); jf.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { jf.dispose(); } }); jf.setVisible(true); } } /** * Popup a SaveDialog for saving the transformed data * * @param ti the Instances to display */ protected void saveTransformedData(Instances ti) { JFileChooser fc; int retVal; BufferedWriter writer; ExtensionFileFilter filter; fc = new JFileChooser(); filter = new ExtensionFileFilter(".arff", "ARFF data files"); fc.setFileFilter(filter); retVal = fc.showSaveDialog(this); if (retVal == JFileChooser.APPROVE_OPTION) { try { writer = new BufferedWriter(new FileWriter(fc.getSelectedFile())); writer.write(ti.toString()); writer.flush(); writer.close(); } catch (Exception e) { e.printStackTrace(); m_Log.logMessage("Problem saving data: " + e.getMessage()); JOptionPane.showMessageDialog( this, "Problem saving data:\n" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } } } /** * 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 visualize(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); FastVector o = null; if (selectedName != null) { o = (FastVector)m_History.getNamedObject(selectedName); } // VisualizePanel temp_vp = null; Instances tempTransformed = null; if (o != null) { for (int i = 0; i < o.size(); i++) { Object temp = o.elementAt(i); // if (temp instanceof VisualizePanel) { if (temp instanceof Instances) { // temp_vp = (VisualizePanel)temp; tempTransformed = (Instances) temp; } } } // final VisualizePanel vp = temp_vp; final Instances ti = tempTransformed; JMenuItem visTrans = null; if (ti != null) { if (ti.relationName().startsWith("AT:")) { visTrans = new JMenuItem("Visualize transformed data"); } else { visTrans = new JMenuItem("Visualize reduced data"); } resultListMenu.addSeparator(); } // JMenuItem visTrans = new JMenuItem("Visualize transformed data"); if (ti != null && visTrans != null) { visTrans.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { visualizeTransformedData(ti); } }); } if (visTrans != null) { resultListMenu.add(visTrans); } JMenuItem saveTrans = null; if (ti != null) { if (ti.relationName().startsWith("AT:")) saveTrans = new JMenuItem("Save transformed data..."); else saveTrans = new JMenuItem("Save reduced data..."); } if (saveTrans != null) { saveTrans.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { saveTransformedData(ti); } }); resultListMenu.add(saveTrans); } resultListMenu.show(m_History.getList(), x, y); } /** * updates the capabilities filter of the GOE * * @param filter the new filter to use */ protected void updateCapabilitiesFilter(Capabilities filter) { Instances tempInst; Capabilities filterClass; Capabilities filterMerged; if (filter == null) { m_AttributeEvaluatorEditor.setCapabilitiesFilter(new Capabilities(null)); m_AttributeSearchEditor.setCapabilitiesFilter(new Capabilities(null)); return; } // class index is never set in Explorer! filter.disable(Capability.NO_CLASS); filter.disableAllClasses(); // determine class attribute (will miss missing class values, but for // efficiency reasons we don't examine the complete dataset again!) if (!ExplorerDefaults.getInitGenericObjectEditorFilter()) tempInst = new Instances(m_Instances, 0); else tempInst = new Instances(m_Instances); tempInst.setClassIndex(m_ClassCombo.getSelectedIndex()); filterClass = null; try { filterClass = Capabilities.forInstances(tempInst); } catch (Exception e) { filterClass = new Capabilities(null); } // generate and set new filter filterMerged = (Capabilities) filter.clone(); filterMerged.or(filterClass); m_AttributeEvaluatorEditor.setCapabilitiesFilter(filterMerged); m_AttributeSearchEditor.setCapabilitiesFilter(filterMerged); } /** * method gets called in case of a change event * * @param e the associated change event */ public void capabilitiesFilterChanged(CapabilitiesFilterChangeEvent e) { updateCapabilitiesFilter((Capabilities) e.getFilter().clone()); } /** * Tests out the attribute selection 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 Explorer: Select attributes"); jf.getContentPane().setLayout(new BorderLayout()); final AttributeSelectionPanel sp = new AttributeSelectionPanel(); 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.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 + -