Example usage for weka.core Instance setDataset

List of usage examples for weka.core Instance setDataset

Introduction

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

Prototype

public void setDataset(Instances instances);

Source Link

Document

Sets the reference to the dataset.

Usage

From source file:moa.streams.generators.cd.AbstractConceptDriftGenerator.java

License:Open Source License

public Instance nextInstance() {
    this.numInstances++;
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setDataset(header);
    double nextValue = this.nextValue();
    if (this.notBinaryStreamOption.isSet()) {
        inst.setValue(0, nextValue);/* w ww.  j  a va 2s  .  c  o  m*/
    } else {
        inst.setValue(0, this.nextbinaryValue(nextValue));
    }
    //Ground truth
    inst.setValue(1, this.getChange() ? 1 : 0);
    if (this.getChange() == true) {
        //this.clusterEvents.add(new ClusterEvent(this, this.numInstances, "Change", "Drift"));
    }
    inst.setValue(2, nextValue);
    return inst;
}

From source file:moa.streams.generators.HyperplaneGenerator.java

License:Open Source License

@Override
public Instance nextInstance() {

    int numAtts = this.numAttsOption.getValue();
    double[] attVals = new double[numAtts + 1];
    double sum = 0.0;
    double sumWeights = 0.0;
    for (int i = 0; i < numAtts; i++) {
        attVals[i] = this.instanceRandom.nextDouble();
        sum += this.weights[i] * attVals[i];
        sumWeights += this.weights[i];
    }//from  w  w  w  .  j a  v  a2 s  .c om
    int classLabel;
    if (sum >= sumWeights * 0.5) {
        classLabel = 1;
    } else {
        classLabel = 0;
    }
    //Add Noise
    if ((1 + (this.instanceRandom.nextInt(100))) <= this.noisePercentageOption.getValue()) {
        classLabel = (classLabel == 0 ? 1 : 0);
    }

    Instance inst = new DenseInstance(1.0, attVals);
    inst.setDataset(getHeader());
    inst.setClassValue(classLabel);
    addDrift();
    return inst;
}

From source file:moa.streams.generators.HyperplaneGeneratorReg.java

License:Open Source License

@Override
public Instance nextInstance() {

    int numAtts = this.numAttsOption.getValue();
    double[] attVals = new double[numAtts + 1];
    double sum = 0.0;
    double sumWeights = 0.0;
    double sumSquareWeights = 0.0;

    for (int i = 0; i < numAtts; i++) {
        attVals[i] = this.instanceRandom.nextDouble();
        sum += this.weights[i] * attVals[i];
        sumWeights += this.weights[i];
        sumSquareWeights += Math.pow(this.weights[i], 2);
    }/*from w  w w  .j  a  v a  2  s .c o m*/
    double classLabel = 0;
    if (targetValueOption.getChosenIndex() == 0)
        if (sum >= sumWeights * 0.5) {
            classLabel = 1;
        } else {
            classLabel = 0;
        }
    else if (targetValueOption.getChosenIndex() == 1)
        classLabel = sum / Math.sqrt(sumSquareWeights);
    else if (targetValueOption.getChosenIndex() == 2)
        classLabel = Math.pow(sum, 2) / sumSquareWeights;
    else if (targetValueOption.getChosenIndex() == 3)
        classLabel = Math.pow(sum / Math.sqrt(sumSquareWeights), 3);

    //Add Noise
    if ((1 + (this.instanceRandom.nextInt(100))) <= this.noisePercentageOption.getValue()) {

        if (targetValueOption.getChosenIndex() == 0) {
            if ((1 + (this.instanceRandom.nextInt(100))) <= this.noisePercentageOption.getValue()) {
                classLabel = (classLabel == 0 ? 1 : 0);
            }
        } else {
            // the range of noise added to the distance increases by the increase of the number of dimentsions  
            double temp = this.instanceRandom.nextInt(1000);
            temp = temp * Math.sqrt(numAtts) / 1000;

            if (targetValueOption.getChosenIndex() == 1)
                temp = Math.pow(temp, 1);
            else if (targetValueOption.getChosenIndex() == 2)
                temp = Math.pow(temp, 2);
            else if (targetValueOption.getChosenIndex() == 3)
                temp = Math.pow(temp, 3);

            if (this.instanceRandom.nextInt(2) == 1)
                classLabel -= temp;
            else
                classLabel += temp;
            classLabel = Math.abs(classLabel);
        }
    }

    //Instance inst = new DenseInstance(1.0, attVals);
    Instance inst = new DenseInstance(1.0, attVals);
    inst.setDataset(getHeader());
    inst.setClassValue(classLabel);
    addDrift();
    return inst;
}

From source file:moa.streams.generators.InducedRandomNonStationaryDataGenerator.java

License:Apache License

@Override
public Instance nextInstance() {
    // 1.) Pick a concept at weighted random
    DriftingExemplarInstance[] activeConceptsArray = activeConcepts.toArray(new DriftingExemplarInstance[1]);
    int nextConceptIdx = rng.nextInt(activeConceptsArray.length);

    // 2.) Pull the sampled Instance from the concept
    DenseInstance candidateInstance = activeConceptsArray[nextConceptIdx].nextInstance();
    Instance inst = new DenseInstance(candidateInstance);
    inst.setDataset(getHeader());

    // 3.) Add label noise
    if (rng.nextDouble() < this.labelNoiseOption.getValue() / 100.0) {
        inst.setClassValue(rng.nextInt(numClasses));
    }/*  ww w .  ja  v a2 s.  c  o m*/

    // 4.) Shift active concepts
    if (!inactiveConcepts.isEmpty()
            && rng.nextDouble() < this.classActivationProbabilityOption.getValue() / 100.0) {
        DriftingExemplarInstance[] inactiveConceptsArray = inactiveConcepts
                .toArray(new DriftingExemplarInstance[1]);
        int idxToActivate = rng.nextInt(inactiveConceptsArray.length);
        int idxToDeactivate = rng.nextInt(activeConceptsArray.length);
        activeConcepts.add(inactiveConceptsArray[idxToActivate]);
        activeConcepts.remove(activeConceptsArray[idxToDeactivate]);
        inactiveConcepts.add(activeConceptsArray[idxToDeactivate]);
        inactiveConcepts.remove(inactiveConceptsArray[idxToActivate]);
    }

    //5.) Return new instance
    return inst;
}

From source file:moa.streams.generators.LEDGenerator.java

License:Open Source License

@Override
public Instance nextInstance() {
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setDataset(header);
    int selected = this.instanceRandom.nextInt(10);
    for (int i = 0; i < 7; i++) {
        if ((1 + (this.instanceRandom.nextInt(100))) <= this.noisePercentageOption.getValue()) {
            inst.setValue(i, originalInstances[selected][i] == 0 ? 1 : 0);
        } else {/*from www  .j  a  va2  s . c om*/
            inst.setValue(i, originalInstances[selected][i]);
        }
    }
    if (!this.suppressIrrelevantAttributesOption.isSet()) {
        for (int i = 0; i < NUM_IRRELEVANT_ATTRIBUTES; i++) {
            inst.setValue(i + 7, this.instanceRandom.nextInt(2));
        }
    }
    inst.setClassValue(selected);
    return inst;
}

From source file:moa.streams.generators.LEDGeneratorDrift.java

License:Open Source License

@Override
public Instance nextInstance() {
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setDataset(header);
    int selected = this.instanceRandom.nextInt(10);
    for (int i = 0; i < 7; i++) {
        if ((1 + (this.instanceRandom.nextInt(100))) <= this.noisePercentageOption.getValue()) {
            inst.setValue(this.numberAttribute[i], originalInstances[selected][i] == 0 ? 1 : 0);
        } else {/*  www  . j av  a2s. c  o  m*/
            inst.setValue(this.numberAttribute[i], originalInstances[selected][i]);
        }
    }
    if (!this.suppressIrrelevantAttributesOption.isSet()) {
        for (int i = 0; i < NUM_IRRELEVANT_ATTRIBUTES; i++) {
            inst.setValue(this.numberAttribute[i + 7], this.instanceRandom.nextInt(2));
        }
    }
    inst.setClassValue(selected);
    return inst;
}

From source file:moa.streams.generators.multilabel.MetaMultilabelGenerator.java

License:Open Source License

/**
 * GenerateMLInstance.//from   www.ja v  a  2  s .  co  m
 *
 * @param   Y   a set of label [indices]
 * @return a multit-labelled example
 */
private Instance generateMLInstance(HashSet<Integer> Y) {

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

    // set classes
    for (int j = 0; j < m_L; j++) {
        x_ml.setValue(j, 0.0);
    }
    for (int l : Y) {
        x_ml.setValue(l, 1.0);
    }

    // generate binary instances
    Instance x_0 = getNextWithBinary(0);
    Instance x_1 = 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 (Y.containsAll(m_TopCombinations[a])) {
            x_ml.setValue(m_L + a, x_1.value(a));
            //x_ml.setValue(m_L+a,1.0);
        } // The combination is absent: use a negative value
        else {
            x_ml.setValue(m_L + a, x_0.value(a));
            //x_ml.setValue(m_L+a,0.0);
        }
    }

    return x_ml;
}

From source file:moa.streams.generators.MySEAGenerator.java

License:Open Source License

@Override
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 av  a 2s.  co m
        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:moa.streams.generators.RandomRBFGenerator.java

License:Open Source License

@Override
public Instance nextInstance() {
    Centroid centroid = this.centroids[MiscUtils.chooseRandomIndexBasedOnWeights(this.centroidWeights,
            this.instanceRandom)];
    int numAtts = this.numAttsOption.getValue();
    double[] attVals = new double[numAtts + 1];
    for (int i = 0; i < numAtts; i++) {
        attVals[i] = (this.instanceRandom.nextDouble() * 2.0) - 1.0;
    }/*from   w  ww . j a  va  2s.  c o  m*/
    double magnitude = 0.0;
    for (int i = 0; i < numAtts; i++) {
        magnitude += attVals[i] * attVals[i];
    }
    magnitude = Math.sqrt(magnitude);
    double desiredMag = this.instanceRandom.nextGaussian() * centroid.stdDev;
    double scale = desiredMag / magnitude;
    for (int i = 0; i < numAtts; i++) {
        attVals[i] = centroid.centre[i] + attVals[i] * scale;
    }
    Instance inst = new DenseInstance(1.0, attVals);
    inst.setDataset(getHeader());
    inst.setClassValue(centroid.classLabel);
    return inst;
}

From source file:moa.streams.generators.RandomTreeGenerator.java

License:Open Source License

@Override
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   www .ja v a2s.c  o  m
    }
    inst.setDataset(header);
    inst.setClassValue(classifyInstance(this.treeRoot, attVals));
    return inst;
}