Search in sources :

Example 6 with Attribute

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;
}
Also used : Attribute(weka.core.Attribute) ArrayList(java.util.ArrayList)

Example 7 with Attribute

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;
}
Also used : Attribute(weka.core.Attribute) ArrayList(java.util.ArrayList)

Example 8 with Attribute

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;
}
Also used : Attribute(weka.core.Attribute) Feature(org.dkpro.tc.api.features.Feature)

Example 9 with Attribute

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");
}
Also used : Instances(weka.core.Instances) Instance(org.dkpro.tc.api.features.Instance) Attribute(weka.core.Attribute) ArrayList(java.util.ArrayList) Feature(org.dkpro.tc.api.features.Feature) Test(org.junit.Test)

Example 10 with Attribute

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);
}
Also used : Instances(weka.core.Instances) Instance(org.dkpro.tc.api.features.Instance) Attribute(weka.core.Attribute) ArrayList(java.util.ArrayList) Feature(org.dkpro.tc.api.features.Feature) Test(org.junit.Test)

Aggregations

Attribute (weka.core.Attribute)28 ArrayList (java.util.ArrayList)12 Instances (weka.core.Instances)12 Feature (org.dkpro.tc.api.features.Feature)8 Instance (org.dkpro.tc.api.features.Instance)8 SparseInstance (weka.core.SparseInstance)5 Test (org.junit.Test)4 DenseInstance (weka.core.DenseInstance)4 Instance (weka.core.Instance)4 ArffSaver (weka.core.converters.ArffSaver)4 File (java.io.File)3 MultiLabelInstances (mulan.data.MultiLabelInstances)3 TextClassificationException (org.dkpro.tc.api.exception.TextClassificationException)3 AttributeStore (org.dkpro.tc.ml.weka.util.AttributeStore)3 FastVector (weka.core.FastVector)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Optional (java.util.Optional)2 FeatureType (org.dkpro.tc.api.features.FeatureType)2 Request (io.milton.http.Request)1