Example usage for weka.core Instance weight

List of usage examples for weka.core Instance weight

Introduction

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

Prototype

public double weight();

Source Link

Document

Returns the instance's weight.

Usage

From source file:GClass.EvaluationInternal.java

License:Open Source License

/**
 * Updates all the statistics about a classifiers performance for
 * the current test instance./*from ww w .  java2 s. co m*/
 *
 * @param predictedDistribution the probabilities assigned to
 * each class
 * @param instance the instance to be classified
 * @exception Exception if the class of the instance is not
 * set
 */
protected void updateStatsForClassifier(double[] predictedDistribution, Instance instance) throws Exception {

    int actualClass = (int) instance.classValue();
    double costFactor = 1;

    if (!instance.classIsMissing()) {
        updateMargins(predictedDistribution, actualClass, instance.weight());

        // Determine the predicted class (doesn't detect multiple
        // classifications)
        int predictedClass = -1;
        double bestProb = 0.0;
        for (int i = 0; i < m_NumClasses; i++) {
            if (predictedDistribution[i] > bestProb) {
                predictedClass = i;
                bestProb = predictedDistribution[i];
            }
        }

        m_WithClass += instance.weight();

        // Determine misclassification cost
        if (m_CostMatrix != null) {
            if (predictedClass < 0) {
                // For missing predictions, we assume the worst possible cost.
                // This is pretty harsh.
                // Perhaps we could take the negative of the cost of a correct
                // prediction (-m_CostMatrix.getElement(actualClass,actualClass)),
                // although often this will be zero
                m_TotalCost += instance.weight() * m_CostMatrix.getMaxCost(actualClass);
            } else {
                m_TotalCost += instance.weight() * m_CostMatrix.getElement(actualClass, predictedClass);
            }
        }

        // Update counts when no class was predicted
        if (predictedClass < 0) {
            m_Unclassified += instance.weight();
            return;
        }

        double predictedProb = Math.max(MIN_SF_PROB, predictedDistribution[actualClass]);
        double priorProb = Math.max(MIN_SF_PROB, m_ClassPriors[actualClass] / m_ClassPriorsSum);
        if (predictedProb >= priorProb) {
            m_SumKBInfo += (Utils.log2(predictedProb) - Utils.log2(priorProb)) * instance.weight();
        } else {
            m_SumKBInfo -= (Utils.log2(1.0 - predictedProb) - Utils.log2(1.0 - priorProb)) * instance.weight();
        }

        m_SumSchemeEntropy -= Utils.log2(predictedProb) * instance.weight();
        m_SumPriorEntropy -= Utils.log2(priorProb) * instance.weight();

        updateNumericScores(predictedDistribution, makeDistribution(instance.classValue()), instance.weight());

        // Update other stats
        m_ConfusionMatrix[actualClass][predictedClass] += instance.weight();
        if (predictedClass != actualClass) {
            m_Incorrect += instance.weight();
        } else {
            m_Correct += instance.weight();
        }
    } else {
        m_MissingClass += instance.weight();
    }
}

From source file:gyc.OverBoostM1.java

License:Open Source License

/**
 * Sets the weights for the next iteration.
 * // w w  w.ja  v a 2s .com
 * @param training the training instances
 * @param reweight the reweighting factor
 * @throws Exception if something goes wrong
 */
protected void setWeights(Instances training, double reweight) throws Exception {

    double oldSumOfWeights, newSumOfWeights;

    oldSumOfWeights = training.sumOfWeights();
    Enumeration enu = training.enumerateInstances();
    while (enu.hasMoreElements()) {
        Instance instance = (Instance) enu.nextElement();
        if (!Utils.eq(m_Classifiers[m_NumIterationsPerformed].classifyInstance(instance),
                instance.classValue()))
            instance.setWeight(instance.weight() * reweight);
    }

    // Renormalize weights
    newSumOfWeights = training.sumOfWeights();
    enu = training.enumerateInstances();
    while (enu.hasMoreElements()) {
        Instance instance = (Instance) enu.nextElement();
        instance.setWeight(instance.weight() * oldSumOfWeights / newSumOfWeights);
    }
}

From source file:j48.ClassifierSplitModel.java

License:Open Source License

/**
 * Splits the given set of instances into subsets.
 *
 * @exception Exception if something goes wrong
 */// w  ww  .ja v a2s . c  o m
public final Instances[] split(Instances data) throws Exception {

    Instances[] instances = new Instances[m_numSubsets];
    double[] weights;
    double newWeight;
    Instance instance;
    int subset, i, j;

    for (j = 0; j < m_numSubsets; j++)
        instances[j] = new Instances((Instances) data, data.numInstances());
    for (i = 0; i < data.numInstances(); i++) {
        instance = ((Instances) data).instance(i);
        weights = weights(instance);
        subset = whichSubset(instance);
        if (subset > -1)
            instances[subset].add(instance);
        else
            for (j = 0; j < m_numSubsets; j++)
                if (Utils.gr(weights[j], 0)) {
                    newWeight = weights[j] * instance.weight();
                    instances[j].add(instance);
                    instances[j].lastInstance().setWeight(newWeight);
                }
    }
    for (j = 0; j < m_numSubsets; j++)
        instances[j].compactify();

    return instances;
}

From source file:j48.Distribution.java

License:Open Source License

/**
 * Adds given instance to given bag.//from   w ww  .j a  v a 2 s.c  o m
 *
 * @exception Exception if something goes wrong
 */
public final void add(int bagIndex, Instance instance) throws Exception {

    int classIndex;
    double weight;

    classIndex = (int) instance.classValue();
    weight = instance.weight();
    m_perClassPerBag[bagIndex][classIndex] = m_perClassPerBag[bagIndex][classIndex] + weight;
    m_perBag[bagIndex] = m_perBag[bagIndex] + weight;
    m_perClass[classIndex] = m_perClass[classIndex] + weight;
    totaL = totaL + weight;
}

From source file:j48.Distribution.java

License:Open Source License

/**
 * Subtracts given instance from given bag.
 *
 * @exception Exception if something goes wrong
 *///  w  w  w  .  j a  v  a  2s . c o m
public final void sub(int bagIndex, Instance instance) throws Exception {

    int classIndex;
    double weight;

    classIndex = (int) instance.classValue();
    weight = instance.weight();
    m_perClassPerBag[bagIndex][classIndex] = m_perClassPerBag[bagIndex][classIndex] - weight;
    m_perBag[bagIndex] = m_perBag[bagIndex] - weight;
    m_perClass[classIndex] = m_perClass[classIndex] - weight;
    totaL = totaL - weight;
}

From source file:j48.Distribution.java

License:Open Source License

/**
 * Adds all instances with unknown values for given attribute, weighted
 * according to frequency of instances in each bag.
 *
 * @exception Exception if something goes wrong
 *///from w  w w  . jav  a  2  s .  c o m
public final void addInstWithUnknown(Instances source, int attIndex) throws Exception {

    double[] probs;
    double weight, newWeight;
    int classIndex;
    Instance instance;
    int j;

    probs = new double[m_perBag.length];
    for (j = 0; j < m_perBag.length; j++) {
        if (Utils.eq(totaL, 0)) {
            probs[j] = 1.0 / probs.length;
        } else {
            probs[j] = m_perBag[j] / totaL;
        }
    }
    Enumeration enu = source.enumerateInstances();
    while (enu.hasMoreElements()) {
        instance = (Instance) enu.nextElement();
        if (instance.isMissing(attIndex)) {
            classIndex = (int) instance.classValue();
            weight = instance.weight();
            m_perClass[classIndex] = m_perClass[classIndex] + weight;
            totaL = totaL + weight;
            for (j = 0; j < m_perBag.length; j++) {
                newWeight = probs[j] * weight;
                m_perClassPerBag[j][classIndex] = m_perClassPerBag[j][classIndex] + newWeight;
                m_perBag[j] = m_perBag[j] + newWeight;
            }
        }
    }
}

From source file:j48.Distribution.java

License:Open Source License

/**
 * Adds all instances in given range to given bag.
 *
 * @exception Exception if something goes wrong
 *///from  w  w  w . j  a v  a2  s.c  o  m
public final void addRange(int bagIndex, Instances source, int startIndex, int lastPlusOne) throws Exception {

    double sumOfWeights = 0;
    int classIndex;
    Instance instance;
    int i;

    for (i = startIndex; i < lastPlusOne; i++) {
        instance = (Instance) source.instance(i);
        classIndex = (int) instance.classValue();
        sumOfWeights = sumOfWeights + instance.weight();
        m_perClassPerBag[bagIndex][classIndex] += instance.weight();
        m_perClass[classIndex] += instance.weight();
    }
    m_perBag[bagIndex] += sumOfWeights;
    totaL += sumOfWeights;
}

From source file:j48.Distribution.java

License:Open Source License

/**
 * Adds given instance to all bags weighting it according to given weights.
 *
 * @exception Exception if something goes wrong
 *///from  ww w. ja va  2  s  .com
public final void addWeights(Instance instance, double[] weights) throws Exception {

    int classIndex;
    int i;

    classIndex = (int) instance.classValue();
    for (i = 0; i < m_perBag.length; i++) {
        double weight = instance.weight() * weights[i];
        m_perClassPerBag[i][classIndex] = m_perClassPerBag[i][classIndex] + weight;
        m_perBag[i] = m_perBag[i] + weight;
        m_perClass[classIndex] = m_perClass[classIndex] + weight;
        totaL = totaL + weight;
    }
}

From source file:j48.Distribution.java

License:Open Source License

/**
 * Deletes given instance from given bag.
 *
 * @exception Exception if something goes wrong
 *//*from w w  w.j av a  2  s .com*/
public final void del(int bagIndex, Instance instance) throws Exception {

    int classIndex;
    double weight;

    classIndex = (int) instance.classValue();
    weight = instance.weight();
    m_perClassPerBag[bagIndex][classIndex] = m_perClassPerBag[bagIndex][classIndex] - weight;
    m_perBag[bagIndex] = m_perBag[bagIndex] - weight;
    m_perClass[classIndex] = m_perClass[classIndex] - weight;
    totaL = totaL - weight;
}

From source file:j48.Distribution.java

License:Open Source License

/**
 * Deletes all instances in given range from given bag.
 *
 * @exception Exception if something goes wrong
 *//*  w  w w  .  ja v a2 s .  c  o  m*/
public final void delRange(int bagIndex, Instances source, int startIndex, int lastPlusOne) throws Exception {

    double sumOfWeights = 0;
    int classIndex;
    Instance instance;
    int i;

    for (i = startIndex; i < lastPlusOne; i++) {
        instance = (Instance) source.instance(i);
        classIndex = (int) instance.classValue();
        sumOfWeights = sumOfWeights + instance.weight();
        m_perClassPerBag[bagIndex][classIndex] -= instance.weight();
        m_perClass[classIndex] -= instance.weight();
    }
    m_perBag[bagIndex] -= sumOfWeights;
    totaL -= sumOfWeights;
}