Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. /**
  2. * @author Evan Fabry
  3. * CS 440
  4. * MP3 - NLP Document Classification/Information Retrieval
  5. */
  6.  
  7.  
  8. import weka.core.converters.ConverterUtils.DataSource;
  9. import weka.core.converters.ConverterUtils.*;
  10. //import weka.core.*;
  11. import weka.core.Instance;
  12. import weka.core.Instances;
  13. import weka.classifiers.functions.*;
  14. import weka.core.converters.AbstractSaver;
  15. import weka.core.converters.AbstractFileSaver;
  16. //import weka.core.converters.ArffSaver;
  17. //import weka.filters.supervised.instance.*;
  18. import weka.filters.supervised.instance.Resample;
  19. import weka.filters.Filter;
  20. import weka.filters.unsupervised.attribute.RemoveUseless;
  21. import weka.filters.unsupervised.attribute.RandomProjection;
  22. import weka.core.Utils.*;
  23. import weka.classifiers.Evaluation;
  24. import weka.classifiers.functions.Logistic;
  25. import weka.classifiers.functions.SMO;
  26. import weka.classifiers.functions.MultilayerPerceptron;
  27. //import weka.classifiers.AbstractClassifier;
  28. import weka.classifiers.Classifier;
  29.  
  30. import java.util.*;
  31. import java.io.*;
  32. import java.lang.Object;
  33. import java.lang.Math;
  34.  
  35.  
  36. class LearnerThread extends Thread {
  37. private Thread t;
  38. private Instances thread_instances;
  39. private int learner_type;
  40. private Classifier cla;
  41.  
  42. LearnerThread( int type, Instances inst, Classifier c ){
  43. this.learner_type = type;
  44. this.thead_instances = inst;
  45. this.cla = c;
  46. File output = new File("out"+Integer.toString(this.learner_type)+".txt");
  47. System.out.println("Creating " + Integer.toString(type) );
  48. }
  49. public void run() {
  50. System.out.println("Running " + Integer.toString(learner_type) );
  51. try {
  52. Evaluation eval = new Evaluation(thread_instances);
  53. eval.crossValidateModel(this.cla, this.thead_instances, 10, new Random(1));
  54. System.out.println(eval.toSummaryString());
  55. } catch (InterruptedException e) {
  56. System.out.println("Thread " + Integer.toString(this.learner_type) + " interrupted.");
  57. }
  58. System.out.println("Thread " + Integer.toString(this.learner_type) + " exiting.");
  59. }
  60.  
  61. public void start ()
  62. {
  63. System.out.println("Starting " + Integer.toString(this.learner_type));
  64. if (t == null)
  65. {
  66. t = new Thread (this, Integer.toString(this.learner_type));
  67. t.start ();
  68. }
  69. }
  70.  
  71. }
  72.  
  73. public class efabry2 {
  74. public static void main(String [] args) throws Exception
  75. {
  76. DataSource tf1_train_source = new DataSource("MP3Part1Solution/data/train.tf1.arff");
  77. //DataSource tf2_train_source = new DataSource("MP3Part1Solution/data/train.tf2.arff");
  78. //DataSource tf_idf_train_source = new DataSource("MP3Part1Solution/data/train.tfidf.arff");
  79. //DataSource idf_train_source = new DataSource("MP3Part1Solution/data/train.idf.arff");
  80.  
  81. Instances tf1_train_all = tf1_train_source.getDataSet();
  82. //Instances tf2_train_all = tf2_train_source.getDataSet();
  83. //Instances tf_idf_train_all = tf_idf_train_source.getDataSet();
  84. //Instances idf_train_all = idf_train_source.getDataSet();
  85.  
  86. tf1_train_all.setClassIndex(tf1_train_all.numAttributes() - 1);
  87. //tf2_train_all.setClassIndex(tf2_train_all.numAttributes() - 1);
  88. //tf_idf_train_all.setClassIndex(tf_idf_train_all.numAttributes() - 1);
  89. //idf_train_all.setClassIndex(idf_train_all.numAttributes() - 1);
  90. for (int k = 0; k < 3; k++) {
  91.  
  92. int percent = 100;
  93. if (k == 0) { percent = 20; }
  94. else if (k == 1) { percent = 60; }
  95. System.out.print("starting loop with percent = ");
  96. System.out.println(percent);
  97.  
  98. RemoveUseless ru = new RemoveUseless();
  99. RandomProjection rp = new RandomProjection();
  100. Resample rs = new Resample();
  101. rp.setOptions( weka.core.Utils.splitOptions( "-N 1000" ));
  102. rs.setOptions( weka.core.Utils.splitOptions( "-Z "+Integer.toString(percent) ));
  103.  
  104. rp.setInputFormat(tf1_train_all);
  105. ru.setInputFormat(tf1_train_all);
  106. Instances tf1_train = Filter.useFilter(tf1_train_all, ru);
  107. tf1_train = Filter.useFilter(tf1_train, rp);
  108. rs.setInputFormat(tf1_train);
  109. tf1_train = Filter.useFilter(tf1_train, rs);
  110. //.resample().setOptions( resample_options ).removeUseless().randomProjection().setOptions("-N 1000");
  111. //tf2_train[k].resample().setOptions( "-Z "+Integer.toString(percent) ).removeUseless().randomProjection().setOptions("-N 1000");
  112. //tf_idf_train[k].resample().setOptions( "-Z "+Integer.toString(percent) ).removeUseless().randomProjection().setOptions("-N 1000");
  113. //idf_train[k].resample().setOptions( "-Z "+Integer.toString(percent) ).removeUseless().randomProjection().setOptions("-N 1000");
  114. System.out.println("evaluating");
  115.  
  116. MultilayerPerceptron slp = new MultilayerPerceptron();
  117. slp.setHiddenLayers("0");
  118.  
  119. MultilayerPerceptron mlp = new MultilayerPerceptron();
  120. mlp.setHiddenLayers("5");
  121.  
  122. SMO smo = new SMO();
  123.  
  124. Logistic log= new Logisitic();
  125.  
  126. LearnerThread T1 = new LearnerThread(0, tf1_train, slp);
  127. LearnerThread T2 = new LearnerThread(1, tf1_train, mlp);
  128. LearnerThread T3 = new LearnerThread(2, tf1_train, smo);
  129. LearnerThread T4 = new LearnerThread(2, tf1_train, log);
  130. T1.start();
  131. T2.start();
  132. T3.start();
  133. T4.start();
  134.  
  135. T1.join(10000);
  136. T2.join(10000);
  137. T3.join(10000);
  138. T4.join(10000);
  139.  
  140. //Evaluation eval = new Evaluation(tf1_train);
  141. //eval.crossValidateModel(slp, tf1_train, 10, new Random(1));
  142. //System.out.println(eval.toSummaryString());
  143. //System.out.println(eval.toClassDetailsString());
  144. //System.out.println(eval.toMatrixString());
  145. //System.out.println(out.getAttributes());
  146. System.out.print("ending loop with percent = ");
  147. System.out.println(percent);
  148. //tf1_train[k] = NULL;
  149. //tf2_train[k] = NULL;
  150. //tf_idf_train[k] = NULL;
  151. //idf_train[k] = NULL
  152. }
  153.  
  154. //PreprocessPanel instances = new PreprocessPanel();
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement