Skip to content

Instantly share code, notes, and snippets.

@happyrobots
Created February 13, 2011 21:47
Show Gist options
  • Save happyrobots/825173 to your computer and use it in GitHub Desktop.
Save happyrobots/825173 to your computer and use it in GitHub Desktop.
This is just one example of creating an instance in-memory. I think this way is readable and provides enough flexibility on attribute types (numeric, nominal, string).
Instances dataSet = // ...
Attribute stringAttribute = // ...
Attribute nominalAttribute = // ...
int numberOfAttributes = 3;
Instance instance = new SparseInstance(numberOfAttributes);
instance.setDataset(dataSet);
instance.setValue(numericAttribute, 8.8);
/*
* The way Weka stores data is a bit low-level. For any
* CRUD operations, we have to care about the index number
* of nominal and string values.
* As a comparison, CRUD operations in SQL or Search
* Engine Database are already abstracted.
*/
instance.setValue(nominalAttribute, dataSet.attribute(
"gender").indexOfValue("male"));
String userIdAsString = "8379";
stringAttribute.addStringValue(userIdAsString);
instance.setValue(stringAttribute, userIdAsString);
dataSet.add(instance);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment