Example usage for weka.core Instance numAttributes

List of usage examples for weka.core Instance numAttributes

Introduction

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

Prototype

public int numAttributes();

Source Link

Document

Returns the number of attributes.

Usage

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

License:Open Source License

public static double[] doNaiveBayesPrediction(Instance inst, DoubleVector observedClassDistribution,
        AutoExpandVector<AttributeClassObserver> attributeObservers) {
    double[] votes = new double[observedClassDistribution.numValues()];
    double observedClassSum = observedClassDistribution.sumOfValues();
    for (int classIndex = 0; classIndex < votes.length; classIndex++) {
        votes[classIndex] = observedClassDistribution.getValue(classIndex) / observedClassSum;
        for (int attIndex = 0; attIndex < inst.numAttributes() - 1; attIndex++) {
            int instAttIndex = modelAttIndexToInstanceAttIndex(attIndex, inst);
            AttributeClassObserver obs = attributeObservers.get(attIndex);
            if ((obs != null) && !inst.isMissing(instAttIndex)) {
                votes[classIndex] *= obs.probabilityOfAttributeValueGivenClass(inst.value(instAttIndex),
                        classIndex);//  w ww.  jav a 2s .co  m
            }
        }
    }
    // TODO: need logic to prevent underflow?
    return votes;
}

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

License:Open Source License

public static double[] doNaiveBayesPredictionLog(Instance inst, DoubleVector observedClassDistribution,
        AutoExpandVector<AttributeClassObserver> observers,
        AutoExpandVector<AttributeClassObserver> observers2) {
    AttributeClassObserver obs;/*from   ww w. j  a  v a 2  s.c  om*/
    double[] votes = new double[observedClassDistribution.numValues()];
    double observedClassSum = observedClassDistribution.sumOfValues();
    for (int classIndex = 0; classIndex < votes.length; classIndex++) {
        votes[classIndex] = Math.log10(observedClassDistribution.getValue(classIndex) / observedClassSum);
        for (int attIndex = 0; attIndex < inst.numAttributes() - 1; attIndex++) {
            int instAttIndex = modelAttIndexToInstanceAttIndex(attIndex, inst);
            if (inst.attribute(instAttIndex).isNominal()) {
                obs = observers.get(attIndex);
            } else {
                obs = observers2.get(attIndex);
            }

            if ((obs != null) && !inst.isMissing(instAttIndex)) {
                votes[classIndex] += Math
                        .log10(obs.probabilityOfAttributeValueGivenClass(inst.value(instAttIndex), classIndex));

            }
        }
    }
    return votes;

}

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

License:Open Source License

/**
 * Trains the classifier with the given instance.
 *
 * @param instance the new training instance to include in the model
 *///from  ww  w  .  ja  v a  2  s. c o m
@Override
public void trainOnInstanceImpl(Instance inst) {
    if (this.reset == true) {
        this.m_numClasses = inst.numClasses();
        double laplace = this.laplaceCorrectionOption.getValue();
        int numAttributes = inst.numAttributes();

        m_probOfClass = new double[m_numClasses];
        Arrays.fill(m_probOfClass, laplace);

        m_classTotals = new double[m_numClasses];
        Arrays.fill(m_classTotals, laplace * numAttributes);

        m_wordTotalForClass = new DoubleVector[m_numClasses];
        for (int i = 0; i < m_numClasses; i++) {
            //Arrays.fill(wordTotal, laplace);
            m_wordTotalForClass[i] = new DoubleVector();
        }
        this.reset = false;
    }
    // Update classifier
    int classIndex = inst.classIndex();
    int classValue = (int) inst.value(classIndex);

    double w = inst.weight();
    m_probOfClass[classValue] += w;

    m_classTotals[classValue] += w * totalSize(inst);
    double total = m_classTotals[classValue];

    for (int i = 0; i < inst.numValues(); i++) {
        int index = inst.index(i);
        if (index != classIndex && !inst.isMissing(i)) {
            //m_wordTotalForClass[index][classValue] += w * inst.valueSparse(i);
            double laplaceCorrection = 0.0;
            if (m_wordTotalForClass[classValue].getValue(index) == 0) {
                laplaceCorrection = this.laplaceCorrectionOption.getValue();
            }
            m_wordTotalForClass[classValue].addToValue(index, w * inst.valueSparse(i) + laplaceCorrection);
        }
    }
}

From source file:moa.classifiers.DecisionStump.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    this.observedClassDistribution.addToValue((int) inst.classValue(), inst.weight());
    for (int i = 0; i < inst.numAttributes() - 1; i++) {
        int instAttIndex = modelAttIndexToInstanceAttIndex(i, inst);
        AttributeClassObserver obs = this.attributeObservers.get(i);
        if (obs == null) {
            obs = inst.attribute(instAttIndex).isNominal() ? newNominalClassObserver()
                    : newNumericClassObserver();
            this.attributeObservers.set(i, obs);
        }//from  ww w  .  jav a2  s  .  c o m
        obs.observeAttributeClass(inst.value(instAttIndex), (int) inst.classValue(), inst.weight());
    }
    if (this.trainingWeightSeenByModel - this.weightSeenAtLastSplit >= this.gracePeriodOption.getValue()) {
        this.bestSplit = findBestSplit((SplitCriterion) getPreparedClassOption(this.splitCriterionOption));
        this.weightSeenAtLastSplit = this.trainingWeightSeenByModel;
    }
}

From source file:moa.classifiers.featureselection.OFSP.java

License:Open Source License

@Override
public double[] getVotesForInstance(Instance inst) {
    if (this.weights == null)
        return (inst.classAttribute().isNominal()) ? new double[2] : new double[1];

    double[] result = (inst.classAttribute().isNominal()) ? new double[2] : new double[1];
    double f_t = 0;
    int[] indices = new int[this.numSelectOption.getValue()];

    if (this.evalOption.getChosenIndex() == 0) {
        f_t = dot(inst.toDoubleArray(), this.weights);
        f_t += this.bias;
    } else {// www  .ja  va 2 s.  c  o m
        for (int i = 0; i < this.numSelectOption.getValue(); i++)
            indices[i] = this.rand.nextInt(inst.numAttributes());
    }

    if (inst.classAttribute().isNumeric()) {
        result[0] = f_t;
        return result;
    }

    if (f_t <= 0) {
        result[0] = 1;
    } else {
        result[1] = 1;
    }

    return result;
}

From source file:moa.classifiers.featureselection.OFSP.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    double y_t, f_t, denom, m_bias;
    int[] indices = new int[this.numSelectOption.getValue()];
    double[] m_weights;

    if (this.weights == null) {

        this.weights = new double[inst.numValues()];
        for (int i = 0; i < this.weights.length; i++)
            this.weights[i] = this.rand.nextGaussian();
        this.bias = 0.0;
        this.weights = truncate(this.weights, this.numSelectOption.getValue());
    }//  ww  w.  ja  va 2s  .co m

    if (inst.classAttribute().isNominal()) {
        y_t = (inst.classValue() == 0) ? -1 : 1;
    } else {
        y_t = inst.classValue();
    }
    double[] x_t = inst.toDoubleArray();
    double[] x_hat = inst.toDoubleArray();

    if (this.rand.nextDouble() < this.searchOption.getValue()) {
        int[] indices_perm = perm(inst.numAttributes());
        for (int i = 0; i < this.numSelectOption.getValue(); i++)
            indices[i] = indices_perm[i];

    } else {
        int[] sorted_indices = bubblesort_index(abs_vector(this.weights));

        for (int i = 0; i < inst.numAttributes() - this.numSelectOption.getValue(); i++)
            x_hat[sorted_indices[i]] = 0.0;

        for (int i = 0; i < this.numSelectOption.getValue(); i++)
            indices[i] = sorted_indices[sorted_indices.length - i - 1];
    }

    f_t = 0;
    for (int i = 0; i < this.numSelectOption.getValue(); i++)
        f_t += this.weights[indices[i]] * x_t[indices[i]];
    f_t += this.bias;

    if (f_t * y_t < 0) {

        for (int i = 0; i < x_hat.length; i++) {
            denom = this.numSelectOption.getValue() / x_hat.length * this.searchOption.getValue();
            if (this.weights[i] != 0)
                denom += (1 - this.searchOption.getValue()) * this.weights[i];
            x_hat[i] /= denom;
        }

        m_weights = scalar_vector(y_t * this.stepSizeOption.getValue(), x_hat);
        m_bias = y_t * this.stepSizeOption.getValue() * this.bias;
        m_weights = vector_add(m_weights, this.weights);
        m_bias += m_bias + this.bias;

        m_weights = l2_projection(m_weights, m_bias, this.boundOption.getValue());
        m_weights = truncate(m_weights, this.numSelectOption.getValue());

        for (int i = 0; i < m_weights.length - 1; i++)
            this.weights[i] = m_weights[i];
        this.bias = m_weights[m_weights.length - 1];
    }

}

From source file:moa.classifiers.functions.Perceptron.java

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {

    //Init Perceptron
    if (this.reset == true) {
        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 * this.classifierRandom.nextDouble() - 0.1;
            }/*from  w w  w.ja v a  2 s. com*/
        }
    }

    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:moa.classifiers.functions.Perceptron.java

License:Open Source License

public double prediction(Instance inst, int classVal) {
    double sum = 0.0;
    for (int i = 0; i < inst.numAttributes() - 1; i++) {
        sum += weightAttribute[classVal][i] * inst.value(i);
    }/*from ww  w . j av a 2  s  .c  o m*/
    sum += weightAttribute[classVal][inst.numAttributes() - 1];
    return 1.0 / (1.0 + Math.exp(-sum));
}

From source file:moa.classifiers.functions.SGDOld.java

License:Open Source License

/**
 * Trains the classifier with the given instance.
 *
 * @param instance    the new training instance to include in the model
 */// w w  w .  ja v a  2 s  .  c om
@Override
public void trainOnInstanceImpl(Instance instance) {

    if (m_weights == null) {
        m_weights = new double[instance.numAttributes() + 1];
    }

    if (!instance.classIsMissing()) {

        double wx = dotProd(instance, m_weights, instance.classIndex());

        double y;
        double z;
        if (instance.classAttribute().isNominal()) {
            y = (instance.classValue() == 0) ? -1 : 1;
            z = y * (wx + m_weights[m_weights.length - 1]);
        } else {
            y = instance.classValue();
            z = y - (wx + m_weights[m_weights.length - 1]);
            y = 1;
        }

        // Compute multiplier for weight decay
        double multiplier = 1.0;
        if (m_numInstances == 0) {
            multiplier = 1.0 - (m_learningRate * m_lambda) / m_t;
        } else {
            multiplier = 1.0 - (m_learningRate * m_lambda) / m_numInstances;
        }
        for (int i = 0; i < m_weights.length - 1; i++) {
            m_weights[i] *= multiplier;
        }

        // Only need to do the following if the loss is non-zero
        if (m_loss != HINGE || (z < 1)) {

            // Compute Factor for updates
            double factor = m_learningRate * y * dloss(z);

            // Update coefficients for attributes
            int n1 = instance.numValues();
            for (int p1 = 0; p1 < n1; p1++) {
                int indS = instance.index(p1);
                if (indS != instance.classIndex() && !instance.isMissingSparse(p1)) {
                    m_weights[indS] += factor * instance.valueSparse(p1);
                }
            }

            // update the bias
            m_weights[m_weights.length - 1] += factor;
        }
        m_t++;
    }
}

From source file:moa.classifiers.functions.SGDOld.java

License:Open Source License

/**
 * Calculates the class membership probabilities for the given test
 * instance.//from w w w .  java  2  s .com
 *
 * @param instance    the instance to be classified
 * @return       predicted class probability distribution
 */
@Override
public double[] getVotesForInstance(Instance inst) {

    if (m_weights == null) {
        return new double[inst.numAttributes() + 1];
    }
    double[] result = (inst.classAttribute().isNominal()) ? new double[2] : new double[1];

    double wx = dotProd(inst, m_weights, inst.classIndex());// * m_wScale;
    double z = (wx + m_weights[m_weights.length - 1]);

    if (inst.classAttribute().isNumeric()) {
        result[0] = z;
        return result;
    }

    if (z <= 0) {
        //  z = 0;
        if (m_loss == LOGLOSS) {
            result[0] = 1.0 / (1.0 + Math.exp(z));
            result[1] = 1.0 - result[0];
        } else {
            result[0] = 1;
        }
    } else {
        if (m_loss == LOGLOSS) {
            result[1] = 1.0 / (1.0 + Math.exp(-z));
            result[0] = 1.0 - result[1];
        } else {
            result[1] = 1;
        }
    }
    return result;
}