Example usage for weka.core Instance setClassValue

List of usage examples for weka.core Instance setClassValue

Introduction

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

Prototype

public void setClassValue(String value);

Source Link

Document

Sets the class value of an instance to the given value.

Usage

From source file:moa.clusterer.outliers.Sieve.java

License:Apache License

/**
 *
 * @param c cluster that is being compared against
 * @param x real data instance// w  w w  .  j a va2s .c  om
 * @return DenseInstance made to work with the outlier-detecting perceptron
 */
private Instance makePerceptronInstance(Riffle c, Instance x) {
    Instance pseudoPoint = new DenseInstance(this.outlierPerceptronTrainingSet.numAttributes());
    pseudoPoint.setDataset(outlierPerceptronTrainingSet);
    double p = c.getInclusionProbability(x);
    double r = (c.getRadius() != 0) ? c.getRadius() : 1;
    //double w = c.getWeight();
    double N = Math.min(c.size(), this.cacheSizeOption.getValue());
    double d = c.getCenterDistance(x);
    double logP = (p == 0) ? 0 : Math.log(p);
    double logDR = (r == 0 || (d / r) == 0) ? 0 : Math.log(d / r);
    pseudoPoint.setValue(0, logP);
    pseudoPoint.setValue(1, logDR);
    pseudoPoint.setValue(2, logDR * logP);
    pseudoPoint.setValue(3,
            logP - Math.log(1.0 / Math.pow(2.0 * N, this.universalCluster.getHeader().numAttributes())));
    pseudoPoint.setClassValue(0);
    pseudoPoint.setWeight(0.0);
    return pseudoPoint;
}

From source file:moa.clusterer.outliers.Sieve.java

License:Apache License

/**
 * @return training accuracy/*from ww  w  .  ja va 2 s .  c  o  m*/
 */
private double trainPerceptron() {
    // Train the perceptron from warmup phase clustering 
    final int epochs = 20;
    final int numberOfPerceptrons = 1;
    final int MEMBER = 0;
    final int OUTLIER = 1;
    double accuracySum = 0;
    double accuracyCount = 0;
    this.outlierPerceptronTrainingSet.clear();
    Random rng = new Random(this.randomSeed);

    // Generate training set
    for (Riffle thisCluster : this.clusters) {
        for (Instance x : thisCluster.getHeader()) {
            Instance pseudoPt = makePerceptronInstance(thisCluster, x);
            for (Riffle thatCluster : this.clusters) {
                double groundTruth = (thisCluster == thatCluster) ? MEMBER : OUTLIER;
                pseudoPt.setClassValue(groundTruth);
                this.outlierPerceptronTrainingSet.add(pseudoPt);
            }
        }
    }
    for (Instance x : this.outlierPerceptronTrainingSet) {
        x.setWeight(1.0 / this.outlierPerceptronTrainingSet.numInstances());
    }
    ;

    // Boost it
    this.perceptrons = new Perceptron[numberOfPerceptrons];
    this.pweights = new double[numberOfPerceptrons];
    for (int perceptronIdx = 0; perceptronIdx < numberOfPerceptrons; ++perceptronIdx) {
        // Discover new weak learner
        Perceptron candidatePerceptron = new Perceptron();
        candidatePerceptron.prepareForUse();
        candidatePerceptron.learningRatioOption.setValue(rng.nextDouble() * 0.9 + 0.1);
        for (int epoch = 0; epoch < epochs; epoch++) {
            for (Instance x : this.outlierPerceptronTrainingSet) {
                if ((rng.nextDouble() / this.outlierPerceptronTrainingSet.numInstances()) < x.weight()) { // weighted subsampling
                    candidatePerceptron.trainOnInstance(x);
                }
            }
        } //end epochs
          // Evaluate weak learner
        double errorFunctionSum = 0;
        double weightSum = 0;
        for (Instance x : this.outlierPerceptronTrainingSet) {
            if (!candidatePerceptron.correctlyClassifies(x)) {
                errorFunctionSum += x.weight();
            }
        }
        // adjust training weights
        for (Instance x : this.outlierPerceptronTrainingSet) {
            double newWeight = x.weight();
            if (candidatePerceptron.correctlyClassifies(x)) {
                newWeight *= errorFunctionSum / (1.0 - errorFunctionSum);
                if (Double.isNaN(newWeight)) {
                    newWeight = weka.core.Utils.SMALL;
                }
                x.setWeight(newWeight);
            }
            weightSum += newWeight;
        }
        // Normalize
        for (Instance x : this.outlierPerceptronTrainingSet) {
            x.setWeight(x.weight() / weightSum);
        }
        // Add to ensemble
        double newPerceptronWeight = Math.log((1 - errorFunctionSum) / errorFunctionSum);

        this.perceptrons[perceptronIdx] = candidatePerceptron;
        this.pweights[perceptronIdx] = newPerceptronWeight;
    } // end numPerceptrons

    // Check training error
    accuracySum = 0;
    accuracyCount = 0;
    for (Instance x : this.outlierPerceptronTrainingSet) {
        if (this.getPerceptronVotesForOutlierStatus(x) == x.classValue()) {
            accuracySum++;
        }
        accuracyCount++;
    }
    double trainingAccuracy = (accuracyCount > 0) ? (accuracySum / accuracyCount) : 0.0;
    this.outlierPerceptronTrainingSet.clear();
    return trainingAccuracy;
}

From source file:moa.clusterers.AmidstClusteringAlgorithm.java

License:Apache License

/**
 * {@inheritDoc}//from  ww  w. j  a v  a  2 s  . c  om
 */
@Override
public Clustering getClusteringResult() {
    //sourceClustering = new Clustering();

    Instances dataset = getDataset(attributes_.getNumberOfAttributes(), getNumClusters());
    Instances newInstances = new Instances(dataset);

    if (bnModel_ == null) {
        //parameterLearningAlgorithm_.setParallelMode(isParallelMode_());
        parameterLearningAlgorithm_.setDAG(dag);
        ((SVB) parameterLearningAlgorithm_).setWindowsSize(timeWindowOption.getValue());
        parameterLearningAlgorithm_.initLearning();
        parameterLearningAlgorithm_.updateModel(batch_);
    } else {
        parameterLearningAlgorithm_.updateModel(batch_);
    }

    bnModel_ = parameterLearningAlgorithm_.getLearntBayesianNetwork();
    predictions_.setModel(bnModel_);

    for (DataInstance dataInstance : batch_) {
        this.predictions_.setEvidence(dataInstance);
        this.predictions_.runInference();
        Multinomial multinomial = this.predictions_.getPosterior(clusterVar_);

        double[] results = multinomial.getProbabilities();

        int cnum = IntStream.rangeClosed(0, getNumClusters() - 1)
                .reduce((a, b) -> (results[a] > results[b]) ? a : b).getAsInt();

        double[] attValues = dataInstance.toArray();
        Instance newInst = new DenseInstance(1.0, attValues);
        newInst.insertAttributeAt(attributes_.getNumberOfAttributes());
        newInst.setDataset(dataset);
        newInst.setClassValue(cnum);
        newInstances.add(newInst);
    }
    clustering = new Clustering(newInstances);

    return clustering;
}

From source file:moa.streams.clustering.RandomRBFGeneratorEvents.java

License:Apache License

public Instance nextInstance() {
    numGeneratedInstances++;//from w  w  w .  ja  va  2 s .  c  om
    eventScheduler();

    //make room for the classlabel
    double[] values_new = new double[numAttsOption.getValue() + 1];
    double[] values = null;
    int clusterChoice = -1;

    if (instanceRandom.nextDouble() > noiseLevelOption.getValue()) {
        clusterChoice = chooseWeightedElement();
        values = kernels.get(clusterChoice).generator.sample(instanceRandom).toDoubleArray();
    } else {
        //get ranodm noise point
        values = getNoisePoint();
    }

    if (Double.isNaN(values[0])) {
        System.out.println("Instance corrupted:" + numGeneratedInstances);
    }
    System.arraycopy(values, 0, values_new, 0, values.length);

    Instance inst = new DenseInstance(1.0, values_new);
    inst.setDataset(getHeader());
    if (clusterChoice == -1) {
        // 2013/06/02 (Yunsu Kim)
        // Noise instance has the last class value instead of "-1"
        // Preventing ArrayIndexOutOfBoundsException in WriteStreamToARFFFile
        inst.setClassValue(numClusterOption.getValue());
    } else {
        inst.setClassValue(kernels.get(clusterChoice).generator.getId());
        //Do we need micro cluster representation if have overlapping clusters?
        //if(!overlappingOption.isSet())
        kernels.get(clusterChoice).addInstance(inst);
    }
    //        System.out.println(numGeneratedInstances+": Overlap is"+updateOverlaps());

    return inst;
}

From source file:moa.streams.ConceptDriftRealStream.java

License:Open Source License

@Override
public Instance nextInstance() {
    numberInstanceStream++;//ww  w.  j  a  v  a2  s.c o  m
    double numclass = 0.0;
    double x = -4.0 * (double) (numberInstanceStream - this.positionOption.getValue())
            / (double) this.widthOption.getValue();
    double probabilityDrift = 1.0 / (1.0 + Math.exp(x));
    if (this.random.nextDouble() > probabilityDrift) {
        if (this.inputStream.hasMoreInstances() == false) {
            this.inputStream.restart();
        }
        this.inputInstance = this.inputStream.nextInstance();
        numclass = this.inputInstance.classValue();
    } else {
        if (this.driftStream.hasMoreInstances() == false) {
            this.driftStream.restart();
        }
        this.driftInstance = this.driftStream.nextInstance();
        numclass = this.driftInstance.classValue();
    }
    int m = 0;
    double[] newVals = new double[this.inputInstance.numAttributes() + this.driftInstance.numAttributes() - 1];
    for (int j = 0; j < this.inputInstance.numAttributes() - 1; j++, m++) {
        newVals[m] = this.inputInstance.value(j);
    }
    for (int j = 0; j < this.driftInstance.numAttributes() - 1; j++, m++) {
        newVals[m] = this.driftInstance.value(j);
    }
    newVals[m] = numclass;
    //return new Instance(1.0, newVals);
    Instance inst = new DenseInstance(1.0, newVals);
    inst.setDataset(this.getHeader());
    inst.setClassValue(numclass);
    return inst;

}

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

License:Open Source License

@Override
public Instance nextInstance() {
    double salary = 0, commission = 0, hvalue = 0, loan = 0;
    int age = 0, elevel = 0, car = 0, zipcode = 0, hyears = 0, group = 0;
    boolean desiredClassFound = false;
    while (!desiredClassFound) {
        // generate attributes
        salary = 20000.0 + 130000.0 * this.instanceRandom.nextDouble();
        commission = (salary >= 75000.0) ? 0 : (10000.0 + 65000.0 * this.instanceRandom.nextDouble());
        // true to c implementation:
        // if (instanceRandom.nextDouble() < 0.5 && salary < 75000.0)
        // commission = 10000.0 + 65000.0 * instanceRandom.nextDouble();
        age = 20 + this.instanceRandom.nextInt(61);
        elevel = this.instanceRandom.nextInt(5);
        car = this.instanceRandom.nextInt(20);
        zipcode = this.instanceRandom.nextInt(9);
        hvalue = (9.0 - zipcode) * 100000.0 * (0.5 + this.instanceRandom.nextDouble());
        hyears = 1 + this.instanceRandom.nextInt(30);
        loan = this.instanceRandom.nextDouble() * 500000.0;
        // determine class
        group = classificationFunctions[this.functionOption.getValue() - 1].determineClass(salary, commission,
                age, elevel, car, zipcode, hvalue, hyears, loan);
        if (!this.balanceClassesOption.isSet()) {
            desiredClassFound = true;/*from  www  . ja va2s .co m*/
        } else {
            // balance the classes
            if ((this.nextClassShouldBeZero && (group == 0)) || (!this.nextClassShouldBeZero && (group == 1))) {
                desiredClassFound = true;
                this.nextClassShouldBeZero = !this.nextClassShouldBeZero;
            } // else keep searching
        }
    }
    // perturb values
    if (this.peturbFractionOption.getValue() > 0.0) {
        salary = perturbValue(salary, 20000, 150000);
        if (commission > 0) {
            commission = perturbValue(commission, 10000, 75000);
        }
        age = (int) Math.round(perturbValue(age, 20, 80));
        hvalue = perturbValue(hvalue, (9.0 - zipcode) * 100000.0, 0, 135000);
        hyears = (int) Math.round(perturbValue(hyears, 1, 30));
        loan = perturbValue(loan, 0, 500000);
    }
    // construct instance
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setValue(0, salary);
    inst.setValue(1, commission);
    inst.setValue(2, age);
    inst.setValue(3, elevel);
    inst.setValue(4, car);
    inst.setValue(5, zipcode);
    inst.setValue(6, hvalue);
    inst.setValue(7, hyears);
    inst.setValue(8, loan);
    inst.setDataset(header);
    inst.setClassValue(group);
    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 va  2 s  . c  o  m*/
    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 ww  w  .j a  va 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());/*from   w ww  . java 2 s  .  com*/

    // 3.) Add label noise
    if (rng.nextDouble() < this.labelNoiseOption.getValue() / 100.0) {
        inst.setClassValue(rng.nextInt(numClasses));
    }

    // 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);//from  ww  w . j ava  2 s . c om
    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 {
            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;
}