Example usage for weka.core Instance setValue

List of usage examples for weka.core Instance setValue

Introduction

In this page you can find the example usage for weka.core Instance setValue.

Prototype

public void setValue(Attribute att, String value);

Source Link

Document

Sets a value of an nominal or string attribute to the given value.

Usage

From source file:tr.gov.ulakbim.jDenetX.streams.generators.multilabel.MetaMultilabelGenerator.java

License:Open Source License

/**
 * GenerateMLInstance./*from w  w w .j a v a 2  s . c o  m*/
 */
private Instance generateMLInstance(ArrayList<Integer> lbls) throws Exception {

    // create a multi-label instance   :
    Instance ml_x = new SparseInstance(this.multilabelStreamTemplate.numAttributes());
    ml_x.setDataset(this.multilabelStreamTemplate);

    // set classes
    for (int i = 0; i < m_N; i++)
        ml_x.setValue(i, 0.0);
    for (int i = 0; i < lbls.size(); i++) {
        ml_x.setValue(lbls.get(i), 1.0);
    }

    // generate binary instances
    Instance binary0 = getNextWithBinary(0);
    Instance binary1 = getNextWithBinary(1);

    // Loop through each feature attribute @warning: assumes class is last index
    for (int a = 0; a < m_A; a++) {

        // The combination is present: use a positive value
        if (lbls.containsAll(m_FeatureEffects[a % m_FeatureEffects.length])) {
            ml_x.setValue(m_N + a, binary1.value(a));
        }
        // The combination is absent: use a negative value
        else {
            ml_x.setValue(m_N + a, binary0.value(a));
        }
    }

    return ml_x;

}

From source file:tr.gov.ulakbim.jDenetX.streams.generators.RandomTreeGenerator.java

License:Open Source License

public Instance nextInstance() {
    double[] attVals = new double[this.numNominalsOption.getValue() + this.numNumericsOption.getValue()];
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    for (int i = 0; i < attVals.length; i++) {
        attVals[i] = i < this.numNominalsOption.getValue()
                ? this.instanceRandom.nextInt(this.numValsPerNominalOption.getValue())
                : this.instanceRandom.nextDouble();
        inst.setValue(i, attVals[i]);
    }//from  ww w . j av a2  s. co  m
    inst.setDataset(header);
    inst.setClassValue(classifyInstance(this.treeRoot, attVals));
    return inst;
}

From source file:tr.gov.ulakbim.jDenetX.streams.generators.SEAGenerator.java

License:Open Source License

public Instance nextInstance() {
    double attrib1 = 0, attrib2 = 0, attrib3 = 0;
    int group = 0;
    boolean desiredClassFound = false;
    while (!desiredClassFound) {
        // generate attributes
        attrib1 = 10 * this.instanceRandom.nextDouble();
        attrib2 = 10 * this.instanceRandom.nextDouble();
        attrib3 = 10 * this.instanceRandom.nextDouble();

        // determine class
        group = classificationFunctions[this.functionOption.getValue() - 1].determineClass(attrib1, attrib2,
                attrib3);//from w  w w .  j  a  v a  2 s.  c  om
        if (!this.balanceClassesOption.isSet()) {
            desiredClassFound = true;
        } else {
            // balance the classes
            if ((this.nextClassShouldBeZero && (group == 0)) || (!this.nextClassShouldBeZero && (group == 1))) {
                desiredClassFound = true;
                this.nextClassShouldBeZero = !this.nextClassShouldBeZero;
            } // else keep searching
        }
    }
    //Add Noise
    if ((1 + (this.instanceRandom.nextInt(100))) <= this.noisePercentageOption.getValue()) {
        group = (group == 0 ? 1 : 0);
    }

    // construct instance
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setValue(0, attrib1);
    inst.setValue(1, attrib2);
    inst.setValue(2, attrib3);
    inst.setDataset(header);
    inst.setClassValue(group);
    return inst;
}

From source file:tr.gov.ulakbim.jDenetX.streams.generators.STAGGERGenerator.java

License:Open Source License

public Instance nextInstance() {

    int size = 0, color = 0, shape = 0, group = 0;
    boolean desiredClassFound = false;
    while (!desiredClassFound) {
        // generate attributes
        size = this.instanceRandom.nextInt(3);
        color = this.instanceRandom.nextInt(3);
        shape = this.instanceRandom.nextInt(3);

        // determine class
        group = classificationFunctions[this.functionOption.getValue() - 1].determineClass(size, color, shape);
        if (!this.balanceClassesOption.isSet()) {
            desiredClassFound = true;/*from w  w w.  jav  a2 s  . c  o  m*/
        } else {
            // balance the classes
            if ((this.nextClassShouldBeZero && (group == 0)) || (!this.nextClassShouldBeZero && (group == 1))) {
                desiredClassFound = true;
                this.nextClassShouldBeZero = !this.nextClassShouldBeZero;
            } // else keep searching
        }
    }

    // construct instance
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setValue(0, size);
    inst.setValue(1, color);
    inst.setValue(2, shape);
    inst.setDataset(header);
    inst.setClassValue(group);
    return inst;
}

From source file:tr.gov.ulakbim.jDenetX.streams.generators.WaveformGenerator.java

License:Open Source License

public Instance nextInstance() {
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setDataset(header);/*from w ww. j av a 2  s  .co m*/
    int waveform = this.instanceRandom.nextInt(NUM_CLASSES);
    int choiceA = 0, choiceB = 0;
    switch (waveform) {
    case 0:
        choiceA = 0;
        choiceB = 1;
        break;
    case 1:
        choiceA = 0;
        choiceB = 2;
        break;
    case 2:
        choiceA = 1;
        choiceB = 2;
        break;
    default:
        break;
    }
    double multiplierA = this.instanceRandom.nextDouble();
    double multiplierB = 1.0 - multiplierA;
    for (int i = 0; i < NUM_BASE_ATTRIBUTES; i++) {
        inst.setValue(i, (multiplierA * hFunctions[choiceA][i]) + (multiplierB * hFunctions[choiceB][i])
                + this.instanceRandom.nextGaussian());
    }
    if (this.addNoiseOption.isSet()) {
        for (int i = NUM_BASE_ATTRIBUTES; i < TOTAL_ATTRIBUTES_INCLUDING_NOISE; i++) {
            inst.setValue(i, this.instanceRandom.nextGaussian());
        }
    }
    inst.setClassValue(waveform);
    return inst;
}

From source file:transformation.mimlTOml.ArithmeticTransformation.java

License:Open Source License

@Override
public MultiLabelInstances transformDataset() throws Exception {
    Instances newData = new Instances(template);
    int labelIndices[] = dataset.getLabelIndices();
    Instance newInst = new DenseInstance(newData.numAttributes());
    newInst.setDataset(newData); // Sets the reference to the dataset

    // For all bags in the dataset
    double nBags = dataset.getNumBags();
    for (int i = 0; i < nBags; i++) {
        // retrieves a bag
        Bag bag = dataset.getBag(i);/*from   w ww.j  a  v  a  2  s . co m*/
        // sets the bagLabel
        newInst.setValue(0, bag.value(0));

        // retrieves instances (relational value) for each bag
        Instances instances = bag.getBagAsInstances();
        // for all attributes in bag
        for (int j = 0, attIdx = 1; j < instances.numAttributes(); j++, attIdx++) {
            double value = instances.meanOrMode(j);
            newInst.setValue(attIdx, value);
        }

        // inserts label information into the instance
        for (int j = 0; j < labelIndices.length; j++) {
            newInst.setValue(updatedLabelIndices[j], dataset.getBag(i).value(labelIndices[j]));
        }

        newData.add(newInst);
    }

    return new MultiLabelInstances(newData, dataset.getLabelsMetaData());
}

From source file:transformation.mimlTOml.ArithmeticTransformation.java

License:Open Source License

@Override
public Instance transformInstance(Bag bag) throws Exception {

    int labelIndices[] = dataset.getLabelIndices();
    Instance newInst = new DenseInstance(template.numAttributes());

    // sets the bagLabel
    newInst.setDataset(bag.dataset()); // Sets the reference to the dataset
    newInst.setValue(0, bag.value(0));

    // retrieves instances (relational value)
    Instances instances = bag.getBagAsInstances();
    // For all attributes in bag
    for (int j = 0, attIdx = 1; j < instances.numAttributes(); j++, attIdx++) {
        double value = instances.meanOrMode(j);
        newInst.setValue(attIdx, value);
    }/* www .  j a v  a 2s. com*/

    // Insert label information into the instance
    for (int j = 0; j < labelIndices.length; j++) {
        newInst.setValue(updatedLabelIndices[j], bag.value(labelIndices[j]));
    }

    return newInst;
}

From source file:transformation.mimlTOml.GeometricTransformation.java

License:Open Source License

@Override
public MultiLabelInstances transformDataset() throws Exception {

    Instances newData = new Instances(template);
    int labelIndices[] = dataset.getLabelIndices();
    Instance newInst = new DenseInstance(newData.numAttributes());
    newInst.setDataset(newData); // Sets the reference to the dataset

    // For all bags in the dataset
    double nBags = dataset.getNumBags();
    for (int i = 0; i < nBags; i++) {
        // retrieves a bag
        Bag bag = dataset.getBag(i);/* w  w w. ja v a2s.  c  o  m*/
        // sets the bagLabel
        newInst.setValue(0, bag.value(0));

        // retrieves instances (relational value) for each bag
        Instances instances = bag.getBagAsInstances();
        // for all attributes in bag
        for (int j = 0, attIdx = 1; j < instances.numAttributes(); j++, attIdx++) {
            double[] minimax = minimax(instances, j);
            double value = (minimax[0] + minimax[1]) / 2.0;
            newInst.setValue(attIdx, value);
        }

        // inserts label information into the instance
        for (int j = 0; j < labelIndices.length; j++) {
            newInst.setValue(updatedLabelIndices[j], dataset.getBag(i).value(labelIndices[j]));
        }

        newData.add(newInst);
    }
    return new MultiLabelInstances(newData, dataset.getLabelsMetaData());
}

From source file:transformation.mimlTOml.GeometricTransformation.java

License:Open Source License

@Override
public Instance transformInstance(Bag bag) throws Exception {
    int labelIndices[] = dataset.getLabelIndices();
    Instance newInst = new DenseInstance(template.numAttributes());

    // sets the bagLabel
    newInst.setDataset(bag.dataset()); // Sets the reference to the dataset
    newInst.setValue(0, bag.value(0));

    // retrieves instances (relational value)
    Instances instances = bag.getBagAsInstances();
    // For all attributes in bag
    for (int j = 0, attIdx = 1; j < instances.numAttributes(); j++, attIdx++) {
        double[] minimax = minimax(instances, j);
        double value = (minimax[0] + minimax[1]) / 2.0;
        newInst.setValue(attIdx, value);
    }/*from w ww. jav  a2  s .  c  o m*/

    // Insert label information into the instance
    for (int j = 0; j < labelIndices.length; j++) {
        newInst.setValue(updatedLabelIndices[j], bag.value(labelIndices[j]));
    }

    return newInst;
}

From source file:transformation.mimlTOml.MiniMaxTransformation.java

License:Open Source License

@Override
public MultiLabelInstances transformDataset() throws Exception {

    Instances newData = new Instances(template);
    int labelIndices[] = dataset.getLabelIndices();
    Instance newInst = new DenseInstance(newData.numAttributes());
    newInst.setDataset(newData); // Sets the reference to the dataset

    // For all bags in the dataset
    double nBags = dataset.getNumBags();
    for (int i = 0; i < nBags; i++) {
        // retrieves a bag
        Bag bag = dataset.getBag(i);/*  w w  w. j a  va  2  s .c  o  m*/
        // sets the bagLabel
        newInst.setValue(0, bag.value(0));

        // retrieves instances (relational value) for each bag
        Instances instances = bag.getBagAsInstances();
        // For all attributes in bag
        for (int j = 0, attIdx = 1; j < instances.numAttributes(); j++, attIdx++) {
            double[] minimax = minimax(instances, j);
            newInst.setValue(attIdx, minimax[0]);// minima value
            newInst.setValue(attIdx + instances.numAttributes(), minimax[1]);// maxima
            // value);
        }
        // Copy label information into the dataset
        for (int j = 0; j < labelIndices.length; j++) {
            newInst.setValue(updatedLabelIndices[j], bag.value(labelIndices[j]));
        }
        newData.add(newInst);

    }
    return new MultiLabelInstances(newData, dataset.getLabelsMetaData());
}