Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Help in recoding more efficiently

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help in recoding 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
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help in recoding more efficiently

    Welcome! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers. Unfortunately, doing it correctly will prevent one using colored code, but then you can comment the code you wish to highlight.

    As for executing code at a timed interval, I recommend you look into using a Java util.Timer. You can find info on that and examples on how to use it in the Java Tutorials.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help in recoding more efficiently

    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Dec 2013
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help in recoding more efficiently

    Quote Originally Posted by GregBrannon View Post
    Welcome! Please read this topic to learn how to post code in code or highlight tags and other useful info for newcomers. Unfortunately, doing it correctly will prevent one using colored code, but then you can comment the code you wish to highlight.

    As for executing code at a timed interval, I recommend you look into using a Java util.Timer. You can find info on that and examples on how to use it in the Java Tutorials.
    Hi Greg

    Executing code at a timed interval is NOT my problem............
    I just wish to extract the highlighted code so that it runs once only

    Thanks

    Bob M

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help in recoding more efficiently

    Use a one time boolean:
      boolean oneTime = true;
       ...
      if(oneTime) {
         oneTime = false;
         // do the stuff here
      }  // end one time code
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Dec 2013
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help in recoding more efficiently

    Quote Originally Posted by Norm View Post
    Use a one time boolean:
      boolean oneTime = true;
       ...
      if(oneTime) {
         oneTime = false;
         // do the stuff here
      }  // end one time code
    Hi Norm

    That is exactly what I have already tried to do but I am having difficulty grasping how to refer to each of the seven classifiers outside the once-only part of code ???

    Bob M

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help in recoding more efficiently

    Define the variables so that their scope covers all the places that the code needs access to them.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Dec 2013
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help in recoding more efficiently

    Quote Originally Posted by Norm View Post
    Define the variables so that their scope covers all the places that the code needs access to them.
    Could you elaborate on your statement with examples please

    Bob M

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help in recoding more efficiently

    Do you understand what the "scope" of a variable's definition means? It has to do with the closest pair of {}s that surround it. A variable's definition is out of scope outside of the {}s that surround its definition.

    I use definition the same as some use declaration. The following defines the variable x as an int:
    int x;  //  define an int variable


    A variable can be defined in one place and given a value in another. Local variables will need to be given a value when they are defined.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Dec 2013
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help in recoding more efficiently

    Quote Originally Posted by Norm View Post
    Do you understand what the "scope" of a variable's definition means? It has to do with the closest pair of {}s that surround it. A variable's definition is out of scope outside of the {}s that surround its definition.

    I use definition the same as some use declaration. The following defines the variable x as an int:
    int x;  //  define an int variable


    A variable can be defined in one place and given a value in another. Local variables will need to be given a value when they are defined.
    hi Norm

    When I encapsulate the red code in a once-only loop I run into problems............

    the very next line of code is fc.buildClassifier(training);

    but the 'fc' no longer is in scope ???

    Bob M

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help in recoding more efficiently

    Where is fc defined relative to where it is being used? Are those two source lines within the same {} pair?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Dec 2013
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help in recoding more efficiently

    Quote Originally Posted by Norm View Post
    Where is fc defined relative to where it is being used? Are those two source lines within the same {} pair?
    Initially, the 'fc' forms part of the once-only code and so is encapsulated within this code
    But I am using fc..... outside this {}

    I therefore took the fc definition line out of the once-only code and put it just above the once-only code start

    My code is now as follows:-
    // 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());
     
    FilteredClassifier fc = new FilteredClassifier(); // Create a FilteredClassifier object
     
    if (oneTime) {
     
    oneTime = false;
    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
     
    ((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);
     
    } // end of oneTime loop
     
    fc.buildClassifier(training);
     
    // test the model
    Evaluation eval = new Evaluation(training);
    eval.evaluateModel(fc, testing);
     
    // print the results a la weka Explorer: etc

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help in recoding more efficiently

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.


    Does the program work like you want it to now?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Dec 2013
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help in recoding more efficiently

    Quote Originally Posted by Norm View Post
    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.


    Does the program work like you want it to now?
    In a word - No!

    The first iteration (time interval) is perfect
    Thereafter it is all wrong

    p.s. normally I wouldn't worry about repeating code over and over but the classifier LibSVM has a 'bug' I think
    On the first iteration the 'S' option is set to '3' as per my array
    Thereafter the 'S' option is set to '0' even tho I am reading the same array details

    Oh well

    Bob M

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help in recoding more efficiently

    Sorry, I don't know anything about "classifier"s
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help in recoding more efficiently

    Also posted at: Help required to code more efficiently - Dev Shed
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Downloading files efficiently
    By angstrem in forum Java Theory & Questions
    Replies: 8
    Last Post: June 9th, 2013, 05:07 AM
  2. how to data parse efficiently
    By coder13 in forum JDBC & Databases
    Replies: 0
    Last Post: April 4th, 2013, 06:12 PM
  3. How to use MappedByteBuffer efficiently with extremely large files
    By Vyse in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 13th, 2012, 08:44 PM
  4. Efficiently processing data ....
    By rdegrijs in forum Java Servlet
    Replies: 0
    Last Post: July 10th, 2012, 06:25 PM
  5. Using arrays more efficiently in Java?
    By mjballa in forum Java Theory & Questions
    Replies: 1
    Last Post: February 4th, 2012, 08:53 PM