Example usage for weka.core SparseInstance setDataset

List of usage examples for weka.core SparseInstance setDataset

Introduction

In this page you can find the example usage for weka.core SparseInstance setDataset.

Prototype

@Override
public final void setDataset(Instances instances) 

Source Link

Document

Sets the reference to the dataset.

Usage

From source file:cluster.ABC.ClusterUtils.java

License:Open Source License

/** Finds sum of 2 instances (handles sparse and non-sparse)
 *//*  w ww. jav  a2  s  . c  om*/

public static Instance sumInstances(Instance inst1, Instance inst2, Instances m_Instances) throws Exception {
    int numAttributes = inst1.numAttributes();
    if (inst2.numAttributes() != numAttributes) {
        throw new Exception("Error!! inst1 and inst2 should have same number of attributes.");
    }
    double weight1 = inst1.weight(), weight2 = inst2.weight();
    double[] values = new double[numAttributes];

    for (int i = 0; i < numAttributes; i++) {
        values[i] = 0;
    }

    if (inst1 instanceof SparseInstance && inst2 instanceof SparseInstance) {
        for (int i = 0; i < inst1.numValues(); i++) {
            int indexOfIndex = inst1.index(i);
            values[indexOfIndex] = inst1.valueSparse(i);
        }
        for (int i = 0; i < inst2.numValues(); i++) {
            int indexOfIndex = inst2.index(i);
            values[indexOfIndex] += inst2.valueSparse(i);
        }
        SparseInstance newInst = new SparseInstance(weight1 + weight2, values);
        newInst.setDataset(m_Instances);
        return newInst;
    } else if (!(inst1 instanceof SparseInstance) && !(inst2 instanceof SparseInstance)) {
        for (int i = 0; i < numAttributes; i++) {
            values[i] = inst1.value(i) + inst2.value(i);
        }
    } else {
        throw new Exception("Error!! inst1 and inst2 should be both of same type -- sparse or non-sparse");
    }
    Instance newInst = new Instance(weight1 + weight2, values);
    newInst.setDataset(m_Instances);
    return newInst;
}

From source file:edu.cmu.lti.oaqa.baseqa.providers.ml.classifiers.WekaProvider.java

License:Apache License

private static Instance newInstance(Map<String, Double> features, String label, double weight,
        Instances dataset) {/*from  www. j av a 2  s  . c om*/
    double[] values = new double[dataset.numAttributes()];
    for (Map.Entry<String, Double> entry : features.entrySet()) {
        Attribute attribute = dataset.attribute(entry.getKey());
        if (attribute == null)
            continue;
        values[attribute.index()] = entry.getValue();
    }
    SparseInstance instance = new SparseInstance(weight, values);
    instance.setDataset(dataset);
    if (label != null)
        instance.setClassValue(label);
    return instance;
}

From source file:gate.plugin.learningframework.data.CorpusRepresentationWeka.java

public static weka.core.Instance wekaInstanceFromMalletInstance(Instances wekaDataset,
        cc.mallet.types.Instance malletInstance) {
    FeatureVector fv = (FeatureVector) malletInstance.getData();
    int size = fv.numLocations();
    int wekaTargetIndex = wekaDataset.classIndex();
    // TODO: for now we just directly copy over the mallet values to the weka values
    // We may need to handle certain cases with missing values separately!

    // create  the arrays with one more entry which will be the target, if we have a target

    //int indices[] = haveTarget ? new int[size + 1] : new int[size];
    // experimental change: always allocate the space for the class attribute! 
    // We do this because Weka Random Forest threw an exception and complained about a missing
    // class. // w  ww .  ja va  2 s. com
    int indices[] = new int[size + 1];
    double values[] = new double[size + 1];
    for (int i = 0; i < size; i++) {
        indices[i] = fv.indexAtLocation(i);
        values[i] = fv.valueAtLocation(i);
    }
    // now set the target, if we have one 
    Object malletValue = malletInstance.getTarget();
    if (malletValue != null) { // we do have a target value, could be a class label or a numeric value
        indices[size] = wekaTargetIndex;
        // if we have a target alphabet, convert the label to a class index, otherwise expect
        // a double value directly
        if (malletInstance.getTargetAlphabet() == null) {
            values[size] = (double) malletInstance.getTarget();
        } else {
            LabelAlphabet la = (LabelAlphabet) malletInstance.getTargetAlphabet();
            Label malletLabel = (Label) malletInstance.getTarget();
            int targetIndex = malletLabel.getIndex();
            String targetString = malletLabel.toString();
            int wekaIndex = wekaDataset.classAttribute().indexOfValue(targetString);
            values[size] = (double) wekaIndex;
            if (targetIndex != wekaIndex) {
                System.err.println("DEBUG ASSERTION FAILED: malletIndex for target is not equal to wekaIndex");
            }
        }
    } else { // we do not have a target value, so lets create a missing value target for weka
        indices[size] = wekaDataset.classIndex();
        values[size] = Double.NaN;
    }
    weka.core.SparseInstance wekaInstance = new weka.core.SparseInstance(1.0, values, indices, values.length);
    // TODO: is this necessary, is this useful?
    // What does this actually do? Hopefully not actually add or modify anything in the wekaDataset
    // and just give the instance a chance to know about the attributes?
    wekaInstance.setDataset(wekaDataset);
    return wekaInstance;
}