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

📄 classifierpanel.java

📁 :<<数据挖掘--实用机器学习技术及java实现>>一书的配套源程序
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
   */  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);  }  /**   * When the Settings button for cross validation is pressed, a window   * appears that allows the user to set the number of folds, whether or not   * to use parallelization, as well as allowing them to customize the   * settings of that parallelization.   */  protected void setCVSettings() {    m_CVSettingsBut.setEnabled(false);    JPanel cvSettingsPanel = new JPanel();    cvSettingsPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));    cvSettingsPanel.setLayout(new GridLayout(3, 1));    JPanel numFoldsOption = new JPanel();    numFoldsOption.setLayout(new BorderLayout());    numFoldsOption.add(m_CVLab, BorderLayout.WEST);    numFoldsOption.add(m_CVText);//, BorderLayout.EAST);    cvSettingsPanel.add(numFoldsOption);    cvSettingsPanel.add(m_RunInParallelBut);    cvSettingsPanel.add(m_ParallelConfigBut);    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(cvSettingsPanel, BorderLayout.CENTER);    all.add(okP, BorderLayout.SOUTH);    final javax.swing.JFrame jf =      new javax.swing.JFrame("Cross-Validation 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_CVSettingsBut.setEnabled(true);      }    });    oK.addActionListener(new ActionListener() {      public void actionPerformed(ActionEvent a) {        m_CVSettingsBut.setEnabled(true);        jf.dispose();      }    });    jf.pack();    jf.setLocation(m_CVSettingsBut.getLocationOnScreen());    jf.setVisible(true);  }  /**   * This function opens up a window when the user wants to customize   * the parallelization aspect of Weka.  The user can choose which computers   * to connect to as well as which port number to connect on.   */  protected void configureParallelization() {    // Gets the current configuration from a file    getDCConfiguration();    m_ParallelConfigBut.setEnabled(false);    JPanel parallelConfigPanel = new JPanel();    parallelConfigPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));    parallelConfigPanel.setLayout(new BorderLayout());    JPanel portOption = new JPanel();    portOption.setLayout(new BorderLayout());    portOption.add(m_PortLab, BorderLayout.WEST);    portOption.add(m_PortText);    parallelConfigPanel.add(portOption, BorderLayout.NORTH);    JPanel p1 = new JPanel();    p1.setBorder(BorderFactory.createCompoundBorder(                 BorderFactory.createTitledBorder("Server Addresses"),                 BorderFactory.createEmptyBorder(0, 5, 5, 5)                 ));    p1.setLayout(new BorderLayout());    JScrollPane scrollArea = new        JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,                    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);    m_AddressesArea.setLineWrap(false);    m_AddressesArea.setWrapStyleWord(false);    scrollArea.getViewport().add(m_AddressesArea, null);    p1.add(scrollArea, BorderLayout.NORTH);    parallelConfigPanel.add(p1, BorderLayout.SOUTH);    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(parallelConfigPanel, BorderLayout.CENTER);    all.add(okP, BorderLayout.SOUTH);    final javax.swing.JFrame jf =      new javax.swing.JFrame("Cross-Validation 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) {          m_ParallelConfigBut.setEnabled(true);          jf.dispose();      }    });    oK.addActionListener(new ActionListener() {      public void actionPerformed(ActionEvent a) {        // Only save the new configuration if OK was pressed.        setDCConfiguration();        m_ParallelConfigBut.setEnabled(true);        jf.dispose();      }    });    jf.pack();    jf.setLocation(m_ParallelConfigBut.getLocationOnScreen());    jf.setVisible(true);  }  /**   * Gets the current configuration for parallel cross validation   * from a file.   */  protected void getDCConfiguration()  {    try    {      String configLocation = System.getProperty("user.home");      // If the user is running windows...      if(System.getProperty("os.name").charAt(0) == 'W')      {          configLocation = configLocation.concat("\\.weka-parallel");      }      // If the user is running anything else      else      {          configLocation = configLocation.concat("/.weka-parallel");      }      // Get the necessary information from the config file      File configFile = new File(configLocation);      if(configFile.exists())      {        BufferedReader fileInStream          = new BufferedReader(new FileReader(configFile));        try        {            // Finds the port number from the config file            String thePort = fileInStream.readLine().trim();            thePort = thePort.substring(5, thePort.length()).trim();            m_PortText.setText(thePort);        }        catch (Exception e)        {            m_PortText.setText("5555");        }        // Finds the list of server names        String aServer = fileInStream.readLine();        if(aServer != null)        {          aServer = aServer.trim();          m_AddressesArea.setText(aServer);          aServer = fileInStream.readLine();        }        while(aServer != null)        {          aServer = aServer.trim();          m_AddressesArea.append("\n" + aServer);          aServer = fileInStream.readLine();        }        fileInStream.close();      }      // If there is no file, stick some default options on the screen      else      {        m_PortText.setText("5555");        m_AddressesArea.setText("overtherainbow.somewhere.edu");      }    }    catch(IOException e)    {      e.printStackTrace();    }  }  /**   * Saves the new configuration for parallel cross-validation to a   * file.   */  protected void setDCConfiguration()  {    try    {        String configLocation = System.getProperty("user.home");        // If the user is running windows...        if(System.getProperty("os.name").charAt(0) == 'W')        {            configLocation = configLocation.concat("\\.weka-parallel");        }        // If the user is running anything else...        else        {            configLocation = configLocation.concat("/.weka-parallel");        }        // Open the config file        File configFile = new File(configLocation);        // Write all necessary information to that file        configFile.createNewFile();        BufferedWriter fileOutStream            = new BufferedWriter(new FileWriter(configFile));        fileOutStream.write("PORT=");        fileOutStream.write(m_PortText.getText().trim());        fileOutStream.newLine();        fileOutStream.write(m_AddressesArea.getText().trim());        fileOutStream.close();    }    catch(IOException e)    {        e.printStackTrace();    }  }  /**   * Process a classifier's

⌨️ 快捷键说明

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