Advertisement
Guest User

Untitled

a guest
May 9th, 2012
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.03 KB | None | 0 0
  1. /*
  2. * Project_Ensemble_GUIView.java
  3. */
  4.  
  5. package project_ensemble_gui;
  6.  
  7. import java.io.FileNotFoundException;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. import org.jdesktop.application.Action;
  11. import org.jdesktop.application.ResourceMap;
  12. import org.jdesktop.application.SingleFrameApplication;
  13. import org.jdesktop.application.FrameView;
  14. import java.io.File;
  15. import java.io.PrintWriter;
  16. import javax.swing.JDialog;
  17. import javax.swing.JFileChooser;
  18. import javax.swing.JFrame;
  19. import javax.swing.JOptionPane;
  20. import libsvm.LibSVM;
  21. import net.sf.javaml.classification.Classifier;
  22. import net.sf.javaml.core.Dataset;
  23. import net.sf.javaml.core.Instance;
  24. import net.sf.javaml.featureselection.ensemble.LinearRankingEnsemble;
  25. import net.sf.javaml.featureselection.ranking.RecursiveFeatureEliminationSVM;
  26. import net.sf.javaml.tools.data.ARFFHandler;
  27.  
  28. /**
  29. * The application's main frame.
  30. */
  31. public class Project_Ensemble_GUIView extends FrameView {
  32.  
  33. public Project_Ensemble_GUIView(SingleFrameApplication app) {
  34. super(app);
  35.  
  36. initComponents();
  37. ResourceMap resourceMap = getResourceMap();
  38. }
  39.  
  40. @Action
  41. public void showAboutBox() {
  42. if (aboutBox == null) {
  43. JFrame mainFrame = Project_Ensemble_GUIApp.getApplication().getMainFrame();
  44. aboutBox = new Project_Ensemble_GUIAboutBox(mainFrame);
  45. aboutBox.setLocationRelativeTo(mainFrame);
  46. }
  47. Project_Ensemble_GUIApp.getApplication().show(aboutBox);
  48. }
  49.  
  50. /** This method is called from within the constructor to
  51. * initialize the form.
  52. * WARNING: Do NOT modify this code. The content of this method is
  53. * always regenerated by the Form Editor.
  54. */
  55. @SuppressWarnings("unchecked")
  56. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  57. private void initComponents() {
  58.  
  59. mainPanel = new javax.swing.JPanel();
  60. jTextField1 = new javax.swing.JTextField();
  61. jTextField2 = new javax.swing.JTextField();
  62. jTextField3 = new javax.swing.JTextField();
  63. jTextField4 = new javax.swing.JTextField();
  64. jTextField5 = new javax.swing.JTextField();
  65. jLabel1 = new javax.swing.JLabel();
  66. jLabel2 = new javax.swing.JLabel();
  67. jLabel3 = new javax.swing.JLabel();
  68. jLabel4 = new javax.swing.JLabel();
  69. jLabel5 = new javax.swing.JLabel();
  70. jTextField6 = new javax.swing.JTextField();
  71. jLabel6 = new javax.swing.JLabel();
  72. jButton1 = new javax.swing.JButton();
  73. menuBar = new javax.swing.JMenuBar();
  74. javax.swing.JMenu fileMenu = new javax.swing.JMenu();
  75. dataSetMenuItem = new javax.swing.JMenuItem();
  76. javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
  77. javax.swing.JMenu helpMenu = new javax.swing.JMenu();
  78. javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
  79.  
  80. mainPanel.setMaximumSize(new java.awt.Dimension(453, 278));
  81. mainPanel.setMinimumSize(new java.awt.Dimension(453, 278));
  82. mainPanel.setName("mainPanel"); // NOI18N
  83. mainPanel.setSize(new java.awt.Dimension(453, 278));
  84.  
  85. org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(project_ensemble_gui.Project_Ensemble_GUIApp.class).getContext().getResourceMap(Project_Ensemble_GUIView.class);
  86. jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
  87. jTextField1.setName("jTextField1"); // NOI18N
  88.  
  89. jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
  90. jTextField2.setName("jTextField2"); // NOI18N
  91.  
  92. jTextField3.setText(resourceMap.getString("jTextField3.text")); // NOI18N
  93. jTextField3.setName("jTextField3"); // NOI18N
  94.  
  95. jTextField4.setText(resourceMap.getString("jTextField4.text")); // NOI18N
  96. jTextField4.setName("jTextField4"); // NOI18N
  97.  
  98. jTextField5.setText(resourceMap.getString("jTextField5.text")); // NOI18N
  99. jTextField5.setName("jTextField5"); // NOI18N
  100.  
  101. jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
  102. jLabel1.setName("jLabel1"); // NOI18N
  103.  
  104. jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
  105. jLabel2.setName("jLabel2"); // NOI18N
  106.  
  107. jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
  108. jLabel3.setName("jLabel3"); // NOI18N
  109.  
  110. jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
  111. jLabel4.setName("jLabel4"); // NOI18N
  112.  
  113. jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
  114. jLabel5.setName("jLabel5"); // NOI18N
  115.  
  116. jTextField6.setText(resourceMap.getString("jTextField6.text")); // NOI18N
  117. jTextField6.setName("jTextField6"); // NOI18N
  118.  
  119. jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
  120. jLabel6.setName("jLabel6"); // NOI18N
  121.  
  122. javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(project_ensemble_gui.Project_Ensemble_GUIApp.class).getContext().getActionMap(Project_Ensemble_GUIView.class, this);
  123. jButton1.setAction(actionMap.get("RunCalculation")); // NOI18N
  124. jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
  125. jButton1.setName("jButton1"); // NOI18N
  126. jButton1.addActionListener(new java.awt.event.ActionListener() {
  127. public void actionPerformed(java.awt.event.ActionEvent evt) {
  128. RunCalculationButtonActionPerformed(evt);
  129. }
  130. });
  131.  
  132. org.jdesktop.layout.GroupLayout mainPanelLayout = new org.jdesktop.layout.GroupLayout(mainPanel);
  133. mainPanel.setLayout(mainPanelLayout);
  134. mainPanelLayout.setHorizontalGroup(
  135. mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  136. .add(mainPanelLayout.createSequentialGroup()
  137. .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  138. .add(mainPanelLayout.createSequentialGroup()
  139. .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  140. .add(org.jdesktop.layout.GroupLayout.TRAILING, mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  141. .add(jTextField4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
  142. .add(jTextField3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
  143. .add(jTextField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
  144. .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE))
  145. .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
  146. .add(jTextField5)
  147. .add(jTextField6)))
  148. .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  149. .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  150. .add(mainPanelLayout.createSequentialGroup()
  151. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  152. .add(jLabel6))
  153. .add(org.jdesktop.layout.GroupLayout.TRAILING, mainPanelLayout.createSequentialGroup()
  154. .add(6, 6, 6)
  155. .add(jLabel5)))
  156. .add(org.jdesktop.layout.GroupLayout.TRAILING, mainPanelLayout.createSequentialGroup()
  157. .add(18, 18, 18)
  158. .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  159. .add(jLabel1)
  160. .add(jLabel2)
  161. .add(jLabel3)
  162. .add(jLabel4)))))
  163. .add(jButton1))
  164. .addContainerGap())
  165. );
  166. mainPanelLayout.setVerticalGroup(
  167. mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  168. .add(mainPanelLayout.createSequentialGroup()
  169. .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
  170. .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  171. .add(jLabel1))
  172. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  173. .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
  174. .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  175. .add(jLabel2))
  176. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  177. .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
  178. .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  179. .add(jLabel3))
  180. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  181. .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
  182. .add(jTextField4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  183. .add(jLabel4))
  184. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  185. .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
  186. .add(jTextField5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  187. .add(jLabel5))
  188. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
  189. .add(mainPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
  190. .add(jTextField6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  191. .add(jLabel6))
  192. .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
  193. .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
  194. .add(28, 28, 28))
  195. );
  196.  
  197. menuBar.setName("menuBar"); // NOI18N
  198.  
  199. fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
  200. fileMenu.setName("fileMenu"); // NOI18N
  201.  
  202. dataSetMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
  203. dataSetMenuItem.setText(resourceMap.getString("dataSetMenuItem.text")); // NOI18N
  204. dataSetMenuItem.setName("dataSetMenuItem"); // NOI18N
  205. dataSetMenuItem.addActionListener(new java.awt.event.ActionListener() {
  206. public void actionPerformed(java.awt.event.ActionEvent evt) {
  207. dataSetMenuItemActionPerformed(evt);
  208. }
  209. });
  210. fileMenu.add(dataSetMenuItem);
  211.  
  212. exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
  213. exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
  214. exitMenuItem.setName("exitMenuItem"); // NOI18N
  215. fileMenu.add(exitMenuItem);
  216.  
  217. menuBar.add(fileMenu);
  218.  
  219. helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
  220. helpMenu.setName("helpMenu"); // NOI18N
  221.  
  222. aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
  223. aboutMenuItem.setName("aboutMenuItem"); // NOI18N
  224. helpMenu.add(aboutMenuItem);
  225.  
  226. menuBar.add(helpMenu);
  227.  
  228. setComponent(mainPanel);
  229. setMenuBar(menuBar);
  230. }// </editor-fold>
  231.  
  232. public static double getAverage(double[] numbers) {
  233. double total = 0.0;
  234. for (int col = 0; col<numbers.length; col++){
  235. if (!Double.isNaN(numbers[col])) {
  236. total +=numbers[col];
  237. }
  238. }
  239. return total/numbers.length;
  240. }
  241.  
  242. public static void printArray(int[][] array) {
  243. System.out.println("======================================");
  244. for (int row =0;row<array.length;row++){
  245. for (int col=0;col<array[row].length;col++){
  246. System.out.print(array[row][col] +" ");
  247. }
  248. System.out.print("\n");
  249. }
  250. }
  251.  
  252.  
  253. public static void saveArray(String filename, int[][] output) {
  254. try{
  255. PrintWriter pri = new PrintWriter(filename);
  256. for (int row =0;row<output.length;row++){
  257. for (int col=0;col<output[row].length;col++){
  258. pri.print(output[row][col] +" ");
  259. }
  260. pri.print("\n");
  261. }
  262. pri.close();
  263. }catch (Exception e) {}
  264. }
  265.  
  266. private void dataSetMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
  267. JFileChooser jfc = new JFileChooser();
  268. jfc.showOpenDialog(jfc);
  269. f = jfc.getSelectedFile();
  270. wasOpen = true;
  271. }
  272.  
  273. public void ShowDialogBox(){
  274. JFrame frame = new JFrame("Show Message Dialog");
  275. JOptionPane.showMessageDialog(frame,"Average accuracy: " + answer);
  276. }
  277.  
  278.  
  279. private void RunCalculationButtonActionPerformed(java.awt.event.ActionEvent evt) {
  280. if(wasOpen==false){
  281. dataSetMenuItemActionPerformed(evt);
  282. }
  283. Dataset data = null;
  284. try {
  285. data = (Dataset) ARFFHandler.loadARFF(f,Integer.parseInt(jTextField6.getText()));
  286. } catch (FileNotFoundException ex) {
  287. Logger.getLogger(Project_Ensemble_GUIView.class.getName()).log(Level.SEVERE, null, ex);
  288. }
  289.  
  290. int n = Integer.parseInt(jTextField1.getText()); //no of repetitions (1, infty)
  291. double trainSize = Double.parseDouble(jTextField2.getText()); //size of training dataset (% of original dataset) (0.0, 1.0]
  292. int b = Integer.parseInt(jTextField3.getText()); //no of bootstrap rounds to create the ensemble (1, infty)
  293. double E = Double.parseDouble(jTextField4.getText());; //% of features to eliminate at each iteration of RFE (0, 1]
  294. double kPer = Double.parseDouble(jTextField5.getText());; //% of best (lowest) ranked attributes to attain (0.0 ,1.0]
  295.  
  296.  
  297. int rand = 0;
  298. double ACC[] = new double[n];
  299. int k = data.noAttributes();
  300. int ranks[][] = new int[n][k];
  301.  
  302.  
  303. for (int j=0;j<n;j++){//BEGIN: main loop
  304.  
  305. // Split the data
  306. Dataset data2 = data.copy();
  307. Dataset data3 = data.copy();
  308.  
  309. Dataset dataTest = data2;
  310. Dataset dataTrain = data3;
  311.  
  312.  
  313. for (int s=0; s<data.size(); s++){
  314. if(dataTrain.size()>(int)Math.round(data.size()*trainSize)){
  315. rand = (int)(Math.random()*(dataTrain.size()));
  316. dataTest.set(s, dataTrain.get(rand));
  317. dataTrain.remove(rand);
  318. } else {
  319. dataTest.remove(dataTest.size()-1);
  320. }
  321. }
  322.  
  323.  
  324. // Create a feature ranking algorithm
  325. RecursiveFeatureEliminationSVM[] svmrfes = new RecursiveFeatureEliminationSVM[40];
  326.  
  327. for (int i = 0; i < svmrfes.length; i++){
  328. svmrfes[i] = new RecursiveFeatureEliminationSVM(E);
  329. }
  330.  
  331. // Build the ensemble
  332. LinearRankingEnsemble ensemble = new LinearRankingEnsemble(svmrfes);
  333. ensemble.build(dataTrain);
  334.  
  335.  
  336. int Aranks[] = new int[k];
  337. for (int i = 0; i < ensemble.noAttributes(); i++) {
  338. ranks[j][i] = ensemble.rank(i);
  339. Aranks[i] = ranks[j][i];
  340. }
  341.  
  342. // Select the attributes with lowest ranks
  343. int counter = 0;
  344. Dataset newDataTrain = dataTrain.copy();
  345. Dataset newDataTest = dataTest.copy();
  346.  
  347. for(int row=0; row<newDataTrain.size(); row++){
  348. counter = 0;
  349. for(int g=0; g<Aranks.length; g++){
  350. if(Aranks[g]>(int)Math.ceil(kPer*k)){
  351. newDataTrain.get(row).removeAttribute(g-counter);
  352. if(newDataTest.size()>row){
  353. newDataTest.get(row).removeAttribute(g-counter);
  354. }
  355. counter++;
  356. }
  357. }
  358. }
  359.  
  360. // Train classifier on attained attributes
  361. Classifier cl = new LibSVM();
  362. cl.buildClassifier(newDataTrain);
  363.  
  364. // Test the classifier, compute accuracy
  365. int correct = 0, wrong = 0;
  366. for (Instance inst : newDataTest) {
  367. Object predictedClassValue = cl.classify(inst);
  368. Object realClassValue = inst.classValue();
  369. if (predictedClassValue.equals(realClassValue))
  370. correct++;
  371. else
  372. wrong++;
  373. }
  374. ACC[j] = correct/(correct + wrong);
  375.  
  376. }//END: main loop
  377.  
  378. // Save ranks from all repetitions to file
  379. /*
  380. saveArray("ranks.data", ranks);
  381. */
  382. // Print ranks from all repetitions
  383. /*
  384. printArray(ranks);
  385. */
  386.  
  387. answer = getAverage(ACC);
  388. // for (int i=0;i<ACC.length;i++){
  389. // System.out.println(ACC[i]);
  390. // }
  391. //
  392. //
  393. // System.out.println("======================================");
  394. // System.out.println("Average accuracy: " + getAverage(ACC));
  395. // System.out.println("======================================");
  396.  
  397. ShowDialogBox();
  398. }
  399.  
  400.  
  401. // Variables declaration - do not modify
  402. private javax.swing.JMenuItem dataSetMenuItem;
  403. private javax.swing.JButton jButton1;
  404. private javax.swing.JLabel jLabel1;
  405. private javax.swing.JLabel jLabel2;
  406. private javax.swing.JLabel jLabel3;
  407. private javax.swing.JLabel jLabel4;
  408. private javax.swing.JLabel jLabel5;
  409. private javax.swing.JLabel jLabel6;
  410. private javax.swing.JTextField jTextField1;
  411. private javax.swing.JTextField jTextField2;
  412. private javax.swing.JTextField jTextField3;
  413. private javax.swing.JTextField jTextField4;
  414. private javax.swing.JTextField jTextField5;
  415. private javax.swing.JTextField jTextField6;
  416. private javax.swing.JPanel mainPanel;
  417. private javax.swing.JMenuBar menuBar;
  418. // End of variables declaration
  419.  
  420.  
  421. private JDialog aboutBox;
  422.  
  423. private File f;
  424. private boolean wasOpen = false;
  425. private double answer;
  426. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement