📄 classifierpanel.java
字号:
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * ClassifierPanel.java * Copyright (C) 1999 Len Trigg * */package weka.gui.explorer;import weka.core.Instances;import weka.core.Instance;import weka.core.FastVector;import weka.core.OptionHandler;import weka.core.Attribute;import weka.core.Utils;import weka.core.Drawable;import weka.core.SerializedObject;import weka.classifiers.Classifier;import weka.classifiers.DistributionClassifier;import weka.classifiers.Evaluation;import weka.classifiers.CostMatrix;import weka.classifiers.evaluation.NominalPrediction;import weka.classifiers.evaluation.MarginCurve;import weka.classifiers.evaluation.ThresholdCurve;import weka.classifiers.evaluation.CostCurve;import weka.filters.Filter;import weka.gui.Logger;import weka.gui.TaskLogger;import weka.gui.SysErrLog;import weka.gui.GenericObjectEditor;import weka.gui.PropertyPanel;import weka.gui.ResultHistoryPanel;import weka.gui.SetInstancesPanel;import weka.gui.CostMatrixEditor;import weka.gui.PropertyDialog;import weka.gui.InstancesSummaryPanel;import weka.gui.SaveBuffer;import weka.gui.visualize.VisualizePanel;import weka.gui.visualize.PlotData2D;import weka.gui.visualize.Plot2D;import weka.gui.ExtensionFileFilter;import weka.gui.treevisualizer.*;import java.util.Random;import java.util.Date;import java.text.SimpleDateFormat;import java.awt.FlowLayout;import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.GridBagLayout;import java.awt.GridBagConstraints;import java.awt.Insets;import java.awt.Font;import java.awt.Point;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.event.InputEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.Window;import java.awt.Dimension;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeSupport;import java.io.File;import java.io.FileWriter;import java.io.Writer;import java.io.BufferedWriter;import java.io.PrintWriter;import java.io.OutputStream;import java.io.ObjectOutputStream;import java.io.FileOutputStream;import java.util.zip.GZIPOutputStream;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.FileInputStream;import java.util.zip.GZIPInputStream;import javax.swing.JFileChooser;import javax.swing.JPanel;import javax.swing.JLabel;import javax.swing.JButton;import javax.swing.BorderFactory;import javax.swing.JTextArea;import javax.swing.JScrollPane;import javax.swing.JRadioButton;import javax.swing.ButtonGroup;import javax.swing.JOptionPane;import javax.swing.JComboBox;import javax.swing.DefaultComboBoxModel;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.JFrame;import javax.swing.event.ChangeListener;import javax.swing.event.ChangeEvent;import javax.swing.JViewport;import javax.swing.JCheckBox;import javax.swing.ListSelectionModel;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import javax.swing.JPopupMenu;import javax.swing.JMenu;import javax.swing.JMenuItem;import javax.swing.filechooser.FileFilter;/** * This panel allows the user to select and configure a classifier, set the * attribute of the current dataset to be used as the class, and evaluate * the classifier using a number of testing modes (test on the training data, * train/test on a percentage split, n-fold cross-validation, test on a * separate split). The results of classification runs are stored in a result * history so that previous results are accessible. * * @author Len Trigg (trigg@cs.waikato.ac.nz) * @author Mark Hall (mhall@cs.waikato.ac.nz) * @author Richard Kirkby (rkirkby@cs.waikato.ac.nz) * @version $Revision: 1.1.1.1 $ */public class ClassifierPanel extends JPanel { /** Lets the user configure the classifier */ protected GenericObjectEditor m_ClassifierEditor = new GenericObjectEditor(); /** The panel showing the current classifier selection */ protected PropertyPanel m_CEPanel = new PropertyPanel(m_ClassifierEditor); /** The output area for classification results */ protected JTextArea m_OutText = new JTextArea(20, 40); /** The destination for log/status messages */ protected Logger m_Log = new SysErrLog(); /** The buffer saving object for saving output */ SaveBuffer m_SaveOut = new SaveBuffer(m_Log, this); /** A panel controlling results viewing */ protected ResultHistoryPanel m_History = new ResultHistoryPanel(m_OutText); /** Lets the user select the class column */ protected JComboBox m_ClassCombo = new JComboBox(); /** Click to set test mode to cross-validation */ protected JRadioButton m_CVBut = new JRadioButton("Cross-validation"); /** Click to set test mode to generate a % split */ protected JRadioButton m_PercentBut = new JRadioButton("Percentage split"); /** Click to set test mode to test on training data */ protected JRadioButton m_TrainBut = new JRadioButton("Use training set"); /** Click to set test mode to a user-specified test set */ protected JRadioButton m_TestSplitBut = new JRadioButton("Supplied test set"); /** Check to save the predictions in the results list for visualizing later on */ protected JCheckBox m_StorePredictionsBut = new JCheckBox("Store predictions for visualization"); /** Check to output the model built from the training data */ protected JCheckBox m_OutputModelBut = new JCheckBox("Output model"); /** Check to output true/false positives, precision/recall for each class */ protected JCheckBox m_OutputPerClassBut = new JCheckBox("Output per-class stats"); /** Check to output a confusion matrix */ protected JCheckBox m_OutputConfusionBut = new JCheckBox("Output confusion matrix"); /** Check to output entropy statistics */ protected JCheckBox m_OutputEntropyBut = new JCheckBox("Output entropy evaluation measures"); /** Check to output text predictions */ protected JCheckBox m_OutputPredictionsTextBut = new JCheckBox("Output text predictions on test set"); /** Check to evaluate w.r.t a cost matrix */ protected JCheckBox m_EvalWRTCostsBut = new JCheckBox("Cost-sensitive evaluation"); protected JButton m_SetCostsBut = new JButton("Set..."); /** Label by where the cv folds are entered */ protected JLabel m_CVLab = new JLabel("Folds", SwingConstants.RIGHT); /** The field where the cv folds are entered */ protected JTextField m_CVText = new JTextField("10"); /** Label by where the % split is entered */ protected JLabel m_PercentLab = new JLabel("%", SwingConstants.RIGHT); /** The field where the % split is entered */ protected JTextField m_PercentText = new JTextField("66"); /** The button used to open a separate test dataset */ protected JButton m_SetTestBut = new JButton("Set..."); /** The frame used to show the test set selection panel */ protected JFrame m_SetTestFrame; /** The frame used to show the cost matrix editing panel */ protected PropertyDialog m_SetCostsFrame; /** * Alters the enabled/disabled status of elements associated with each * radio button */ ActionListener m_RadioListener = new ActionListener() { public void actionPerformed(ActionEvent e) { updateRadioLinks(); } }; /** Button for further output/visualize options */ JButton m_MoreOptions = new JButton("More options..."); /** * User specified random seed for cross validation or % split */ protected JTextField m_RandomSeedText = new JTextField("1 "); protected JLabel m_RandomLab = new JLabel("Random seed for XVal / % Split", SwingConstants.RIGHT); /** Click to start running the classifier */ protected JButton m_StartBut = new JButton("Start"); /** Click to stop a running classifier */ protected JButton m_StopBut = new JButton("Stop"); /** Stop the class combo from taking up to much space */ private Dimension COMBO_SIZE = new Dimension(150, m_StartBut .getPreferredSize().height); /** The cost matrix editor for evaluation costs */ protected CostMatrixEditor m_CostMatrixEditor = new CostMatrixEditor(); /** The main set of instances we're playing with */ protected Instances m_Instances; /** The user-supplied test set (if any) */ protected Instances m_TestInstances; /** The user supplied test set after preprocess filters have been applied */ protected Instances m_TestInstancesCopy; /** A thread that classification runs in */ protected Thread m_RunThread; /** default x index for visualizing */ protected int m_visXIndex; /** default y index for visualizing */ protected int m_visYIndex; /** The current visualization object */ protected VisualizePanel m_CurrentVis = null; /** The instances summary panel displayed by m_SetTestFrame */ protected InstancesSummaryPanel m_Summary = null; /** Filter to ensure only model files are selected */ protected FileFilter m_ModelFilter = new ExtensionFileFilter("model", "Model object files"); /** The file chooser for selecting model files */ protected JFileChooser m_FileChooser = new JFileChooser(new File(System.getProperty("user.dir"))); /* Register the property editors we need */ static { java.beans.PropertyEditorManager .registerEditor(weka.core.SelectedTag.class, weka.gui.SelectedTagEditor.class); java.beans.PropertyEditorManager .registerEditor(weka.filters.Filter.class, weka.gui.GenericObjectEditor.class); java.beans.PropertyEditorManager .registerEditor(weka.classifiers.Classifier [].class, weka.gui.GenericArrayEditor.class); java.beans.PropertyEditorManager .registerEditor(weka.classifiers.DistributionClassifier.class, weka.gui.GenericObjectEditor.class); java.beans.PropertyEditorManager .registerEditor(weka.classifiers.Classifier.class, weka.gui.GenericObjectEditor.class); java.beans.PropertyEditorManager .registerEditor(weka.classifiers.CostMatrix.class, weka.gui.CostMatrixEditor.class); } /** * Creates the classifier panel */ public ClassifierPanel() { // Connect / configure the components m_OutText.setEditable(false); m_OutText.setFont(new Font("Monospaced", Font.PLAIN, 12)); m_OutText.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); m_OutText.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != InputEvent.BUTTON1_MASK) { m_OutText.selectAll(); } } }); m_History.setBorder(BorderFactory.createTitledBorder("Result list (right-click for options)")); m_ClassifierEditor.setClassType(Classifier.class); m_ClassifierEditor.setValue(new weka.classifiers.rules.ZeroR()); m_ClassifierEditor.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { repaint(); } }); m_ClassCombo.setToolTipText("Select the attribute to use as the class"); m_TrainBut.setToolTipText("Test on the same set that the classifier" + " is trained on"); m_CVBut.setToolTipText("Perform a n-fold cross-validation"); m_PercentBut.setToolTipText("Train on a percentage of the data and" + " test on the remainder"); m_TestSplitBut.setToolTipText("Test on a user-specified dataset"); m_StartBut.setToolTipText("Starts the classification"); m_StopBut.setToolTipText("Stops a running classification"); m_StorePredictionsBut. setToolTipText("Store predictions in the result list for later " +"visualization"); m_OutputModelBut .setToolTipText("Output the model obtained from the full training set"); m_OutputPerClassBut.setToolTipText("Output precision/recall & true/false" + " positives for each class"); m_OutputConfusionBut .setToolTipText("Output the matrix displaying class confusions"); m_OutputEntropyBut .setToolTipText("Output entropy-based evaluation measures"); m_EvalWRTCostsBut .setToolTipText("Evaluate errors with respect to a cost matrix"); m_OutputPredictionsTextBut .setToolTipText("Include the predictions on the test set in the output buffer"); m_FileChooser.setFileFilter(m_ModelFilter); m_FileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); m_StorePredictionsBut.setSelected(true); m_OutputModelBut.setSelected(true); m_OutputPerClassBut.setSelected(true); m_OutputConfusionBut.setSelected(true); m_ClassCombo.setEnabled(false); m_ClassCombo.setPreferredSize(COMBO_SIZE); m_ClassCombo.setMaximumSize(COMBO_SIZE); m_ClassCombo.setMinimumSize(COMBO_SIZE); m_CVBut.setSelected(true); updateRadioLinks(); ButtonGroup bg = new ButtonGroup(); bg.add(m_TrainBut); bg.add(m_CVBut); bg.add(m_PercentBut); bg.add(m_TestSplitBut); m_TrainBut.addActionListener(m_RadioListener); m_CVBut.addActionListener(m_RadioListener); m_PercentBut.addActionListener(m_RadioListener); m_TestSplitBut.addActionListener(m_RadioListener); m_SetTestBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setTestSet(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -