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 8095787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:15:02+00:00 2026-06-05T21:15:02+00:00

I need to iteratively extend a weka ARFF file with SparseInstance objects. Each time

  • 0

I need to iteratively extend a weka ARFF file with SparseInstance objects. Each time a new SparseInstance is added the header might change since the new Instance might add additional attributes. I thought the mergeInstances method would solve my problem but it does not. It requires both dataset to have no shared attributes.

If this is not absolutely clear look at the following example:

Dataset1
a b c
1 2 3
4 5 6

Dataset2
c d
7 8

Merged result:
a b c d
1 2 3 ?
4 5 6 ?
? ? 7 8

The only solution I see at the moment is parsing the arff file by hand and merging it using String processing. Does anyone know of a better solution?

  • 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-05T21:15:04+00:00Added an answer on June 5, 2026 at 9:15 pm

    Ok. I found the solution myself. The central part of the solution is the method Instances#insertAttributeAt, which inserts a new attribute as the last one if the second parameter is model.numAttributes(). Here is some example code for numerical attributes. It is easy to adapt to other types of attributes as well:

        Map<String,String> currentInstanceFeatures = currentInstance.getFeatures();
        Instances model = null;
        try {
            if (targetFile.exists()) {
                FileReader in = new FileReader(targetFile);
                try {
                    BufferedReader reader = new BufferedReader(in);
                    ArffReader arff = new ArffReader(reader);
                    model = arff.getData();
                } finally {
                    IOUtils.closeQuietly(in);
                }
            } else {
                FastVector schema = new FastVector();
                model = new Instances("model", schema, 1);
            }
            Instance newInstance = new SparseInstance(0);
            newInstance.setDataset(model);
    
            for(Map.Entry<String,String> feature:currentInstanceFeatures.entrySet()) {
                Attribute attribute = model.attribute(feature.getKey());
                    if (attribute == null) {
                        attribute = new Attribute(feature.getKey());
                        model.insertAttributeAt(attribute, model.numAttributes());
                        attribute = model.attribute(feature.getKey());
                    }
                newInstance.setValue(attribute, feature.getValue());
            }
    
            model.add(newInstance);
            model.compactify();
            ArffSaver saver = new ArffSaver();
            saver.setInstances(model);
            saver.setFile(targetFile);
            LOGGER.debug("Saving dataset to: " + targetFile.getAbsoluteFile());
            saver.writeBatch();
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to calculate permutations iteratively. The method signature looks like: int[][] permute(int n)
Need to apply a filter to a file like this: TUPAC_0006:1:1:2554:2356#0/1 0 * 0
I am very new to regular expressions, and I need some help. First I
I need to store a growing large number of objects in a collection. While
I need to selectively revert a few lines in a massive file (to be
I'm doing an iterative computation on a flow network during which I need to
Need a map reduce function by mongo in php This my mongo structure [_id]
Need some regular expressions help. So far I have my code working to allow
Need help, function getFamily() { FB.api('/me/family', function(response) { alert(JSON.stringify(response)); }); } With the above
Need just a push in the right direction with this. I'm building a multi-language

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.