Skip to content

Instantly share code, notes, and snippets.

@ohmrefresh
Last active August 29, 2015 14:21
Show Gist options
  • Save ohmrefresh/e19806231d93608b8c42 to your computer and use it in GitHub Desktop.
Save ohmrefresh/e19806231d93608b8c42 to your computer and use it in GitHub Desktop.
Create ARFF File
import java.io.File;
import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.filters.Filter;
import weka.filters.unsupervised.instance.NonSparseToSparse;
public class AttTest {
public static void main(String[] args) throws Exception {
FastVector atts;
Instances data;
double[] vals;
FastVector attZone = new FastVector();
attZone.addElement("safe_1");
attZone.addElement("safe_2");
attZone.addElement("danger_1");
atts = new FastVector();
atts.addElement(new Attribute("ref1"));
atts.addElement(new Attribute("ref2"));
atts.addElement(new Attribute("ref3"));
atts.addElement(new Attribute("ref4"));
atts.addElement(new Attribute("tag"));
atts.addElement(new Attribute("zone", attZone));
// 2. create Instances object
data = new Instances("MyRelation", atts, 0);
// 3. fill with data
// first instance
vals = new double[data.numAttributes()];
vals[0] = Math.PI;
vals[1] = 2;
vals[2] = 33;
vals[3] = 555;
vals[4] = 11;
vals[5] =attZone.indexOf("safe_2");
data.add(new Instance(1.0, vals));
NonSparseToSparse nonSparseToSparseInstance = new NonSparseToSparse();
nonSparseToSparseInstance.setInputFormat(data);
Instances sparseDataset = Filter.useFilter(data, nonSparseToSparseInstance);
System.out.println(sparseDataset);
ArffSaver arffSaverInstance = new ArffSaver();
arffSaverInstance.setInstances(sparseDataset);
arffSaverInstance.setFile(new File("/sdcard/ESDN.arff"));
arffSaverInstance.writeBatch();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment