Created
February 13, 2011 21:47
-
-
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).
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
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