attributeselectionpanel.java
来自「Weka」· Java 代码 · 共 1,156 行 · 第 1/3 页
JAVA
1,156 行
JPanel p1_1 = new JPanel(); p1_1.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Search Method"), BorderFactory.createEmptyBorder(0, 5, 5, 5) )); p1_1.setLayout(new BorderLayout()); p1_1.add(m_ASEPanel, BorderLayout.NORTH); JPanel p_new = new JPanel(); p_new.setLayout(new BorderLayout()); p_new.add(p1, BorderLayout.NORTH); p_new.add(p1_1, BorderLayout.CENTER); JPanel p2 = new JPanel(); GridBagLayout gbL = new GridBagLayout(); p2.setLayout(gbL); p2.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Attribute Selection Mode"), BorderFactory.createEmptyBorder(0, 5, 5, 5) )); GridBagConstraints gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.WEST; gbC.gridy = 2; gbC.gridx = 0; gbL.setConstraints(m_TrainBut, gbC); p2.add(m_TrainBut); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.WEST; gbC.gridy = 4; gbC.gridx = 0; gbL.setConstraints(m_CVBut, gbC); p2.add(m_CVBut); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.EAST; gbC.fill = GridBagConstraints.HORIZONTAL; gbC.gridy = 4; gbC.gridx = 1; gbC.insets = new Insets(2, 10, 2, 10); gbL.setConstraints(m_CVLab, gbC); p2.add(m_CVLab); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.EAST; gbC.fill = GridBagConstraints.HORIZONTAL; gbC.gridy = 4; gbC.gridx = 2; gbC.weightx = 100; gbC.ipadx = 20; gbL.setConstraints(m_CVText, gbC); p2.add(m_CVText); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.EAST; gbC.fill = GridBagConstraints.HORIZONTAL; gbC.gridy = 6; gbC.gridx = 1; gbC.insets = new Insets(2, 10, 2, 10); gbL.setConstraints(m_SeedLab, gbC); p2.add(m_SeedLab); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.EAST; gbC.fill = GridBagConstraints.HORIZONTAL; gbC.gridy = 6; gbC.gridx = 2; gbC.weightx = 100; gbC.ipadx = 20; gbL.setConstraints(m_SeedText, gbC); p2.add(m_SeedText); JPanel buttons = new JPanel(); buttons.setLayout(new GridLayout(2, 2)); buttons.add(m_ClassCombo); m_ClassCombo.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); JPanel ssButs = new JPanel(); ssButs.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); ssButs.setLayout(new GridLayout(1, 2, 5, 5)); ssButs.add(m_StartBut); ssButs.add(m_StopBut); buttons.add(ssButs); JPanel p3 = new JPanel(); p3.setBorder(BorderFactory. createTitledBorder("Attribute selection output")); p3.setLayout(new BorderLayout()); final JScrollPane js = new JScrollPane(m_OutText); p3.add(js, BorderLayout.CENTER); js.getViewport().addChangeListener(new ChangeListener() { private int lastHeight; public void stateChanged(ChangeEvent e) { JViewport vp = (JViewport)e.getSource(); int h = vp.getViewSize().height; if (h != lastHeight) { // i.e. an addition not just a user scrolling lastHeight = h; int x = h - vp.getExtentSize().height; vp.setViewPosition(new Point(0, x)); } } }); JPanel mondo = new JPanel(); gbL = new GridBagLayout(); mondo.setLayout(gbL); gbC = new GridBagConstraints(); gbC.fill = GridBagConstraints.HORIZONTAL; gbC.gridy = 0; gbC.gridx = 0; gbC.weightx = 0; gbL.setConstraints(p2, gbC); mondo.add(p2); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.NORTH; gbC.fill = GridBagConstraints.HORIZONTAL; gbC.gridy = 1; gbC.gridx = 0; gbC.weightx = 0; gbL.setConstraints(buttons, gbC); mondo.add(buttons); gbC = new GridBagConstraints(); gbC.fill = GridBagConstraints.BOTH; gbC.gridy = 2; gbC.gridx = 0; gbC.weightx = 0; gbC.weighty = 100; gbL.setConstraints(m_History, gbC); mondo.add(m_History); gbC = new GridBagConstraints(); gbC.fill = GridBagConstraints.BOTH; gbC.gridy = 0; gbC.gridx = 1; gbC.gridheight = 3; gbC.weightx = 100; gbC.weighty = 100; gbL.setConstraints(p3, gbC); mondo.add(p3); setLayout(new BorderLayout()); add(p_new, BorderLayout.NORTH); add(mondo, BorderLayout.CENTER); } /** * Updates the enabled status of the input fields and labels. */ protected void updateRadioLinks() { m_CVBut.setEnabled(true); m_CVText.setEnabled(m_CVBut.isSelected()); m_CVLab.setEnabled(m_CVBut.isSelected()); m_SeedText.setEnabled(m_CVBut.isSelected()); m_SeedLab.setEnabled(m_CVBut.isSelected()); if (m_AttributeEvaluatorEditor.getValue() instanceof AttributeTransformer) { m_CVBut.setSelected(false); m_CVBut.setEnabled(false); m_CVText.setEnabled(false); m_CVLab.setEnabled(false); m_SeedText.setEnabled(false); m_SeedLab.setEnabled(false); m_TrainBut.setSelected(true); } } /** * Sets the Logger to receive informational messages * * @param newLog the Logger that will now get info messages */ public void setLog(Logger newLog) { m_Log = newLog; } /** * Tells the panel to use a new set of instances. * * @param inst a set of Instances */ public void setInstances(Instances inst) { m_Instances = inst; String [] attribNames = new String [m_Instances.numAttributes()]; for (int i = 0; i < attribNames.length; i++) { String type = ""; switch (m_Instances.attribute(i).type()) { case Attribute.NOMINAL: type = "(Nom) "; break; case Attribute.NUMERIC: type = "(Num) "; break; case Attribute.STRING: type = "(Str) "; break; case Attribute.DATE: type = "(Dat) "; break; case Attribute.RELATIONAL: type = "(Rel) "; break; default: type = "(???) "; } String attnm = m_Instances.attribute(i).name(); attribNames[i] = type + attnm; } m_StartBut.setEnabled(m_RunThread == null); m_StopBut.setEnabled(m_RunThread != null); m_ClassCombo.setModel(new DefaultComboBoxModel(attribNames)); if (inst.classIndex() == -1) m_ClassCombo.setSelectedIndex(attribNames.length - 1); else m_ClassCombo.setSelectedIndex(inst.classIndex()); m_ClassCombo.setEnabled(true); } /** * Starts running the currently configured attribute evaluator and * search method. This is run in a separate thread, and will only start if * there is no attribute selection already running. The attribute selection * output is sent to the results history panel. */ protected void startAttributeSelection() { if (m_RunThread == null) { m_StartBut.setEnabled(false); m_StopBut.setEnabled(true); m_RunThread = new Thread() { public void run() { // Copy the current state of things m_Log.statusMessage("Setting up..."); Instances inst = new Instances(m_Instances); int testMode = 0; int numFolds = 10; int seed = 1; int classIndex = m_ClassCombo.getSelectedIndex(); ASEvaluation evaluator = (ASEvaluation) m_AttributeEvaluatorEditor.getValue(); ASSearch search = (ASSearch) m_AttributeSearchEditor.getValue(); StringBuffer outBuff = new StringBuffer(); String name = (new SimpleDateFormat("HH:mm:ss - ")) .format(new Date()); String sname = search.getClass().getName(); if (sname.startsWith("weka.attributeSelection.")) { name += sname.substring("weka.attributeSelection.".length()); } else { name += sname; } String ename = evaluator.getClass().getName(); if (ename.startsWith("weka.attributeSelection.")) { name += (" + " +ename.substring("weka.attributeSelection.".length())); } else { name += (" + "+ename); } // assemble commands String cmd; String cmdFilter; String cmdClassifier; // 1. attribute selection command Vector<String> list = new Vector<String>(); list.add("-s"); if (search instanceof OptionHandler) list.add(sname + " " + Utils.joinOptions(((OptionHandler) search).getOptions())); else list.add(sname); if (evaluator instanceof OptionHandler) { String[] opt = ((OptionHandler) evaluator).getOptions(); for (int i = 0; i < opt.length; i++) list.add(opt[i]); } cmd = ename + " " + Utils.joinOptions(list.toArray(new String[list.size()])); // 2. filter command weka.filters.supervised.attribute.AttributeSelection filter = new weka.filters.supervised.attribute.AttributeSelection(); filter.setEvaluator((ASEvaluation) m_AttributeEvaluatorEditor.getValue()); filter.setSearch((ASSearch) m_AttributeSearchEditor.getValue()); cmdFilter = filter.getClass().getName() + " " + Utils.joinOptions(((OptionHandler) filter).getOptions()); // 3. meta-classifier command weka.classifiers.meta.AttributeSelectedClassifier cls = new weka.classifiers.meta.AttributeSelectedClassifier(); cls.setEvaluator((ASEvaluation) m_AttributeEvaluatorEditor.getValue()); cls.setSearch((ASSearch) m_AttributeSearchEditor.getValue()); cmdClassifier = cls.getClass().getName() + " " + Utils.joinOptions(cls.getOptions()); AttributeSelection eval = null; try { if (m_CVBut.isSelected()) { testMode = 1; numFolds = Integer.parseInt(m_CVText.getText()); seed = Integer.parseInt(m_SeedText.getText()); if (numFolds <= 1) { throw new Exception("Number of folds must be greater than 1"); } } inst.setClassIndex(classIndex); // Output some header information m_Log.logMessage("Started " + ename); m_Log.logMessage("Command: " + cmd); m_Log.logMessage("Filter command: " + cmdFilter); m_Log.logMessage("Meta-classifier command: " + cmdClassifier); if (m_Log instanceof TaskLogger) { ((TaskLogger)m_Log).taskStarted(); } outBuff.append("=== Run information ===\n\n"); outBuff.append("Evaluator: " + ename); if (evaluator instanceof OptionHandler) { String [] o = ((OptionHandler) evaluator).getOptions(); outBuff.append(" " + Utils.joinOptions(o)); } outBuff.append("\nSearch: " + sname); if (search instanceof OptionHandler) { String [] o = ((OptionHandler) search).getOptions(); outBuff.append(" " + Utils.joinOptions(o)); } outBuff.append("\n"); outBuff.append("Relation: " + inst.relationName() + '\n'); outBuff.append("Instances: " + inst.numInstances() + '\n'); outBuff.append("Attributes: " + inst.numAttributes() + '\n'); if (inst.numAttributes() < 100) { for (int i = 0; i < inst.numAttributes(); i++) { outBuff.append(" " + inst.attribute(i).name() + '\n'); } } else { outBuff.append(" [list of attributes omitted]\n"); } outBuff.append("Evaluation mode: "); switch (testMode) { case 0: // select using all training outBuff.append("evaluate on all training data\n"); break; case 1: // CV mode outBuff.append("" + numFolds + "-fold cross-validation\n"); break; } outBuff.append("\n"); m_History.addResult(name, outBuff); m_History.setSingle(name); // Do the feature selection and output the results. m_Log.statusMessage("Doing feature selection..."); m_History.updateResult(name); eval = new AttributeSelection(); eval.setEvaluator(evaluator); eval.setSearch(search); eval.setFolds(numFolds); eval.setSeed(seed); if (testMode == 1) { eval.setXval(true); } switch (testMode) { case 0: // select using training m_Log.statusMessage("Evaluating on training data..."); eval.SelectAttributes(inst); break; case 1: // CV mode m_Log.statusMessage("Randomizing instances..."); Random random = new Random(seed); inst.randomize(random); if (inst.attribute(classIndex).isNominal()) { m_Log.statusMessage("Stratifying instances..."); 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());
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?