Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9253427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:12:56+00:00 2026-06-18T11:12:56+00:00

I have written the code to create the model and save it. It works

  • 0

I have written the code to create the model and save it. It works fine. My understanding is data, by default, is split in 10 folds. I want data to be split into two sets (training and testing) when I create the model. On Weka UI, I can do it by using “Percentage split” radio button. I want to know how to do it through code. I want it to be split in two parts 80% being the training and 20% being the testing. Here is my code.

        FilteredClassifier model = new FilteredClassifier();
        model.setFilter(new StringToWordVector());
        model.setClassifier(new NaiveBayesMultinomial());
        try {
            model.buildClassifier(trainingSet);
        } catch (Exception e1) { // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        ObjectOutputStream oos = new ObjectOutputStream(
                new FileOutputStream(
                        "/Users/me/models/MyModel.model"));
        oos.writeObject(model);
        oos.flush();
        oos.close();

trainingSet here is already populated Instances object. Can someone help me with this?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:12:58+00:00Added an answer on June 18, 2026 at 11:12 am

    In the UI class ClassifierPanel‘s method startClassifier(), I found the following code:

    // Percent split
    
    int trainSize = (int) Math.round(inst.numInstances() * percent
        / 100);
    int testSize = inst.numInstances() - trainSize;
    Instances train = new Instances(inst, 0, trainSize);
    Instances test = new Instances(inst, trainSize, testSize);
    

    so after randomizing your dataset…

    trainingSet.randomize(new java.util.Random(0));
    

    … I suggest you split your trainingSetin the same way:

    int trainSize = (int) Math.round(trainingSet.numInstances() * 0.8);
    int testSize = trainingSet.numInstances() - trainSize;
    Instances train = new Instances(trainingSet, 0, trainSize);
    Instances test = new Instances(trainingSet, trainSize, testSize);
    

    then use Classifier#buildClassifier(Instances data) to train the classifier with 80% of your set instances:

    model.buildClassifier(train);
    

    UPDATE: thanks to @ChengkunWu’s answer, I added the randomizing step above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this code in JavaScript and works perfectly fine when I include
I have written code in Java to access web cam,and to save image... I
I have written a code for taking JSON data from a PHP and putting
I have a model which id is a code that must be written by
I have a code snippet in C# which i want to convert into VB.Net
I have some code written like so: class Invite(models.Model): STATE_UNKNOWN = 0 STATE_WILL_PLAY =
I have written a multi threaded java code, which when runs creates 8 threads
I have written code that opens 16 figures at once. Currently, they all open
I have written code to perform a function that could take a while to
Hi I have written code like this @Id @Column(nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) public int getUserID() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.