tags:

views:

27

answers:

1

I'm using the Weka Java library to read in a CSV file and convert it to an ARFF file.

The problem is that the CSV file doesn't have a header row, only data. How do I assign attribute names after I bring in the CSV file? (all the columns would be string data types)

Here is the code I have so far:

    CSVLoader loader = new CSVLoader();
    loader.setSource(new File(CSVFilePath));
    Instances data = loader.getDataSet();

    ArffSaver saver = new ArffSaver();
    saver.setInstances(data);
    saver.setFile(new File(outputFilePath));
    saver.writeBatch();

I tried looking through the Weka source code to figure this out but I couldn't make heads or tails of it :-(

+1  A: 

The short answer is, you can't assign attribute names after you read in the file.

CSVLoader assumes the first line of the CSV is the header. If that's an instance, it will use that instance data as the header row and not as instance data, which is definitely not what you want.

Before the code above, you need to read the file in, write a header row, and save the file again.

See my answer to your question on the weka mailing list.

michaeltwofish
Thanks. I'll try that. I assumed my question to the mailing list got lost in the shuffle :-(
Greg
No worries, asking through different avenues is a good idea :)
michaeltwofish