#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    Help required to code more efficiently


    Hi

    I am very new to Java and need help in recoding my program
    At the moment I have a patch of code which is performed every 6 hours
    Part of this code needs only to be performed ONCE!
    But I am having the greatest difficulty in separating the once-only portion out......
    I have marked the once-only portion in red

    This is the relevant portions of code.............
    ********************************************

    // create an array of weka filters - select attributes to be unused - choices for each classifier
    int[][] filtersArray = new int[][] {
    { 1, 3, 5, 6, 8, 10}, // KStar
    { 1, 3, 5, 6, 8, 10}, // J48
    { 1, 3, 5, 6, 7, 8, 10}, // JRip
    { 1, 3, 6, 7, 8, 9, 10}, // NaiveBayes
    { 1, 2, 3, 5, 6, 8, 10}, // LMT
    { 1, 3, 5, 6, 8, 11}, // KStar
    { 2, 4, 7, 9, 11} // LibSVM
    };

    // create an array of the number of the class attribute for each classifier prior to filtering
    int[] classAttributeArray = new int[] {
    12, 12, 12, 12, 12, 11, 11};

    // create strings of weka options - choices for each classifier
    String[][] optionsArray = new String[][] {
    weka.core.Utils.splitOptions("-B 35 -M a"), // KStar
    weka.core.Utils.splitOptions("-C 0.25 -M 2"), // J48
    weka.core.Utils.splitOptions("--F 3 -N 2.0 -0 2 -S 1 -E"), // JRip
    weka.core.Utils.splitOptions(""), // NaiveBayes
    weka.core.Utils.splitOptions("-I -1 -M 15 -W 0.0 -A"), // LMT
    weka.core.Utils.splitOptions("-B 35 -M a"), // KStar
    weka.core.Utils.splitOptions("-S 3 -K 2 -D 3 -G 0.0 -R 0.0 -N 0.5 -M 40.0 -C 1.0 -E 0.0010 -P 0.1") // LibSVM
    };

    //************************************************** *****
    private class WekaApp {

    public BufferedReader readDataFile(String filename) {
    BufferedReader inputReader = null;

    return inputReader;
    }

    void doInit() throws Exception {

    BufferedReader datafile = readDataFile("C:/Databases/us_copiosus");

    InstanceQuery query = new InstanceQuery();
    query.setQuery("SELECT * from USD_JPY");
    Instances data = query.retrieveInstances();

    // Split instances into training and testing (the split percentage is 97.56%)
    double percent = 97.56;
    int trainingSize = (int) Math.round(data.numInstances() * percent / 100);
    int testingSize = data.numInstances() - trainingSize;
    Instances training = new Instances(data, 0, trainingSize);
    Instances testing = new Instances(data, trainingSize, testingSize);

    // Choose a set of classifiers
    Classifier[] models = new Classifier[] {
    new KStar(),
    new J48(),
    new JRip(),
    new NaiveBayes(),
    new LMT(),
    new KStar(),
    new LibSVM()
    };

    Predicted_Trend = 0;

    // Run for each classifier model
    for(int j = 0; j < models.length; j++) {

    training.setClassIndex(classAttributeArray[j]);
    testing.setClassIndex(classAttributeArray[j]);

    myConsole.getOut().println("ClassIndex: "+ training.classAttribute().name());


    Remove filter = new Remove(); // First, we create the base object.
    filter.setAttributeIndicesArray(filtersArray[j]); // Finally, we provide an array of integer indexes.

    //build classifier - do this once only for each classifier

    FilteredClassifier fc = new FilteredClassifier(); // Create a FilteredClassifier object
    ((OptionHandler)models[j]).setOptions(optionsArray[j]);

    try{
    myConsole.getOut().println("Options4: "+ Arrays.toString(((OptionHandler)models[j]).getOptions()));
    }
    catch(Exception e)
    {
    myConsole.getOut().println(e);
    }

    fc.setClassifier(models[j]);

    fc.setFilter(filter);


    fc.buildClassifier(training);

    // test the model
    Evaluation eval = new Evaluation(training);
    eval.evaluateModel(fc, testing);

    // print the results a la weka Explorer: etc
    *****************************************

    Any suggestions / help much appreciated

    Bob M
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,719
    Rep Power
    348

IMN logo majestic logo threadwatch logo seochat tools logo