Search in sources :

Example 11 with Instance

use of weka.core.Instance in project dkpro-tc by dkpro.

the class WekaOutcomeIDReport method generateSlProperties.

protected Properties generateSlProperties(Instances predictions, boolean isRegression, boolean isUnit, Map<Integer, String> documentIdMap, List<String> labels) throws Exception {
    Properties props = new SortedKeyProperties();
    String[] classValues = new String[predictions.numClasses()];
    for (int i = 0; i < predictions.numClasses(); i++) {
        classValues[i] = predictions.classAttribute().value(i);
    }
    int attOffset = predictions.attribute(ID_FEATURE_NAME).index();
    prepareBaseline();
    int idx = 0;
    for (Instance inst : predictions) {
        Double gold;
        try {
            gold = new Double(inst.value(predictions.attribute(CLASS_ATTRIBUTE_NAME + WekaUtils.COMPATIBLE_OUTCOME_CLASS)));
        } catch (NullPointerException e) {
            // if train and test data have not been balanced
            gold = new Double(inst.value(predictions.attribute(CLASS_ATTRIBUTE_NAME)));
        }
        Attribute gsAtt = predictions.attribute(WekaTestTask.PREDICTION_CLASS_LABEL_NAME);
        Double prediction = new Double(inst.value(gsAtt));
        if (!isRegression) {
            Map<String, Integer> class2number = classNamesToMapping(labels);
            // Integer predictionAsNumber = class2number
            // .get(gsAtt.value(prediction.intValue()));
            Integer goldAsNumber = class2number.get(classValues[gold.intValue()]);
            String stringValue = inst.stringValue(attOffset);
            if (!isUnit && documentIdMap != null) {
                stringValue = documentIdMap.get(idx++);
            }
            props.setProperty(stringValue, getPrediction(prediction, class2number, gsAtt) + SEPARATOR_CHAR + goldAsNumber + SEPARATOR_CHAR + String.valueOf(-1));
        } else {
            // the outcome is numeric
            String stringValue = inst.stringValue(attOffset);
            if (documentIdMap != null) {
                stringValue = documentIdMap.get(idx++);
            }
            props.setProperty(stringValue, prediction + SEPARATOR_CHAR + gold + SEPARATOR_CHAR + String.valueOf(0));
        }
    }
    return props;
}
Also used : SortedKeyProperties(org.dkpro.tc.ml.report.util.SortedKeyProperties) Instance(weka.core.Instance) Attribute(weka.core.Attribute) Properties(java.util.Properties) SortedKeyProperties(org.dkpro.tc.ml.report.util.SortedKeyProperties)

Example 12 with Instance

use of weka.core.Instance in project iobserve-analysis by research-iobserve.

the class TVectorQuantizationClustering method printInstances.

private void printInstances(final ClusteringResults results) {
    results.printClusteringResults();
    final Instances centroids = results.getClusteringMetrics().getCentroids();
    for (int i = 0; i < centroids.numInstances(); i++) {
        String logString = "";
        logString += "***************************";
        logString += "Cluster " + i;
        logString += "***************************";
        final Instance instance = centroids.instance(i);
        for (int a = 0; a < instance.numAttributes(); a++) {
            logString += centroids.attribute(a).name() + " : " + instance.value(a);
        }
        TVectorQuantizationClustering.LOGGER.info(logString);
    }
}
Also used : Instances(weka.core.Instances) Instance(weka.core.Instance)

Example 13 with Instance

use of weka.core.Instance in project iobserve-analysis by research-iobserve.

the class ClusterMerger method execute.

/*
     * (non-Javadoc)
     *
     * @see teetime.framework.AbstractConsumerStage#execute(java.lang.Object)
     */
@Override
protected void execute(final Map<Integer, List<Pair<Instance, Double>>> clustering) throws Exception {
    /**
     * simply pick the first instance of every cluster lookup attributes to build a new
     * instances Object
     */
    Instance instance = clustering.entrySet().iterator().next().getValue().get(0).getElement1();
    final FastVector attributes = new FastVector();
    for (int j = 0; j < instance.numAttributes(); j++) {
        attributes.addElement(instance.attribute(j));
    }
    final Instances result = new Instances("Clustering Result", attributes, clustering.size());
    for (final List<Pair<Instance, Double>> entry : clustering.values()) {
        if (!entry.isEmpty()) {
            instance = entry.get(0).getElement1();
            result.add(instance);
        }
    }
    this.printInstances(result);
    this.outputPort.send(result);
}
Also used : Instances(weka.core.Instances) FastVector(weka.core.FastVector) Instance(weka.core.Instance) Pair(org.eclipse.net4j.util.collection.Pair)

Example 14 with Instance

use of weka.core.Instance in project iobserve-analysis by research-iobserve.

the class BehaviorModelTable method toInstance.

/**
 * returns an instance vector.
 *
 * @return instance
 */
public Instance toInstance() {
    final List<Double> attValues = new ArrayList<>();
    // add transitions
    for (int i = 0; i < this.signatures.size(); i++) {
        for (int j = 0; j < this.signatures.size(); j++) {
            if (this.transitions[i][j] > AbstractBehaviorModelTable.TRANSITION_THRESHOLD) {
                attValues.add(Double.valueOf(this.transitions[i][j]));
            } else {
                continue;
            }
        }
    }
    this.signatures.values().stream().forEach(pair -> Arrays.stream(pair.getSecond()).forEach(callInformation -> attValues.add(callInformation.getRepresentativeCode())));
    final double[] attArray = new double[attValues.size()];
    for (int i = 0; i < attValues.size(); i++) {
        attArray[i] = attValues.get(i) == null ? 0.0 : attValues.get(i);
    }
    final Instance instance = new Instance(1.0, attArray);
    return instance;
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) FastVector(weka.core.FastVector) Pair(org.apache.commons.math3.util.Pair) SingleOrNoneCollector(org.iobserve.analysis.clustering.SingleOrNoneCollector) Instances(weka.core.Instances) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PayloadAwareEntryCallEvent(org.iobserve.stages.general.data.PayloadAwareEntryCallEvent) EntryCallEvent(org.iobserve.stages.general.data.EntryCallEvent) Instance(weka.core.Instance) List(java.util.List) Map(java.util.Map) Optional(java.util.Optional) Attribute(weka.core.Attribute) Instance(weka.core.Instance) ArrayList(java.util.ArrayList)

Aggregations

Instance (weka.core.Instance)14 Instances (weka.core.Instances)6 Attribute (weka.core.Attribute)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 DenseInstance (weka.core.DenseInstance)4 FastVector (weka.core.FastVector)4 List (java.util.List)3 Arrays (java.util.Arrays)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Pair (org.apache.commons.math3.util.Pair)2 Pair (org.eclipse.net4j.util.collection.Pair)2 SingleOrNoneCollector (org.iobserve.analysis.clustering.SingleOrNoneCollector)2 EntryCallEvent (org.iobserve.stages.general.data.EntryCallEvent)2 PayloadAwareEntryCallEvent (org.iobserve.stages.general.data.PayloadAwareEntryCallEvent)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Font (java.awt.Font)1 GridBagConstraints (java.awt.GridBagConstraints)1