This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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