Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Project_Ensemble_GUIView.java
- */
- package project_ensemble_gui;
- import java.io.FileNotFoundException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import org.jdesktop.application.Action;
- import org.jdesktop.application.ResourceMap;
- import org.jdesktop.application.SingleFrameApplication;
- import org.jdesktop.application.FrameView;
- import java.io.File;
- import java.io.PrintWriter;
- import javax.swing.JDialog;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- import libsvm.LibSVM;
- import net.sf.javaml.classification.Classifier;
- import net.sf.javaml.core.Dataset;
- import net.sf.javaml.core.Instance;
- import net.sf.javaml.featureselection.ensemble.LinearRankingEnsemble;
- import net.sf.javaml.featureselection.ranking.RecursiveFeatureEliminationSVM;
- import net.sf.javaml.tools.data.ARFFHandler;
- /**
- * The application's main frame.
- */
- public class Project_Ensemble_GUIView extends FrameView {
- public Project_Ensemble_GUIView(SingleFrameApplication app) {
- super(app);
- initComponents();
- ResourceMap resourceMap = getResourceMap();
- }
- @Action
- public void showAboutBox() {
- if (aboutBox == null) {
- JFrame mainFrame = Project_Ensemble_GUIApp.getApplication().getMainFrame();
- aboutBox = new Project_Ensemble_GUIAboutBox(mainFrame);
- aboutBox.setLocationRelativeTo(mainFrame);
- }
- Project_Ensemble_GUIApp.getApplication().show(aboutBox);
- }
- /** This method is called from within the constructor to
- * initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is
- * always regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- mainPanel = new javax.swing.JPanel();
- jTextField1 = new javax.swing.JTextField();
- jTextField2 = new javax.swing.JTextField();
- jTextField3 = new javax.swing.JTextField();
- jTextField4 = new javax.swing.JTextField();
- jTextField5 = new javax.swing.JTextField();
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jLabel3 = new javax.swing.JLabel();
- jLabel4 = new javax.swing.JLabel();
- jLabel5 = new javax.swing.JLabel();
- jTextField6 = new javax.swing.JTextField();
- jLabel6 = new javax.swing.JLabel();
- jButton1 = new javax.swing.JButton();
- menuBar = new javax.swing.JMenuBar();
- javax.swing.JMenu fileMenu = new javax.swing.JMenu();
- dataSetMenuItem = new javax.swing.JMenuItem();
- javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
- javax.swing.JMenu helpMenu = new javax.swing.JMenu();
- javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
- mainPanel.setMaximumSize(new java.awt.Dimension(453, 278));
- mainPanel.setMinimumSize(new java.awt.Dimension(453, 278));
- mainPanel.setName("mainPanel"); // NOI18N
- mainPanel.setSize(new java.awt.Dimension(453, 278));
- org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(project_ensemble_gui.Project_Ensemble_GUIApp.class).getContext().getResourceMap(Project_Ensemble_GUIView.class);
- jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
- jTextField1.setName("jTextField1"); // NOI18N
- jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
- jTextField2.setName("jTextField2"); // NOI18N
- jTextField3.setText(resourceMap.getString("jTextField3.text")); // NOI18N
- jTextField3.setName("jTextField3"); // NOI18N
- jTextField4.setText(resourceMap.getString("jTextField4.text")); // NOI18N
- jTextField4.setName("jTextField4"); // NOI18N
- jTextField5.setText(resourceMap.getString("jTextField5.text")); // NOI18N
- jTextField5.setName("jTextField5"); // NOI18N
- jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
- jLabel1.setName("jLabel1"); // NOI18N
- jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
- jLabel2.setName("jLabel2"); // NOI18N
- jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
- jLabel3.setName("jLabel3"); // NOI18N
- jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
- jLabel4.setName("jLabel4"); // NOI18N
- jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
- jLabel5.setName("jLabel5"); // NOI18N
- jTextField6.setText(resourceMap.getString("jTextField6.text")); // NOI18N
- jTextField6.setName("jTextField6"); // NOI18N
- jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
- jLabel6.setName("jLabel6"); // NOI18N
- javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(project_ensemble_gui.Project_Ensemble_GUIApp.class).getContext().getActionMap(Project_Ensemble_GUIView.class, this);
- jButton1.setAction(actionMap.get("RunCalculation")); // NOI18N
- jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
- jButton1.setName("jButton1"); // NOI18N
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- RunCalculationButtonActionPerformed(evt);
- }
- });
- org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
- mainPanel.setLayout(mainPanelLayout);
- mainPanelLayout.setHorizontalGroup(
- mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(mainPanelLayout.createSequentialGroup()
- .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(mainPanelLayout.createSequentialGroup()
- .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(org.jdesktop.layout.GroupLayout.TRAILING, mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(jTextField4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
- .add(jTextField3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
- .add(jTextField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
- .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE))
- .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
- .add(jTextField5)
- .add(jTextField6)))
- .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(mainPanelLayout.createSequentialGroup()
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(jLabel6))
- .add(org.jdesktop.layout.GroupLayout.TRAILING, mainPanelLayout.createSequentialGroup()
- .add(6, 6, 6)
- .add(jLabel5)))
- .add(org.jdesktop.layout.GroupLayout.TRAILING, mainPanelLayout.createSequentialGroup()
- .add(18, 18, 18)
- .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(jLabel1)
- .add(jLabel2)
- .add(jLabel3)
- .add(jLabel4)))))
- .add(jButton1))
- .addContainerGap())
- );
- mainPanelLayout.setVerticalGroup(
- mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
- .add(mainPanelLayout.createSequentialGroup()
- .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
- .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .add(jLabel1))
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
- .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .add(jLabel2))
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
- .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .add(jLabel3))
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
- .add(jTextField4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .add(jLabel4))
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
- .add(jTextField5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .add(jLabel5))
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
- .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
- .add(jTextField6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
- .add(jLabel6))
- .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
- .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
- .add(28, 28, 28))
- );
- menuBar.setName("menuBar"); // NOI18N
- fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
- fileMenu.setName("fileMenu"); // NOI18N
- dataSetMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
- dataSetMenuItem.setText(resourceMap.getString("dataSetMenuItem.text")); // NOI18N
- dataSetMenuItem.setName("dataSetMenuItem"); // NOI18N
- dataSetMenuItem.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- dataSetMenuItemActionPerformed(evt);
- }
- });
- fileMenu.add(dataSetMenuItem);
- exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
- exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
- exitMenuItem.setName("exitMenuItem"); // NOI18N
- fileMenu.add(exitMenuItem);
- menuBar.add(fileMenu);
- helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
- helpMenu.setName("helpMenu"); // NOI18N
- aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
- aboutMenuItem.setName("aboutMenuItem"); // NOI18N
- helpMenu.add(aboutMenuItem);
- menuBar.add(helpMenu);
- setComponent(mainPanel);
- setMenuBar(menuBar);
- }// </editor-fold>
- public static double getAverage(double[] numbers) {
- double total = 0.0;
- for (int col = 0; col<numbers.length; col++){
- if (!Double.isNaN(numbers[col])) {
- total +=numbers[col];
- }
- }
- return total/numbers.length;
- }
- public static void printArray(int[][] array) {
- System.out.println("======================================");
- for (int row =0;row<array.length;row++){
- for (int col=0;col<array[row].length;col++){
- System.out.print(array[row][col] +" ");
- }
- System.out.print("\n");
- }
- }
- public static void saveArray(String filename, int[][] output) {
- try{
- PrintWriter pri = new PrintWriter(filename);
- for (int row =0;row<output.length;row++){
- for (int col=0;col<output[row].length;col++){
- pri.print(output[row][col] +" ");
- }
- pri.print("\n");
- }
- pri.close();
- }catch (Exception e) {}
- }
- private void dataSetMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
- JFileChooser jfc = new JFileChooser();
- jfc.showOpenDialog(jfc);
- f = jfc.getSelectedFile();
- wasOpen = true;
- }
- public void ShowDialogBox(){
- JFrame frame = new JFrame("Show Message Dialog");
- JOptionPane.showMessageDialog(frame,"Average accuracy: " + answer);
- }
- private void RunCalculationButtonActionPerformed(java.awt.event.ActionEvent evt) {
- if(wasOpen==false){
- dataSetMenuItemActionPerformed(evt);
- }
- Dataset data = null;
- try {
- data = (Dataset) ARFFHandler.loadARFF(f,Integer.parseInt(jTextField6.getText()));
- } catch (FileNotFoundException ex) {
- Logger.getLogger(Project_Ensemble_GUIView.class.getName()).log(Level.SEVERE, null, ex);
- }
- int n = Integer.parseInt(jTextField1.getText()); //no of repetitions (1, infty)
- double trainSize = Double.parseDouble(jTextField2.getText()); //size of training dataset (% of original dataset) (0.0, 1.0]
- int b = Integer.parseInt(jTextField3.getText()); //no of bootstrap rounds to create the ensemble (1, infty)
- double E = Double.parseDouble(jTextField4.getText());; //% of features to eliminate at each iteration of RFE (0, 1]
- double kPer = Double.parseDouble(jTextField5.getText());; //% of best (lowest) ranked attributes to attain (0.0 ,1.0]
- int rand = 0;
- double ACC[] = new double[n];
- int k = data.noAttributes();
- int ranks[][] = new int[n][k];
- for (int j=0;j<n;j++){//BEGIN: main loop
- // Split the data
- Dataset data2 = data.copy();
- Dataset data3 = data.copy();
- Dataset dataTest = data2;
- Dataset dataTrain = data3;
- for (int s=0; s<data.size(); s++){
- if(dataTrain.size()>(int)Math.round(data.size()*trainSize)){
- rand = (int)(Math.random()*(dataTrain.size()));
- dataTest.set(s, dataTrain.get(rand));
- dataTrain.remove(rand);
- } else {
- dataTest.remove(dataTest.size()-1);
- }
- }
- // Create a feature ranking algorithm
- RecursiveFeatureEliminationSVM[] svmrfes = new RecursiveFeatureEliminationSVM[40];
- for (int i = 0; i < svmrfes.length; i++){
- svmrfes[i] = new RecursiveFeatureEliminationSVM(E);
- }
- // Build the ensemble
- LinearRankingEnsemble ensemble = new LinearRankingEnsemble(svmrfes);
- ensemble.build(dataTrain);
- int Aranks[] = new int[k];
- for (int i = 0; i < ensemble.noAttributes(); i++) {
- ranks[j][i] = ensemble.rank(i);
- Aranks[i] = ranks[j][i];
- }
- // Select the attributes with lowest ranks
- int counter = 0;
- Dataset newDataTrain = dataTrain.copy();
- Dataset newDataTest = dataTest.copy();
- for(int row=0; row<newDataTrain.size(); row++){
- counter = 0;
- for(int g=0; g<Aranks.length; g++){
- if(Aranks[g]>(int)Math.ceil(kPer*k)){
- newDataTrain.get(row).removeAttribute(g-counter);
- if(newDataTest.size()>row){
- newDataTest.get(row).removeAttribute(g-counter);
- }
- counter++;
- }
- }
- }
- // Train classifier on attained attributes
- Classifier cl = new LibSVM();
- cl.buildClassifier(newDataTrain);
- // Test the classifier, compute accuracy
- int correct = 0, wrong = 0;
- for (Instance inst : newDataTest) {
- Object predictedClassValue = cl.classify(inst);
- Object realClassValue = inst.classValue();
- if (predictedClassValue.equals(realClassValue))
- correct++;
- else
- wrong++;
- }
- ACC[j] = correct/(correct + wrong);
- }//END: main loop
- // Save ranks from all repetitions to file
- /*
- saveArray("ranks.data", ranks);
- */
- // Print ranks from all repetitions
- /*
- printArray(ranks);
- */
- answer = getAverage(ACC);
- // for (int i=0;i<ACC.length;i++){
- // System.out.println(ACC[i]);
- // }
- //
- //
- // System.out.println("======================================");
- // System.out.println("Average accuracy: " + getAverage(ACC));
- // System.out.println("======================================");
- ShowDialogBox();
- }
- // Variables declaration - do not modify
- private javax.swing.JMenuItem dataSetMenuItem;
- private javax.swing.JButton jButton1;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JLabel jLabel3;
- private javax.swing.JLabel jLabel4;
- private javax.swing.JLabel jLabel5;
- private javax.swing.JLabel jLabel6;
- private javax.swing.JTextField jTextField1;
- private javax.swing.JTextField jTextField2;
- private javax.swing.JTextField jTextField3;
- private javax.swing.JTextField jTextField4;
- private javax.swing.JTextField jTextField5;
- private javax.swing.JTextField jTextField6;
- private javax.swing.JPanel mainPanel;
- private javax.swing.JMenuBar menuBar;
- // End of variables declaration
- private JDialog aboutBox;
- private File f;
- private boolean wasOpen = false;
- private double answer;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement