Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Evan Fabry
- * CS 440
- * MP3 - NLP Document Classification/Information Retrieval
- */
- import weka.core.converters.ConverterUtils.DataSource;
- import weka.core.converters.ConverterUtils.*;
- //import weka.core.*;
- import weka.core.Instance;
- import weka.core.Instances;
- import weka.classifiers.functions.*;
- import weka.core.converters.AbstractSaver;
- import weka.core.converters.AbstractFileSaver;
- //import weka.core.converters.ArffSaver;
- //import weka.filters.supervised.instance.*;
- import weka.filters.supervised.instance.Resample;
- import weka.filters.Filter;
- import weka.filters.unsupervised.attribute.RemoveUseless;
- import weka.filters.unsupervised.attribute.RandomProjection;
- import weka.core.Utils.*;
- import weka.classifiers.Evaluation;
- import weka.classifiers.functions.Logistic;
- import weka.classifiers.functions.SMO;
- import weka.classifiers.functions.MultilayerPerceptron;
- //import weka.classifiers.AbstractClassifier;
- import weka.classifiers.Classifier;
- import java.util.*;
- import java.io.*;
- import java.lang.Object;
- import java.lang.Math;
- class LearnerThread extends Thread {
- private Thread t;
- private Instances thread_instances;
- private int learner_type;
- private Classifier cla;
- LearnerThread( int type, Instances inst, Classifier c ){
- this.learner_type = type;
- this.thead_instances = inst;
- this.cla = c;
- File output = new File("out"+Integer.toString(this.learner_type)+".txt");
- System.out.println("Creating " + Integer.toString(type) );
- }
- public void run() {
- System.out.println("Running " + Integer.toString(learner_type) );
- try {
- Evaluation eval = new Evaluation(thread_instances);
- eval.crossValidateModel(this.cla, this.thead_instances, 10, new Random(1));
- System.out.println(eval.toSummaryString());
- } catch (InterruptedException e) {
- System.out.println("Thread " + Integer.toString(this.learner_type) + " interrupted.");
- }
- System.out.println("Thread " + Integer.toString(this.learner_type) + " exiting.");
- }
- public void start ()
- {
- System.out.println("Starting " + Integer.toString(this.learner_type));
- if (t == null)
- {
- t = new Thread (this, Integer.toString(this.learner_type));
- t.start ();
- }
- }
- }
- public class efabry2 {
- public static void main(String [] args) throws Exception
- {
- DataSource tf1_train_source = new DataSource("MP3Part1Solution/data/train.tf1.arff");
- //DataSource tf2_train_source = new DataSource("MP3Part1Solution/data/train.tf2.arff");
- //DataSource tf_idf_train_source = new DataSource("MP3Part1Solution/data/train.tfidf.arff");
- //DataSource idf_train_source = new DataSource("MP3Part1Solution/data/train.idf.arff");
- Instances tf1_train_all = tf1_train_source.getDataSet();
- //Instances tf2_train_all = tf2_train_source.getDataSet();
- //Instances tf_idf_train_all = tf_idf_train_source.getDataSet();
- //Instances idf_train_all = idf_train_source.getDataSet();
- tf1_train_all.setClassIndex(tf1_train_all.numAttributes() - 1);
- //tf2_train_all.setClassIndex(tf2_train_all.numAttributes() - 1);
- //tf_idf_train_all.setClassIndex(tf_idf_train_all.numAttributes() - 1);
- //idf_train_all.setClassIndex(idf_train_all.numAttributes() - 1);
- for (int k = 0; k < 3; k++) {
- int percent = 100;
- if (k == 0) { percent = 20; }
- else if (k == 1) { percent = 60; }
- System.out.print("starting loop with percent = ");
- System.out.println(percent);
- RemoveUseless ru = new RemoveUseless();
- RandomProjection rp = new RandomProjection();
- Resample rs = new Resample();
- rp.setOptions( weka.core.Utils.splitOptions( "-N 1000" ));
- rs.setOptions( weka.core.Utils.splitOptions( "-Z "+Integer.toString(percent) ));
- rp.setInputFormat(tf1_train_all);
- ru.setInputFormat(tf1_train_all);
- Instances tf1_train = Filter.useFilter(tf1_train_all, ru);
- tf1_train = Filter.useFilter(tf1_train, rp);
- rs.setInputFormat(tf1_train);
- tf1_train = Filter.useFilter(tf1_train, rs);
- //.resample().setOptions( resample_options ).removeUseless().randomProjection().setOptions("-N 1000");
- //tf2_train[k].resample().setOptions( "-Z "+Integer.toString(percent) ).removeUseless().randomProjection().setOptions("-N 1000");
- //tf_idf_train[k].resample().setOptions( "-Z "+Integer.toString(percent) ).removeUseless().randomProjection().setOptions("-N 1000");
- //idf_train[k].resample().setOptions( "-Z "+Integer.toString(percent) ).removeUseless().randomProjection().setOptions("-N 1000");
- System.out.println("evaluating");
- MultilayerPerceptron slp = new MultilayerPerceptron();
- slp.setHiddenLayers("0");
- MultilayerPerceptron mlp = new MultilayerPerceptron();
- mlp.setHiddenLayers("5");
- SMO smo = new SMO();
- Logistic log= new Logisitic();
- LearnerThread T1 = new LearnerThread(0, tf1_train, slp);
- LearnerThread T2 = new LearnerThread(1, tf1_train, mlp);
- LearnerThread T3 = new LearnerThread(2, tf1_train, smo);
- LearnerThread T4 = new LearnerThread(2, tf1_train, log);
- T1.start();
- T2.start();
- T3.start();
- T4.start();
- T1.join(10000);
- T2.join(10000);
- T3.join(10000);
- T4.join(10000);
- //Evaluation eval = new Evaluation(tf1_train);
- //eval.crossValidateModel(slp, tf1_train, 10, new Random(1));
- //System.out.println(eval.toSummaryString());
- //System.out.println(eval.toClassDetailsString());
- //System.out.println(eval.toMatrixString());
- //System.out.println(out.getAttributes());
- System.out.print("ending loop with percent = ");
- System.out.println(percent);
- //tf1_train[k] = NULL;
- //tf2_train[k] = NULL;
- //tf_idf_train[k] = NULL;
- //idf_train[k] = NULL
- }
- //PreprocessPanel instances = new PreprocessPanel();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement