classifierpanel.java
来自「weka 源代码很好的 对于学习 数据挖掘算法很有帮助」· Java 代码 · 共 1,607 行 · 第 1/4 页
JAVA
1,607 行
m_ClassCombo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int selected = m_ClassCombo.getSelectedIndex(); if (selected != -1) { boolean isNominal = m_Instances.attribute(selected).isNominal(); m_OutputPerClassBut.setEnabled(isNominal); m_OutputConfusionBut.setEnabled(isNominal); } } }); m_History.setHandleRightClicks(false); // see if we can popup a menu for the selected result m_History.getList().addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if ((e.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) { } else { int index = m_History.getList().locationToIndex(e.getPoint()); if (index != -1) { String name = m_History.getNameAtIndex(index); visualize(name, e.getX(), e.getY()); } } } }); m_MoreOptions.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { m_MoreOptions.setEnabled(false); JPanel moreOptionsPanel = new JPanel(); moreOptionsPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5)); moreOptionsPanel.setLayout(new GridLayout(6, 1)); moreOptionsPanel.add(m_OutputModelBut); moreOptionsPanel.add(m_OutputPerClassBut); moreOptionsPanel.add(m_OutputEntropyBut); moreOptionsPanel.add(m_OutputConfusionBut); moreOptionsPanel.add(m_StorePredictionsBut); JPanel costMatrixOption = new JPanel(); costMatrixOption.setLayout(new BorderLayout()); costMatrixOption.add(m_EvalWRTCostsBut, BorderLayout.WEST); costMatrixOption.add(m_SetCostsBut, BorderLayout.EAST); moreOptionsPanel.add(costMatrixOption); JPanel all = new JPanel(); all.setLayout(new BorderLayout()); JButton oK = new JButton("OK"); JPanel okP = new JPanel(); okP.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); okP.setLayout(new GridLayout(1,1,5,5)); okP.add(oK); all.add(moreOptionsPanel, BorderLayout.CENTER); all.add(okP, BorderLayout.SOUTH); final javax.swing.JFrame jf = new javax.swing.JFrame("Classifier evaluation options"); jf.getContentPane().setLayout(new BorderLayout()); jf.getContentPane().add(all, BorderLayout.CENTER); jf.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent w) { jf.dispose(); m_MoreOptions.setEnabled(true); } }); oK.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent a) { m_MoreOptions.setEnabled(true); jf.dispose(); } }); jf.pack(); jf.setLocation(m_MoreOptions.getLocationOnScreen()); jf.setVisible(true); } }); // Layout the GUI JPanel p1 = new JPanel(); p1.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Classifier"), BorderFactory.createEmptyBorder(0, 5, 5, 5) )); p1.setLayout(new BorderLayout()); p1.add(m_CEPanel, BorderLayout.NORTH); JPanel p2 = new JPanel(); GridBagLayout gbL = new GridBagLayout(); p2.setLayout(gbL); p2.setBorder(BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder("Test options"), BorderFactory.createEmptyBorder(0, 5, 5, 5) )); GridBagConstraints gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.WEST; gbC.gridy = 0; gbC.gridx = 0; gbL.setConstraints(m_TrainBut, gbC); p2.add(m_TrainBut); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.WEST; gbC.gridy = 1; gbC.gridx = 0; gbL.setConstraints(m_TestSplitBut, gbC); p2.add(m_TestSplitBut); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.EAST; gbC.fill = GridBagConstraints.HORIZONTAL; gbC.gridy = 1; gbC.gridx = 1; gbC.gridwidth = 2; gbC.insets = new Insets(2, 10, 2, 0); gbL.setConstraints(m_SetTestBut, gbC); p2.add(m_SetTestBut); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.WEST; gbC.gridy = 2; 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 = 2; 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 = 2; gbC.gridx = 2; gbC.weightx = 100; gbC.ipadx = 20; gbL.setConstraints(m_CVText, gbC); p2.add(m_CVText); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.WEST; gbC.gridy = 3; gbC.gridx = 0; gbL.setConstraints(m_PercentBut, gbC); p2.add(m_PercentBut); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.EAST; gbC.fill = GridBagConstraints.HORIZONTAL; gbC.gridy = 3; gbC.gridx = 1; gbC.insets = new Insets(2, 10, 2, 10); gbL.setConstraints(m_PercentLab, gbC); p2.add(m_PercentLab); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.EAST; gbC.fill = GridBagConstraints.HORIZONTAL; gbC.gridy = 3; gbC.gridx = 2; gbC.weightx = 100; gbC.ipadx = 20; gbL.setConstraints(m_PercentText, gbC); p2.add(m_PercentText); gbC = new GridBagConstraints(); gbC.anchor = GridBagConstraints.WEST; gbC.fill = GridBagConstraints.HORIZONTAL; gbC.gridy = 4; gbC.gridx = 0; gbC.weightx = 100; gbC.gridwidth = 3; gbC.insets = new Insets(3, 0, 1, 0); gbL.setConstraints(m_MoreOptions, gbC); p2.add(m_MoreOptions); 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("Classifier 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.anchor = GridBagConstraints.WEST; gbC.fill = GridBagConstraints.HORIZONTAL; gbC.gridy = 0; gbC.gridx = 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; gbL.setConstraints(buttons, gbC); mondo.add(buttons); gbC = new GridBagConstraints(); //gbC.anchor = GridBagConstraints.NORTH; gbC.fill = GridBagConstraints.BOTH; gbC.gridy = 2; gbC.gridx = 0; gbC.weightx = 0; 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(p1, BorderLayout.NORTH); add(mondo, BorderLayout.CENTER); } /** * Updates the enabled status of the input fields and labels. */ protected void updateRadioLinks() { m_SetTestBut.setEnabled(m_TestSplitBut.isSelected()); if ((m_SetTestFrame != null) && (!m_TestSplitBut.isSelected())) { m_SetTestFrame.setVisible(false); } m_CVText.setEnabled(m_CVBut.isSelected()); m_CVLab.setEnabled(m_CVBut.isSelected()); m_PercentText.setEnabled(m_PercentBut.isSelected()); m_PercentLab.setEnabled(m_PercentBut.isSelected()); } /** * Sets the preprocess panel through which user selected * filters can be applied to any supplied test data * @param p the preprocess panel to use */ public void setPreprocess(PreprocessPanel p) { m_Preprocess = p; } /** * 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; } /** * Set the default attributes to use on the x and y axis * of a new visualization object. * @param x the index of the attribute to use on the x axis * @param y the index of the attribute to use on the y axis */ public void setXY_VisualizeIndexes(int x, int y) { m_visXIndex = x; m_visYIndex = y; } /** * Tells the panel to use a new set of instances. * * @param inst a set of Instances */ public void setInstances(Instances inst) { m_Instances = inst; setXY_VisualizeIndexes(0,0); // reset the default x and y indexes 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; default: type = "(???) "; } attribNames[i] = type + m_Instances.attribute(i).name(); } m_ClassCombo.setModel(new DefaultComboBoxModel(attribNames)); if (attribNames.length > 0) { m_ClassCombo.setSelectedIndex(attribNames.length - 1); m_ClassCombo.setEnabled(true); m_StartBut.setEnabled(m_RunThread == null); m_StopBut.setEnabled(m_RunThread != null); } else { m_StartBut.setEnabled(false); m_StopBut.setEnabled(false); } } /** * Attempts to filter the user specified test set through * the most currently used set of filters (if any) from the * pre-process panel. */ protected void filterUserTestInstances() { if (m_Preprocess != null && m_TestInstances != null) { m_TestInstancesCopy = new Instances(m_TestInstances); SerializedObject sf = m_Preprocess.getMostRecentFilters(); if (sf != null) { Filter [] filters = null; try { filters = (Filter [])sf.getObject(); } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Could not deserialize filters", null, JOptionPane.ERROR_MESSAGE); } if (filters.length != 0) { try { m_Log.statusMessage("Applying preprocess filters to test data..."); m_TestInstancesCopy = new Instances(m_TestInstances); for (int i = 0; i < filters.length; i++) { m_Log.statusMessage("Passing through filter " + (i + 1) + ": " + filters[i].getClass().getName()); filters[i].setInputFormat(m_TestInstancesCopy); m_TestInstancesCopy = Filter.useFilter(m_TestInstancesCopy, filters[i]); } m_Log.statusMessage("OK"); } catch (Exception ex) { m_Log.statusMessage("See error log"); m_Log.logMessage("Problem applying filters to test data " +"(Classifier Panel)"); } if (m_Summary != null && m_TestInstancesCopy != null) { m_Summary.setInstances(m_TestInstancesCopy); } } } } } /** * Sets the user test set. Information about the current test set * is displayed in an InstanceSummaryPanel and the user is given the * ability to load another set from a file or url. * */ protected void setTestSet() { if (m_SetTestFrame == null) { final SetInstancesPanel sp = new SetInstancesPanel(); m_Summary = sp.getSummary(); if (m_TestInstancesCopy != null) { sp.setInstances(m_TestInstancesCopy); } sp.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { m_TestInstances = sp.getInstances(); filterUserTestInstances(); } }); // Add propertychangelistener to update m_TestInstances whenever // it changes in the settestframe m_SetTestFrame = new JFrame("Test Instances"); m_SetTestFrame.getContentPane().setLayout(new BorderLayout()); m_SetTestFrame.getContentPane().add(sp, BorderLayout.CENTER); m_SetTestFrame.pack(); } m_SetTestFrame.setVisible(true); } /** * Process a classifier's prediction for an instance and update a * set of plotting instances and additional plotting info. plotInfo * for nominal class datasets holds shape types (actual data points have * automatic shape type assignment; classifer error data points have * box shape type). For numeric class datasets, the actual data points * are stored in plotInstances and plotInfo stores the error (which is
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?