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:Helper.ClassifyHelper.java

public static void clasifyInstance(Classifier cls, Instance inst) throws Exception {
    double result = cls.classifyInstance(inst);
    inst.setClassValue(result);
}

From source file:hsa_jni.hsa_jni.EvaluatePeriodicHeldOutTestBatch.java

License:Open Source License

@Override
protected Object doMainTask(TaskMonitor monitor, ObjectRepository repository) {
    Classifier learner = (Classifier) getPreparedClassOption(this.learnerOption);
    InstanceStream stream = (InstanceStream) getPreparedClassOption(this.streamOption);
    ClassificationPerformanceEvaluator evaluator = (ClassificationPerformanceEvaluator) getPreparedClassOption(
            this.evaluatorOption);
    learner.setModelContext(stream.getHeader());
    long instancesProcessed = 0;
    LearningCurve learningCurve = new LearningCurve("evaluation instances");
    File dumpFile = this.dumpFileOption.getFile();
    PrintStream immediateResultStream = null;
    if (dumpFile != null) {
        try {/*from   w w w  .  j a  v a2s .  c o m*/
            if (dumpFile.exists()) {
                immediateResultStream = new PrintStream(new FileOutputStream(dumpFile, true), true);
            } else {
                immediateResultStream = new PrintStream(new FileOutputStream(dumpFile), true);
            }
        } catch (Exception ex) {
            throw new RuntimeException("Unable to open immediate result file: " + dumpFile, ex);
        }
    }
    boolean firstDump = true;
    InstanceStream testStream = null;
    int testSize = this.testSizeOption.getValue();
    if (this.cacheTestOption.isSet()) {
        monitor.setCurrentActivity("Caching test examples...", -1.0);
        Instances testInstances = new Instances(stream.getHeader(), this.testSizeOption.getValue());
        while (testInstances.numInstances() < testSize) {
            testInstances.add(stream.nextInstance());
            if (testInstances.numInstances() % INSTANCES_BETWEEN_MONITOR_UPDATES == 0) {
                if (monitor.taskShouldAbort()) {
                    return null;
                }
                monitor.setCurrentActivityFractionComplete(
                        (double) testInstances.numInstances() / (double) (this.testSizeOption.getValue()));
            }
        }
        testStream = new CachedInstancesStream(testInstances);
    } else {
        //testStream = (InstanceStream) stream.copy();
        testStream = stream;
        /*monitor.setCurrentActivity("Skipping test examples...", -1.0);
        for (int i = 0; i < testSize; i++) {
        stream.nextInstance();
        }*/
    }
    instancesProcessed = 0;
    TimingUtils.enablePreciseTiming();
    double totalTrainTime = 0.0;
    while ((this.trainSizeOption.getValue() < 1 || instancesProcessed < this.trainSizeOption.getValue())
            && stream.hasMoreInstances() == true) {
        monitor.setCurrentActivityDescription("Training...");
        long instancesTarget = instancesProcessed + this.sampleFrequencyOption.getValue();

        ArrayList<Instance> instanceCache = new ArrayList<Instance>();
        long trainStartTime = TimingUtils.getNanoCPUTimeOfCurrentThread();
        double lastTrainTime = 0;
        while (instancesProcessed < instancesTarget && stream.hasMoreInstances() == true) {
            instanceCache.add(stream.nextInstance());
            instancesProcessed++;
            if (instancesProcessed % INSTANCES_BETWEEN_MONITOR_UPDATES == 0) {
                if (monitor.taskShouldAbort()) {
                    return null;
                }
                monitor.setCurrentActivityFractionComplete(
                        (double) (instancesProcessed) / (double) (this.trainSizeOption.getValue()));
            }
            if (instanceCache.size() % 1000 == 0) {
                trainStartTime = TimingUtils.getNanoCPUTimeOfCurrentThread();
                for (Instance inst : instanceCache) {
                    learner.trainOnInstance(inst);
                }
                lastTrainTime += TimingUtils
                        .nanoTimeToSeconds(TimingUtils.getNanoCPUTimeOfCurrentThread() - trainStartTime);
                instanceCache.clear();
            }

        }
        trainStartTime = TimingUtils.getNanoCPUTimeOfCurrentThread();
        for (Instance inst : instanceCache) {
            learner.trainOnInstance(inst);
        }
        if (learner instanceof BatchClassifier)
            ((BatchClassifier) learner).commit();
        lastTrainTime += TimingUtils
                .nanoTimeToSeconds(TimingUtils.getNanoCPUTimeOfCurrentThread() - trainStartTime);

        totalTrainTime += lastTrainTime;
        if (totalTrainTime > this.trainTimeOption.getValue()) {
            break;
        }
        if (this.cacheTestOption.isSet()) {
            testStream.restart();
        }
        evaluator.reset();
        long testInstancesProcessed = 0;
        monitor.setCurrentActivityDescription("Testing (after "
                + StringUtils.doubleToString(
                        ((double) (instancesProcessed) / (double) (this.trainSizeOption.getValue()) * 100.0), 2)
                + "% training)...");
        long testStartTime = TimingUtils.getNanoCPUTimeOfCurrentThread();
        int instCount = 0;
        for (instCount = 0; instCount < testSize; instCount++) {
            if (stream.hasMoreInstances() == false) {
                break;
            }
            Instance testInst = (Instance) testStream.nextInstance().copy();
            double trueClass = testInst.classValue();
            testInst.setClassMissing();
            double[] prediction = learner.getVotesForInstance(testInst);
            testInst.setClassValue(trueClass);
            evaluator.addResult(testInst, prediction);
            testInstancesProcessed++;
            if (testInstancesProcessed % INSTANCES_BETWEEN_MONITOR_UPDATES == 0) {
                if (monitor.taskShouldAbort()) {
                    return null;
                }
                monitor.setCurrentActivityFractionComplete(
                        (double) testInstancesProcessed / (double) (testSize));
            }
        }
        if (instCount != testSize) {
            break;
        }
        double testTime = TimingUtils
                .nanoTimeToSeconds(TimingUtils.getNanoCPUTimeOfCurrentThread() - testStartTime);
        List<Measurement> measurements = new ArrayList<Measurement>();
        measurements.add(new Measurement("evaluation instances", instancesProcessed));
        measurements.add(new Measurement("total train time", totalTrainTime));
        measurements.add(new Measurement("total train speed", instancesProcessed / totalTrainTime));
        measurements.add(new Measurement("last train time", lastTrainTime));
        measurements.add(
                new Measurement("last train speed", this.sampleFrequencyOption.getValue() / lastTrainTime));
        measurements.add(new Measurement("test time", testTime));
        measurements.add(new Measurement("test speed", this.testSizeOption.getValue() / testTime));
        Measurement[] performanceMeasurements = evaluator.getPerformanceMeasurements();
        for (Measurement measurement : performanceMeasurements) {
            measurements.add(measurement);
        }
        Measurement[] modelMeasurements = learner.getModelMeasurements();
        for (Measurement measurement : modelMeasurements) {
            measurements.add(measurement);
        }
        learningCurve.insertEntry(
                new LearningEvaluation(measurements.toArray(new Measurement[measurements.size()])));
        if (immediateResultStream != null) {
            if (firstDump) {
                immediateResultStream.println(learningCurve.headerToString());
                firstDump = false;
            }
            immediateResultStream.println(learningCurve.entryToString(learningCurve.numEntries() - 1));
            immediateResultStream.flush();
        }
        if (monitor.resultPreviewRequested()) {
            monitor.setLatestResultPreview(learningCurve.copy());
        }
        // if (learner instanceof HoeffdingTree
        // || learner instanceof HoeffdingOptionTree) {
        // int numActiveNodes = (int) Measurement.getMeasurementNamed(
        // "active learning leaves",
        // modelMeasurements).getValue();
        // // exit if tree frozen
        // if (numActiveNodes < 1) {
        // break;
        // }
        // int numNodes = (int) Measurement.getMeasurementNamed(
        // "tree size (nodes)", modelMeasurements)
        // .getValue();
        // if (numNodes == lastNumNodes) {
        // noGrowthCount++;
        // } else {
        // noGrowthCount = 0;
        // }
        // lastNumNodes = numNodes;
        // } else if (learner instanceof OzaBoost || learner instanceof
        // OzaBag) {
        // double numActiveNodes = Measurement.getMeasurementNamed(
        // "[avg] active learning leaves",
        // modelMeasurements).getValue();
        // // exit if all trees frozen
        // if (numActiveNodes == 0.0) {
        // break;
        // }
        // int numNodes = (int) (Measurement.getMeasurementNamed(
        // "[avg] tree size (nodes)",
        // learner.getModelMeasurements()).getValue() * Measurement
        // .getMeasurementNamed("ensemble size",
        // modelMeasurements).getValue());
        // if (numNodes == lastNumNodes) {
        // noGrowthCount++;
        // } else {
        // noGrowthCount = 0;
        // }
        // lastNumNodes = numNodes;
        // }
    }
    if (immediateResultStream != null) {
        immediateResultStream.close();
    }
    return learningCurve;
}

From source file:meka.classifiers.multilabel.incremental.RTUpdateable.java

License:Open Source License

@Override
public void updateClassifier(Instance x) throws Exception {

    int L = x.classIndex();

    for (int j = 0; j < L; j++) {
        if (x.value(j) > 0.0) {
            Instance x_j = convertInstance(x);
            x_j.setClassValue(j);
            ((UpdateableClassifier) m_Classifier).updateClassifier(x_j);
        }//from w  w w .  j ava 2  s. com
    }
}

From source file:meka.classifiers.multitarget.NSR.java

License:Open Source License

public Instances convertInstances(Instances D, int L) throws Exception {

    //Gather combinations
    HashMap<String, Integer> distinctCombinations = MLUtils.classCombinationCounts(D);
    if (getDebug())
        System.out.println("Found " + distinctCombinations.size() + " unique combinations");

    //Prune combinations
    MLUtils.pruneCountHashMap(distinctCombinations, m_P);
    if (getDebug())
        System.out.println("Pruned to " + distinctCombinations.size() + " with P=" + m_P);

    // Remove all class attributes
    Instances D_ = MLUtils.deleteAttributesAt(new Instances(D), MLUtils.gen_indices(L));
    // Add a new class attribute
    D_.insertAttributeAt(new Attribute("CLASS", new ArrayList(distinctCombinations.keySet())), 0); // create the class attribute
    D_.setClassIndex(0);//ww  w  .j  a v a2 s.c o m

    //Add class values
    for (int i = 0; i < D.numInstances(); i++) {
        String y = MLUtils.encodeValue(MLUtils.toIntArray(D.instance(i), L));
        // add it
        if (distinctCombinations.containsKey(y)) //if its class value exists
            D_.instance(i).setClassValue(y);
        // decomp
        else if (m_N > 0) {
            String d_subsets[] = SuperLabelUtils.getTopNSubsets(y, distinctCombinations, m_N);
            for (String s : d_subsets) {
                int w = distinctCombinations.get(s);
                Instance copy = (Instance) (D_.instance(i)).copy();
                copy.setClassValue(s);
                copy.setWeight(1.0 / d_subsets.length);
                D_.add(copy);
            }
        }
    }

    // remove with missing class
    D_.deleteWithMissingClass();

    // keep the header of new dataset for classification
    m_InstancesTemplate = new Instances(D_, 0);

    if (getDebug())
        System.out.println("" + D_);

    return D_;
}

From source file:meka.core.PSUtils.java

License:Open Source License

/**
 * Transform instances into a multi-class representation.
 * @param D         original dataset//from  w w w  .  j a  va 2  s . c o  m
 * @param L         number of labels in the original dataset
 * @param cname      class name for the new dataset (may want to encode the list of indices here for RAkEL-like methods)
 * @param p         pruning value
 * @param n         restoration value
 * @return transformed dataset
 */
public static Instances PSTransformation(Instances D, int L, String cname, int p, int n) {
    D = new Instances(D);

    // Gather combinations
    HashMap<LabelSet, Integer> distinctCombinations = PSUtils.countCombinationsSparse(D, L);

    // Prune combinations
    if (p > 0)
        MLUtils.pruneCountHashMap(distinctCombinations, p);

    // Check there are > 2
    if (distinctCombinations.size() <= 1 && p > 0) {
        // ... or try again if not ...
        System.err.println("[Warning] You did too much pruning, setting P = P-1");
        return PSTransformation(D, L, cname, p - 1, n);
    }

    // Create class attribute
    ArrayList<String> ClassValues = new ArrayList<String>();
    for (LabelSet y : distinctCombinations.keySet())
        ClassValues.add(y.toString());
    Attribute C = new Attribute(cname, ClassValues);

    // Insert new special attribute (which has all possible combinations of labels) 
    D.insertAttributeAt(C, L);
    D.setClassIndex(L);

    //Add class values
    int N = D.numInstances();
    for (int i = 0; i < N; i++) {
        Instance x = D.instance(i);
        LabelSet y = new LabelSet(MLUtils.toSparseIntArray(x, L));
        String y_string = y.toString();

        // add it
        if (ClassValues.contains(y_string)) //if its class value exists
            x.setClassValue(y_string);
        // decomp
        else if (n > 0) {
            //String d_subsets[] = getTopNSubsets(comb,distinctCombinations,n);
            LabelSet d_subsets[] = PSUtils.getTopNSubsets(y, distinctCombinations, n);
            //LabelSet d_subsets[] = PSUtils.cover(y,distinctCombinations);
            if (d_subsets.length > 0) {
                // fast
                x.setClassValue(d_subsets[0].toString());
                // additional
                if (d_subsets.length > 1) {
                    for (int s_i = 1; s_i < d_subsets.length; s_i++) {
                        Instance x_ = (Instance) (x).copy();
                        x_.setClassValue(d_subsets[s_i].toString());
                        D.add(x_);
                    }
                }
            } else {
                x.setClassMissing();
            }
        }
    }

    // remove with missing class
    D.deleteWithMissingClass();

    try {
        D = F.removeLabels(D, L);
    } catch (Exception e) {
        // should never happen
    }
    D.setClassIndex(0);

    return D;
}

From source file:meka.core.PSUtils.java

License:Open Source License

/**
 * Transform instances into a multi-class representation.
 * @param D         original dataset//from  w w  w  . j av a  2s.c om
 * @param L         number of labels in that dataset
 * @param cname      class name for the new dataset (may want to encode the list of indices here for RAkEL-like methods)
 * @param p         pruning value
 * @param n         restoration value
 * @return transformed dataset
 */
public static Instances SLTransformation(Instances D, int L, String cname, int p, int n) {
    D = new Instances(D);

    // Gather combinations
    HashMap<LabelSet, Integer> distinctCombinations = PSUtils.countCombinationsSparse(D, L);

    // Prune combinations
    if (p > 0)
        MLUtils.pruneCountHashMap(distinctCombinations, p);

    // Check there are > 2
    if (distinctCombinations.size() <= 1 && p > 0) {
        // ... or try again if not ...
        System.err.println("[Warning] You did too much pruning, setting P = P-1");
        return PSTransformation(D, L, cname, p - 1, n);
    }

    // Create class attribute
    ArrayList<String> ClassValues = new ArrayList<String>();
    for (LabelSet y : distinctCombinations.keySet())
        ClassValues.add(y.toString());
    Attribute C = new Attribute(cname, ClassValues);

    // Insert new special attribute (which has all possible combinations of labels)
    D.insertAttributeAt(C, L);
    D.setClassIndex(L);

    //Add class values
    int N = D.numInstances();
    for (int i = 0; i < N; i++) {
        Instance x = D.instance(i);
        LabelSet y = new LabelSet(MLUtils.toSparseIntArray(x, L));
        String y_string = y.toString();

        // add it
        if (ClassValues.contains(y_string)) //if its class value exists
            x.setClassValue(y_string);
        // decomp
        else if (n > 0) {
            //String d_subsets[] = getTopNSubsets(comb,distinctCombinations,n);
            LabelSet d_subsets[] = PSUtils.getTopNSubsets(y, distinctCombinations, n);
            //LabelSet d_subsets[] = PSUtils.cover(y,distinctCombinations);
            if (d_subsets.length > 0) {
                // fast
                x.setClassValue(d_subsets[0].toString());
                // additional
                if (d_subsets.length > 1) {
                    for (int s_i = 1; s_i < d_subsets.length; s_i++) {
                        Instance x_ = (Instance) (x).copy();
                        x_.setClassValue(d_subsets[s_i].toString());
                        D.add(x_);
                    }
                }
            } else {
                x.setClassMissing();
            }
        }
    }

    // remove with missing class
    D.deleteWithMissingClass();

    try {
        D = F.removeLabels(D, L);
    } catch (Exception e) {
        // should never happen
    }
    D.setClassIndex(0);

    return D;
}

From source file:milk.classifiers.SimpleMI.java

License:Open Source License

public Instances transform(Exemplars train) throws Exception {

     Instances data = new Instances(m_Attributes);// Data to learn a model   
     data.deleteAttributeAt(m_IdIndex);// ID attribute useless   
     Instances dataset = new Instances(data, 0);
     Instance template = new Instance(dataset.numAttributes());
     template.setDataset(dataset);//from  w ww .  j  a  v a 2s.c  om
     double N = train.numExemplars(); // Number of exemplars

     for (int i = 0; i < N; i++) {
         Exemplar exi = train.exemplar(i);
         Instances insts = exi.getInstances();
         int attIdx = 0;
         Instance newIns = new Instance(template);
         newIns.setDataset(dataset);
         for (int j = 0; j < insts.numAttributes(); j++) {
             if ((j == m_IdIndex) || (j == m_ClassIndex))
                 continue;
             double value;
             if (m_TransformMethod == 1) {
                 value = insts.meanOrMode(j);
             } else {
                 double[] minimax = minimax(insts, j);
                 value = (minimax[0] + minimax[1]) / 2.0;
             }
             newIns.setValue(attIdx++, value);
         }
         newIns.setClassValue(exi.classValue());
         data.add(newIns);
     }

     return data;
 }

From source file:moa.classifiers.LeveragingBag.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    int numClasses = inst.numClasses();
    //Output Codes
    if (this.initMatrixCodes == true) {
        this.matrixCodes = new int[this.ensemble.length][inst.numClasses()];
        for (int i = 0; i < this.ensemble.length; i++) {
            int numberOnes;
            int numberZeros;

            do { // until we have the same number of zeros and ones
                numberOnes = 0;/*  w  w  w . j  a  v a  2 s  . c o  m*/
                numberZeros = 0;
                for (int j = 0; j < numClasses; j++) {
                    int result = 0;
                    if (j == 1 && numClasses == 2) {
                        result = 1 - this.matrixCodes[i][0];
                    } else {
                        result = (this.classifierRandom.nextBoolean() ? 1 : 0);
                    }
                    this.matrixCodes[i][j] = result;
                    if (result == 1) {
                        numberOnes++;
                    } else {
                        numberZeros++;
                    }
                }
            } while ((numberOnes - numberZeros) * (numberOnes - numberZeros) > (this.ensemble.length % 2));

        }
        this.initMatrixCodes = false;
    }

    boolean Change = false;
    double w = 1.0;
    double mt = 0.0;
    Instance weightedInst = (Instance) inst.copy();
    /*for (int i = 0; i < this.ensemble.length; i++) {
    if (this.outputCodesOption.isSet()) {
    weightedInst.setClassValue((double) this.matrixCodes[i][(int) inst.classValue()] );
    }
    if(!this.ensemble[i].correctlyClassifies(weightedInst)) {
    mt++;
    }
    }*/
    //update w
    w = this.weightShrinkOption.getValue(); //1.0 +mt/2.0;
    //Train ensemble of classifiers
    for (int i = 0; i < this.ensemble.length; i++) {
        int k = MiscUtils.poisson(w, this.classifierRandom);
        if (k > 0) {
            if (this.outputCodesOption.isSet()) {
                weightedInst.setClassValue((double) this.matrixCodes[i][(int) inst.classValue()]);
            }
            weightedInst.setWeight(inst.weight() * k);
            this.ensemble[i].trainOnInstance(weightedInst);
        }
        boolean correctlyClassifies = this.ensemble[i].correctlyClassifies(weightedInst);
        double ErrEstim = this.ADError[i].getEstimation();
        if (this.ADError[i].setInput(correctlyClassifies ? 0 : 1)) {
            if (this.ADError[i].getEstimation() > ErrEstim) {
                Change = true;
            }
        }
    }
    if (Change) {
        numberOfChangesDetected++;
        double max = 0.0;
        int imax = -1;
        for (int i = 0; i < this.ensemble.length; i++) {
            if (max < this.ADError[i].getEstimation()) {
                max = this.ADError[i].getEstimation();
                imax = i;
            }
        }
        if (imax != -1) {
            this.ensemble[imax].resetLearning();
            //this.ensemble[imax].trainOnInstance(inst);
            this.ADError[imax] = new ADWIN((double) this.deltaAdwinOption.getValue());
        }
    }
}

From source file:moa.classifiers.LeveragingBag.java

License:Open Source License

public double[] getVotesForInstanceBinary(Instance inst) {
    double combinedVote[] = new double[(int) inst.numClasses()];
    Instance weightedInst = (Instance) inst.copy();
    if (this.initMatrixCodes == false) {
        for (int i = 0; i < this.ensemble.length; i++) {
            //Replace class by OC
            weightedInst.setClassValue((double) this.matrixCodes[i][(int) inst.classValue()]);

            double vote[];
            vote = this.ensemble[i].getVotesForInstance(weightedInst);
            //Binary Case
            int voteClass = 0;
            if (vote.length == 2) {
                voteClass = (vote[1] > vote[0] ? 1 : 0);
            }/*from   w  w  w  .  j  a  v a 2 s  .  co m*/
            //Update votes
            for (int j = 0; j < inst.numClasses(); j++) {
                if (this.matrixCodes[i][j] == voteClass) {
                    combinedVote[j] += 1;
                }
            }
        }
    }
    return combinedVote;
}

From source file:moa.classifiers.LeveragingBagHalf.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    int numClasses = inst.numClasses();
    //Output Codes
    if (this.initMatrixCodes == true) {
        this.matrixCodes = new int[this.ensemble.length][inst.numClasses()];
        for (int i = 0; i < this.ensemble.length; i++) {
            int numberOnes;
            int numberZeros;

            do { // until we have the same number of zeros and ones
                numberOnes = 0;/* w w w . j a  v a 2  s.com*/
                numberZeros = 0;
                for (int j = 0; j < numClasses; j++) {
                    int result = 0;
                    if (j == 1 && numClasses == 2) {
                        result = 1 - this.matrixCodes[i][0];
                    } else {
                        result = (this.classifierRandom.nextBoolean() ? 1 : 0);
                    }
                    this.matrixCodes[i][j] = result;
                    if (result == 1) {
                        numberOnes++;
                    } else {
                        numberZeros++;
                    }
                }
            } while ((numberOnes - numberZeros) * (numberOnes - numberZeros) > (this.ensemble.length % 2));

        }
        this.initMatrixCodes = false;
    }

    boolean Change = false;
    double w = 1.0;
    double mt = 0.0;
    Instance weightedInst = (Instance) inst.copy();
    //Train ensemble of classifiers
    for (int i = 0; i < this.ensemble.length; i++) {
        int k = this.classifierRandom.nextBoolean() ? 0 : (int) this.weightShrinkOption.getValue(); //half bagging
        if (k > 0) {
            if (this.outputCodesOption.isSet()) {
                weightedInst.setClassValue((double) this.matrixCodes[i][(int) inst.classValue()]);
            }
            weightedInst.setWeight(k);
            this.ensemble[i].trainOnInstance(weightedInst);
        }
        boolean correctlyClassifies = this.ensemble[i].correctlyClassifies(weightedInst);
        double ErrEstim = this.ADError[i].getEstimation();
        if (this.ADError[i].setInput(correctlyClassifies ? 0 : 1)) {
            if (this.ADError[i].getEstimation() > ErrEstim) {
                Change = true;
            }
        }
    }
    if (Change) {
        numberOfChangesDetected++;
        double max = 0.0;
        int imax = -1;
        for (int i = 0; i < this.ensemble.length; i++) {
            if (max < this.ADError[i].getEstimation()) {
                max = this.ADError[i].getEstimation();
                imax = i;
            }
        }
        if (imax != -1) {
            this.ensemble[imax].resetLearning();
            //this.ensemble[imax].trainOnInstance(inst);
            this.ADError[imax] = new ADWIN((double) this.deltaAdwinOption.getValue());
        }
    }
}