use of weka.core.Attribute in project dkpro-tc by dkpro.
the class WekaUtils method createOutcomeAttributes.
/**
* @param outcomeValues
* the outcome values
* @return list of weka attributes
*/
private static List<Attribute> createOutcomeAttributes(List<String> outcomeValues) {
// make the order of the attributes predictable
Collections.sort(outcomeValues);
List<Attribute> atts = new ArrayList<Attribute>();
for (String outcome : outcomeValues) {
String name = outcome.contains(CLASS_ATTRIBUTE_PREFIX) ? outcome : CLASS_ATTRIBUTE_PREFIX + outcome;
atts.add(new Attribute(name, Arrays.asList(new String[] { "0", "1" })));
}
return atts;
}
use of weka.core.Attribute in project dkpro-tc by dkpro.
the class MekaDataWriter method createOutcomeAttributes.
private static List<Attribute> createOutcomeAttributes(List<String> outcomeValues) {
// make the order of the attributes predictable
Collections.sort(outcomeValues);
List<Attribute> atts = new ArrayList<Attribute>();
for (String outcome : outcomeValues) {
String name = outcome.contains(CLASS_ATTRIBUTE_PREFIX) ? outcome : CLASS_ATTRIBUTE_PREFIX + outcome;
atts.add(new Attribute(name, Arrays.asList(new String[] { "0", "1" })));
}
return atts;
}
use of weka.core.Attribute in project dkpro-tc by dkpro.
the class WekaDataWriter method getFeatureValues.
private double[] getFeatureValues(AttributeStore attributeStore, Instance instance) {
double[] featureValues = new double[attributeStore.getAttributes().size()];
for (Feature feature : instance.getFeatures()) {
try {
Attribute attribute = attributeStore.getAttribute(feature.getName());
Object featureValue = feature.getValue();
double attributeValue;
if (feature.getType() == FeatureType.NUMERIC) {
// numeric attribute
attributeValue = ((Number) feature.getValue()).doubleValue();
} else if (feature.getType() == FeatureType.BOOLEAN) {
// boolean attribute
if (featureValue instanceof Boolean) {
// value is provided as true/false value
attributeValue = (Boolean) featureValue ? 1.0d : 0.0d;
} else {
// we already have numerical values
if (featureValue instanceof Double) {
attributeValue = (Double) featureValue;
} else {
attributeValue = ((Integer) featureValue).doubleValue();
}
}
} else {
// nominal or string
Object stringValue = feature.getValue();
if (!attribute.isNominal() && !attribute.isString()) {
throw new IllegalArgumentException("Attribute neither nominal nor string: " + stringValue);
}
int valIndex = attribute.indexOfValue(stringValue.toString());
if (valIndex == -1) {
if (attribute.isNominal()) {
throw new IllegalArgumentException("Value not defined for given nominal attribute!");
} else {
attribute.addStringValue(stringValue.toString());
valIndex = attribute.indexOfValue(stringValue.toString());
}
}
attributeValue = valIndex;
}
int offset = attributeStore.getAttributeOffset(attribute.name());
if (offset != -1) {
featureValues[offset] = attributeValue;
}
} catch (NullPointerException e) {
// ignore unseen attributes
}
}
return featureValues;
}
use of weka.core.Attribute in project dkpro-tc by dkpro.
the class WekaUtilTest method tcInstanceToWekaInstanceTest.
@Test
public void tcInstanceToWekaInstanceTest() throws Exception {
List<String> outcomeValues = Arrays.asList(new String[] { "outc_1", "outc_2", "outc_3" });
Instance i1 = new Instance();
i1.addFeature(new Feature("feature1", 2, FeatureType.NUMERIC));
i1.addFeature(new Feature("feature2", 2, FeatureType.NUMERIC));
i1.addFeature(new Feature("feature3_{{", "a", FeatureType.STRING));
Instance i2 = new Instance();
i2.addFeature(new Feature("feature1", 1, FeatureType.NUMERIC));
i2.addFeature(new Feature("feature4", "val_1", FeatureType.STRING));
i2.addFeature(new Feature("feature3_{{", "b", FeatureType.STRING));
ArrayList<Attribute> attributes = new ArrayList<Attribute>();
attributes.add(new Attribute("feature5"));
attributes.add(new Attribute("feature2"));
attributes.add(new Attribute("feature4", Arrays.asList(new String[] { "val_1", "val_2" })));
attributes.add(new Attribute("feature1"));
attributes.add(new Attribute("outcome", outcomeValues));
Instances trainingData = new Instances("test", attributes, 0);
weka.core.Instance wekaInstance1 = WekaUtils.tcInstanceToWekaInstance(i1, trainingData, outcomeValues, false);
weka.core.Instance wekaInstance2 = WekaUtils.tcInstanceToWekaInstance(i2, trainingData, outcomeValues, false);
assertEquals(true, wekaInstance1.equalHeaders(wekaInstance2));
assertEquals(5, wekaInstance1.numAttributes());
wekaInstance1.dataset().add(wekaInstance1);
wekaInstance2.dataset().add(wekaInstance2);
System.out.println(wekaInstance1.dataset() + "\n");
System.out.println(wekaInstance2.dataset() + "\n");
}
use of weka.core.Attribute in project dkpro-tc by dkpro.
the class WekaUtilTest method tcInstanceToWekaInstanceFailTest.
@Test(expected = IllegalArgumentException.class)
public void tcInstanceToWekaInstanceFailTest() throws Exception {
List<String> outcomeValues = Arrays.asList(new String[] { "outc_1", "outc_2", "outc_3" });
Instance i1 = new Instance();
i1.addFeature(new Feature("feature1", 2, FeatureType.NUMERIC));
i1.addFeature(new Feature("feature4", "val_1", FeatureType.STRING));
i1.addFeature(new Feature("feature3_{{", "a", FeatureType.STRING));
ArrayList<Attribute> attributes = new ArrayList<Attribute>();
attributes.add(new Attribute("feature2"));
attributes.add(new Attribute("feature4", Arrays.asList(new String[] { "val_4", "val_2" })));
attributes.add(new Attribute("outcome", outcomeValues));
Instances trainingData = new Instances("test", attributes, 0);
@SuppressWarnings("unused") weka.core.Instance wekaInstance1 = WekaUtils.tcInstanceToWekaInstance(i1, trainingData, outcomeValues, false);
}
Aggregations