Example usage for weka.core Utils logs2probs

List of usage examples for weka.core Utils logs2probs

Introduction

In this page you can find the example usage for weka.core Utils logs2probs.

Prototype

public static double[] logs2probs(double[] a) 

Source Link

Document

Converts an array containing the natural logarithms of probabilities stored in a vector back into probabilities.

Usage

From source file:gyc.OverBoostM1.java

License:Open Source License

/**
 * Calculates the class membership probabilities for the given test instance.
 *
 * @param instance the instance to be classified
 * @return predicted class probability distribution
 * @throws Exception if instance could not be classified
 * successfully/*www .  j  av  a2 s.  c  om*/
 */
public double[] distributionForInstance(Instance instance) throws Exception {

    // default model?
    if (m_ZeroR != null) {
        return m_ZeroR.distributionForInstance(instance);
    }

    if (m_NumIterationsPerformed == 0) {
        throw new Exception("No model built");
    }
    double[] sums = new double[instance.numClasses()];

    if (m_NumIterationsPerformed == 1) {
        return m_Classifiers[0].distributionForInstance(instance);
    } else {
        for (int i = 0; i < m_NumIterationsPerformed; i++) {
            sums[(int) m_Classifiers[i].classifyInstance(instance)] += m_Betas[i];
        }
        return Utils.logs2probs(sums);
    }
}

From source file:moa.classifiers.bayes.NaiveBayesMultinomial.java

License:Open Source License

/**
 * Calculates the class membership probabilities for the given test
 * instance.//  w  w  w.j  av  a  2  s .  c  o m
 *
 * @param instance the instance to be classified
 * @return predicted class probability distribution
 */
@Override
public double[] getVotesForInstance(Instance instance) {
    if (this.reset == true) {
        return new double[2];
    }
    double[] probOfClassGivenDoc = new double[m_numClasses];
    double totalSize = totalSize(instance);

    for (int i = 0; i < m_numClasses; i++) {
        probOfClassGivenDoc[i] = Math.log(m_probOfClass[i]) - totalSize * Math.log(m_classTotals[i]);
    }

    for (int i = 0; i < instance.numValues(); i++) {

        int index = instance.index(i);
        if (index == instance.classIndex() || instance.isMissing(i)) {
            continue;
        }

        double wordCount = instance.valueSparse(i);
        for (int c = 0; c < m_numClasses; c++) {
            double value = m_wordTotalForClass[c].getValue(index);
            probOfClassGivenDoc[c] += wordCount
                    * Math.log(value == 0 ? this.laplaceCorrectionOption.getValue() : value);
        }
    }

    return Utils.logs2probs(probOfClassGivenDoc);
}

From source file:moa.classifiers.NaiveBayesMultinomial.java

License:Open Source License

/**
 * Calculates the class membership probabilities for the given test
 * instance.//from   w w w. j av  a 2  s  .  co m
 *
 * @param instance    the instance to be classified
 * @return       predicted class probability distribution
 */
@Override
public double[] getVotesForInstance(Instance instance) {
    if (this.reset == true) {
        return new double[2];
    }
    double[] probOfClassGivenDoc = new double[m_numClasses];
    double totalSize = totalSize(instance);

    for (int i = 0; i < m_numClasses; i++) {
        probOfClassGivenDoc[i] = Math.log(m_probOfClass[i]) - totalSize * Math.log(m_classTotals[i]);
    }

    for (int i = 0; i < instance.numValues(); i++) {

        int index = instance.index(i);
        if (index == instance.classIndex() || instance.isMissing(i)) {
            continue;
        }

        double wordCount = instance.valueSparse(i);
        for (int c = 0; c < m_numClasses; c++) {
            probOfClassGivenDoc[c] += wordCount * Math.log(m_wordTotalForClass[index][c]);
        }
    }

    return Utils.logs2probs(probOfClassGivenDoc);
}