Skip to content

Instantly share code, notes, and snippets.

@andrewm4894
Created April 30, 2019 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewm4894/9e78efeb96921e5272ae2d1d2d603fef to your computer and use it in GitHub Desktop.
Save andrewm4894/9e78efeb96921e5272ae2d1d2d603fef to your computer and use it in GitHub Desktop.
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Add;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class wekaDev {
public static void main(String[] args) throws Exception {
// read in data
DataSource source = new DataSource("C:\\Program Files\\Weka-3-8\\data\\iris.arff");
Instances data = source.getDataSet();
// look at data before
System.out.println("\n====== BEFORE ======\n");
printHead(data,5);
// make a list of random doubles
List<Double> list = new ArrayList<>(data.numInstances());
Random rand = new Random();
for (int i=0;i<=data.numInstances();i++) {
list.add(rand.nextDouble());
}
// get list of numbers into Instances object as new attribute
Add filter = new Add();
filter.setAttributeIndex("last"); // add as last so we know where it is
filter.setAttributeName("randomDouble"); // give it a name
filter.setInputFormat(data); // set the filter
data = Filter.useFilter(data, filter); // use the filter
// now update all the values
for (int i = 0; i < data.numInstances(); i++) {
data.instance(i).setValue(data.numAttributes()-1, list.get(i)); // need to -1 as zero based index
}
// look at data after
System.out.println("\n====== AFTER ======\n");
printHead(data,5);
}
public static void printHead(Instances data, int n) {
int numCols = data.numAttributes();
for (int i=0; i<=(numCols-1); i++){
if (i < (numCols-1)) {
System.out.print(data.attribute(i).name() + ",");
}
else {
System.out.print(data.attribute(i).name() + "\n");
}
}
for (int i=0; i<=(n-1); i++){
System.out.println(data.instance(i));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment