Example usage for weka.core Instance numClasses

List of usage examples for weka.core Instance numClasses

Introduction

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

Prototype

public int numClasses();

Source Link

Document

Returns the number of class labels.

Usage

From source file:tr.gov.ulakbim.jDenetX.classifiers.OzaBoostAdwin.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    int numClasses = inst.numClasses();
    // Set log (k-1) and (k-1) for SAMME Method
    if (this.sammeOption.isSet()) {
        this.Km1 = numClasses - 1;
        this.logKm1 = Math.log(this.Km1);
        this.initKm1 = false;
    }// w w  w .ja  v a2  s .c o m
    //Output Codes
    if (this.initMatrixCodes) {

        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;
                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 lambda_d = 1.0;
    Instance weightedInst = (Instance) inst.copy();
    for (int i = 0; i < this.ensemble.length; i++) {
        double k = this.pureBoostOption.isSet() ? lambda_d
                : MiscUtils.poisson(lambda_d * this.Km1, this.classifierRandom);
        if (k > 0.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);
        if (correctlyClassifies) {
            this.scms[i] += lambda_d;
            lambda_d *= this.trainingWeightSeenByModel / (2 * this.scms[i]);
        } else {
            this.swms[i] += lambda_d;
            lambda_d *= this.trainingWeightSeenByModel / (2 * this.swms[i]);
        }

        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());
            this.scms[imax] = 0;
            this.swms[imax] = 0;
        }
    }
}

From source file:tr.gov.ulakbim.jDenetX.classifiers.OzaBoostAdwin.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) {
        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 ww . j a v  a 2  s . c om*/
            //Update votes
            for (int j = 0; j < inst.numClasses(); j++) {
                if (this.matrixCodes[i][j] == voteClass) {
                    combinedVote[j] += getEnsembleMemberWeight(i);
                }
            }
        }
    }
    return combinedVote;
}

From source file:tr.gov.ulakbim.jDenetX.classifiers.Perceptron.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {

    //Init Perceptron
    if (this.reset) {
        this.reset = false;
        this.numberAttributes = inst.numAttributes();
        this.numberClasses = inst.numClasses();
        this.weightAttribute = new double[inst.numClasses()][inst.numAttributes()];
        for (int i = 0; i < inst.numClasses(); i++) {
            for (int j = 0; j < inst.numAttributes(); j++) {
                weightAttribute[i][j] = 0.2 * Math.random() - 0.1;
            }/*  w ww  .j  av  a  2  s  .c o  m*/
        }
    }

    double[] preds = new double[inst.numClasses()];
    for (int i = 0; i < inst.numClasses(); i++) {
        preds[i] = prediction(inst, i);
    }
    double learningRatio = learningRatioOption.getValue();

    int actualClass = (int) inst.classValue();
    for (int i = 0; i < inst.numClasses(); i++) {
        double actual = (i == actualClass) ? 1.0 : 0.0;
        double delta = (actual - preds[i]) * preds[i] * (1 - preds[i]);
        for (int j = 0; j < inst.numAttributes() - 1; j++) {
            this.weightAttribute[i][j] += learningRatio * delta * inst.value(j);
        }
        this.weightAttribute[i][inst.numAttributes() - 1] += learningRatio * delta;
    }
}

From source file:tr.gov.ulakbim.jDenetX.classifiers.Perceptron.java

License:Open Source License

@Override
public double[] getVotesForInstance(Instance inst) {
    double[] votes = new double[inst.numClasses()];
    if (!this.reset) {
        for (int i = 0; i < votes.length; i++) {
            votes[i] = prediction(inst, i);
        }/*from ww  w.  jav  a2  s  .c  o  m*/
        try {
            weka.core.Utils.normalize(votes);
        } catch (Exception e) {
            // ignore all zero votes error
        }
    }
    return votes;
}

From source file:xlong.urlclassify.others.LibLINEAR.java

License:Open Source License

/**
 * Computes the distribution for a given instance.
 *
 * @param instance       the instance for which distribution is computed
 * @return          the distribution/* www .j a  v a  2  s. c o  m*/
 * @throws Exception       if the distribution can't be computed successfully
 */
public double[] distributionForInstance(Instance instance) throws Exception {

    if (!getDoNotReplaceMissingValues()) {
        m_ReplaceMissingValues.input(instance);
        m_ReplaceMissingValues.batchFinished();
        instance = m_ReplaceMissingValues.output();
    }

    if (getConvertNominalToBinary() && m_NominalToBinary != null) {
        m_NominalToBinary.input(instance);
        m_NominalToBinary.batchFinished();
        instance = m_NominalToBinary.output();
    }

    if (m_Filter != null) {
        m_Filter.input(instance);
        m_Filter.batchFinished();
        instance = m_Filter.output();
    }

    FeatureNode[] x = instanceToArray(instance);
    double[] result = new double[instance.numClasses()];
    if (m_ProbabilityEstimates) {
        if (m_SolverType != SolverType.L2R_LR && m_SolverType != SolverType.L2R_LR_DUAL
                && m_SolverType != SolverType.L1R_LR) {
            throw new WekaException("probability estimation is currently only "
                    + "supported for L2-regularized logistic regression");
        }

        int[] labels = m_Model.getLabels();
        double[] prob_estimates = new double[instance.numClasses()];

        Linear.predictProbability(m_Model, x, prob_estimates);

        // Return order of probabilities to canonical weka attribute order
        for (int k = 0; k < labels.length; k++) {
            result[labels[k]] = prob_estimates[k];
        }
    } else {
        int prediction = Linear.predict(m_Model, x);
        assert (instance.classAttribute().isNominal());
        result[prediction] = 1;
    }

    return result;
}