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

  • SEARCH
  • Home
  • 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 8348235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:37:55+00:00 2026-06-09T07:37:55+00:00

I am using InstanceQuery , SQL queries, to construct my Instances . But my

  • 0

I am using InstanceQuery , SQL queries, to construct my Instances. But my query results does not come in the same order always as it is normal in SQL.
Beacuse of this Instances constucted from different SQL has different headers. A simple example can be seen below. I suspect my results changes because of this behavior.

Header 1

@attribute duration numeric
@attribute protocol_type {tcp,udp}
@attribute service {http,domain_u}
@attribute flag {SF}

Header 2

@attribute duration numeric
@attribute protocol_type {tcp}
@attribute service {pm_dump,pop_2,pop_3}
@attribute flag {SF,S0,SH}

My question is : How can I give correct header information to Instance construction.

Is something like below workflow is possible?

  1. get pre-prepared header information from arff file or another place.
  2. give instance construction this header information
  3. call sql function and get Instances (header + data)

I am using following sql function to get instances from database.

public static Instances getInstanceDataFromDatabase(String pSql
                                      ,String pInstanceRelationName){
    try {
        DatabaseUtils utils = new DatabaseUtils();

        InstanceQuery query = new InstanceQuery();

        query.setUsername(username);
        query.setPassword(password);
        query.setQuery(pSql);

        Instances data = query.retrieveInstances();
        data.setRelationName(pInstanceRelationName);

        if (data.classIndex() == -1)
        {
              data.setClassIndex(data.numAttributes() - 1);
        }
        return data;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
  • 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-09T07:37:57+00:00Added an answer on June 9, 2026 at 7:37 am

    I tried various approaches to my problem. But it seems that weka internal API does not allow solution to this problem right now. I modified weka.core.Instances append command line code for my purposes. This code is also given in this answer

    According to this, here is my solution. I created a SampleWithKnownHeader.arff file , which contains correct header values. I read this file with following code.

    public static Instances getSampleInstances() {
        Instances data = null;
        try {
            BufferedReader reader = new BufferedReader(new FileReader(
                    "datas\\SampleWithKnownHeader.arff"));
            data = new Instances(reader);
            reader.close();
            // setting class attribute
            data.setClassIndex(data.numAttributes() - 1);
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        } 
        return data;
    
    }
    

    After that , I use following code to create instances. I had to use StringBuilder and string values of instance, then I save corresponding string to file.

    public static void main(String[] args) {
    
        Instances SampleInstance = MyUtilsForWeka.getSampleInstances();
    
        DataSource source1 = new DataSource(SampleInstance);
    
        Instances data2 = InstancesFromDatabase
                .getInstanceDataFromDatabase(DatabaseQueries.WEKALIST_QUESTION1);
    
        MyUtilsForWeka.saveInstancesToFile(data2, "fromDatabase.arff");
    
        DataSource source2 = new DataSource(data2);
    
        Instances structure1;
        Instances structure2;
        StringBuilder sb = new StringBuilder();
        try {
            structure1 = source1.getStructure();
            sb.append(structure1);
            structure2 = source2.getStructure();
            while (source2.hasMoreElements(structure2)) {
                String elementAsString = source2.nextElement(structure2)
                        .toString();
                sb.append(elementAsString);
                sb.append("\n");
    
            }
    
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    
        MyUtilsForWeka.saveInstancesToFile(sb.toString(), "combined.arff");
    
    }
    

    My save instances to file code is as below.

    public static void saveInstancesToFile(String contents,String filename) {
    
         FileWriter fstream;
        try {
            fstream = new FileWriter(filename);
          BufferedWriter out = new BufferedWriter(fstream);
          out.write(contents);
          out.close();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    

    This solves my problem but I wonder if more elegant solution exists.

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

Sidebar

Related Questions

Using Python (2.7)and sqlite (3) I am trying to copy results of a query
Using SQL Server 2008 R2 we are looking for a way to select the
Using Microsoft SQL Server 2005, is there any way to see when a table
Using the navigator.geolocation object in JavaScript. Trying to establish accurate ranges, but wondering exactly
Using NSDateComponents I know how to get the day component, but this gives me
Using emacs on Ubuntu 11.10. I want to connect to a SQL Server database
Using SQL Server 2000 Table1 Id date --- ---------- 001 23/01/2012 002 25/01/2012 003
Using Yii, I want to delete all the rows that are not from today.
Using Xcode4.2.1, with a basic PhoneGap template based app. (I say template, but I
Using report builder 3.0, I have a report that queries a cube. How do

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.