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:moa.clusterer.FeS2.java

License:Apache License

/**
 * /*  ww  w  .j av  a  2s  .c om*/
 * @param c cluster that is being compared against
 * @param x real data instance
 * @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(), 1.0 / (this.learningRateAlphaOption.getValue() + 1e-9));
    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

/**
 *
 * @param c cluster that is being compared against
 * @param x real data instance//  ww  w .j a v  a 2s  . c  o m
 * @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.streams.clustering.FileStream.java

License:Apache License

protected boolean readNextInstanceFromFile() {
    try {/*from  w w w.  j  a  v a2s . co  m*/

        if (this.instances.readInstance(this.fileReader)) {
            Instance rawInstance = this.instances.instance(0);

            //remove dataset from instance so we can delete attributes
            rawInstance.setDataset(null);
            for (int i = removeAttributes.length - 1; i >= 0; i--) {
                rawInstance.deleteAttributeAt(removeAttributes[i]);
            }
            //set adjusted dataset for instance
            rawInstance.setDataset(filteredDataset);

            if (normalizeOption.isSet() && valuesMinMaxDiff != null) {
                for (int i = 0; i < rawInstance.numAttributes(); i++) {
                    if (valuesMinMaxDiff.get(i)[2] != 1 && // Already normalized
                            valuesMinMaxDiff.get(i)[2] != 0 && // Max. value is 0 (unable to be normalized)
                            i != rawInstance.classIndex()) { // Class label is not subject to be normalized
                        double v = rawInstance.value(i);
                        v = (v - valuesMinMaxDiff.get(i)[0]) / valuesMinMaxDiff.get(i)[2];
                        rawInstance.setValue(i, v);
                    }
                }
            }

            this.lastInstanceRead = rawInstance;
            this.instances.delete(); // keep instances clean
            this.numInstancesRead++;
            return true;
        }
        if (this.fileReader != null) {
            this.fileReader.close();
            this.fileReader = null;
        }
        return false;
    } catch (IOException ioe) {
        throw new RuntimeException("ArffFileStream failed to read instance from stream.", ioe);
    }
}

From source file:moa.streams.filters.AddNoiseFilter.java

License:Open Source License

@Override
public Instance nextInstance() {
    Instance inst = (Instance) this.inputStream.nextInstance().copy();
    for (int i = 0; i < inst.numAttributes(); i++) {
        double noiseFrac = i == inst.classIndex() ? this.classNoiseFractionOption.getValue()
                : this.attNoiseFractionOption.getValue();
        if (inst.attribute(i).isNominal()) {
            DoubleVector obs = (DoubleVector) this.attValObservers.get(i);
            if (obs == null) {
                obs = new DoubleVector();
                this.attValObservers.set(i, obs);
            }/*from   w  w w  .ja  v  a  2  s  .  co m*/
            int originalVal = (int) inst.value(i);
            if (!inst.isMissing(i)) {
                obs.addToValue(originalVal, inst.weight());
            }
            if ((this.random.nextDouble() < noiseFrac) && (obs.numNonZeroEntries() > 1)) {
                do {
                    inst.setValue(i, this.random.nextInt(obs.numValues()));
                } while (((int) inst.value(i) == originalVal) || (obs.getValue((int) inst.value(i)) == 0.0));
            }
        } else {
            GaussianEstimator obs = (GaussianEstimator) this.attValObservers.get(i);
            if (obs == null) {
                obs = new GaussianEstimator();
                this.attValObservers.set(i, obs);
            }
            obs.addObservation(inst.value(i), inst.weight());
            inst.setValue(i, inst.value(i) + this.random.nextGaussian() * obs.getStdDev() * noiseFrac);
        }
    }
    return inst;
}

From source file:moa.streams.filters.ReplacingMissingValuesFilter.java

License:Open Source License

@Override
public Instance nextInstance() {
    Instance inst = (Instance) this.inputStream.nextInstance().copy();

    // Initialization
    if (numAttributes < 0) {
        numAttributes = inst.numAttributes();
        columnsStatistics = new double[numAttributes];
        numberOfSamples = new long[numAttributes];
        lastNominalValues = new String[numAttributes];
        frequencies = new HashMap[numAttributes];
        for (int i = 0; i < inst.numAttributes(); i++) {
            if (inst.attribute(i).isNominal())
                frequencies[i] = new HashMap<String, Integer>();
        }/*  w w w .j  a  v  a 2 s  . c om*/

        numericalSelectedStrategy = this.numericReplacementStrategyOption.getChosenIndex();
        nominalSelectedStrategy = this.nominalReplacementStrategyOption.getChosenIndex();
    }

    for (int i = 0; i < numAttributes; i++) {

        // ---- Numerical values ----
        if (inst.attribute(i).isNumeric()) {
            // Handle missing value
            if (inst.isMissing(i)) {
                switch (numericalSelectedStrategy) {
                case 0: // NOTHING
                    break;
                case 1: // LAST KNOWN VALUE
                case 2: // MEAN
                case 3: // MAX
                case 4: // MIN
                    inst.setValue(i, columnsStatistics[i]);
                    break;
                case 5: // CONSTANT
                    inst.setValue(i, numericalConstantValueOption.getValue());
                    break;
                default:
                    continue;
                }
            }
            // Update statistics with non-missing values
            else {
                switch (numericalSelectedStrategy) {
                case 1: // LAST KNOWN VALUE
                    columnsStatistics[i] = inst.value(i);
                    break;
                case 2: // MEAN
                    numberOfSamples[i]++;
                    columnsStatistics[i] = columnsStatistics[i]
                            + (inst.value(i) - columnsStatistics[i]) / numberOfSamples[i];
                    break;
                case 3: // MAX
                    columnsStatistics[i] = columnsStatistics[i] < inst.value(i) ? inst.value(i)
                            : columnsStatistics[i];
                    break;
                case 4: // MIN
                    columnsStatistics[i] = columnsStatistics[i] > inst.value(i) ? inst.value(i)
                            : columnsStatistics[i];
                    break;
                default:
                    continue;
                }
            }
        }
        // ---- Nominal values ----
        else if (inst.attribute(i).isNominal()) {
            // Handle missing value
            if (inst.isMissing(i)) {
                switch (nominalSelectedStrategy) {
                case 0: // NOTHING
                    break;
                case 1: // LAST KNOWN VALUE
                    if (lastNominalValues[i] != null) {
                        inst.setValue(i, lastNominalValues[i]);
                    }
                    break;
                case 2: // MODE
                    if (!frequencies[i].isEmpty()) {
                        // Sort the map to get the most frequent value
                        Map<String, Integer> sortedMap = MapUtil.sortByValue(frequencies[i]);
                        inst.setValue(i, sortedMap.entrySet().iterator().next().getKey());
                    }
                    break;
                default:
                    continue;
                }
            }
            // Update statistics with non-missing values
            else {
                switch (nominalSelectedStrategy) {
                case 1: // LAST KNOWN VALUE
                    lastNominalValues[i] = inst.stringValue(i);
                    break;
                case 2: // MODE
                    Integer previousCounter = frequencies[i].get(inst.stringValue(i));
                    if (previousCounter == null)
                        previousCounter = 0;
                    frequencies[i].put(inst.stringValue(i), ++previousCounter);
                    break;
                default:
                    continue;
                }
            }
        }
    }

    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  w  ww.j  ava  2 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
        }
    }
    // 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.cd.AbstractConceptDriftGenerator.java

License:Open Source License

public Instance nextInstance() {
    this.numInstances++;
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setDataset(header);/*w w w . j  a  v  a2  s .  c om*/
    double nextValue = this.nextValue();
    if (this.notBinaryStreamOption.isSet()) {
        inst.setValue(0, nextValue);
    } 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.LEDGenerator.java

License:Open Source License

@Override
public Instance nextInstance() {
    InstancesHeader header = getHeader();
    Instance inst = new DenseInstance(header.numAttributes());
    inst.setDataset(header);/*from w w w  . ja va2s . 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;
}

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);/*ww w  . j  av a  2 s  . c o m*/
    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 {
            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./* w  ww  . j av a 2s. c o 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;
}