Store data to file

Java-ML can store Datasets back to file using the FileHandler class. At the moment it is not possible to store data in the ARFF format.

In the previous article in the tutorial trail we have shown how to load data from a file, we will use this mechanism again to load our data and then store it to a different file.

Storing data to a file
[Documented source code]

  1. /* Load the iris data set from file */
  2. Dataset data = FileHandler.loadDataset(new File(""), 4, ",");
  3. System.out.println(data);
  4. /* Store the data back to another file */
  5. FileHandler.exportDataset(data,new File("output.txt"));

The above example will load the iris data set and will store it back to another file (output.txt). Each instance is written to the file separately with the class label on position 0. The fields are delimited with a tab character.

The data that was outputted can be read again with the following code:

  1. Dataset data = FileHandler.loadDataset(new File("output.txt"), 0,"\t");

Caveats: Note that data sets with mixed sparse and dense instances may result in files that cannot be loaded with the methods in FileHandler. Exporting data sets where some Instances have their class label set and some don't, will result in unwanted behaviour when loading the file with the methods in FileHandler.