Example usage for weka.core Instance numValues

List of usage examples for weka.core Instance numValues

Introduction

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

Prototype

public int numValues();

Source Link

Document

Returns the number of values present in a sparse representation.

Usage

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

License:Open Source License

/**
 * Trains the classifier with the given instance.
 *
 * @param instance    the new training instance to include in the model
 *///  w  ww  .  java2 s  .c  o m
@Override
public void trainOnInstanceImpl(Instance instance) {

    if (m_weights == null) {
        m_weights = new DoubleVector();
        m_bias = 0.0;
    }

    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_bias);
        } else {
            y = instance.classValue();
            z = y - (wx + m_bias);
            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.numValues(); i++) {
            m_weights.setValue(i, m_weights.getValue(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.addToValue(indS, factor * instance.valueSparse(p1));
                }
            }

            // update the bias
            m_bias += factor;
        }
        m_t++;
    }
}

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

License:Open Source License

public void trainOnInstanceImpl(Instance instance, int classLabel) {
    if (!instance.classIsMissing()) {

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

        double y;
        double z;
        if (instance.classAttribute().isNominal()) {
            y = (instance.classValue() != classLabel) ? -1 : 1;
            z = y * (wx + m_bias[classLabel]);
        } else {//w ww  .ja  va  2s. co  m
            y = instance.classValue();
            z = y - (wx + m_bias[classLabel]);
            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[classLabel].numValues(); i++) {
            m_weights[classLabel].setValue(i, m_weights[classLabel].getValue(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[classLabel].addToValue(indS, factor * instance.valueSparse(p1));
                }
            }

            // update the bias
            m_bias[classLabel] += factor;
        }

    }
}

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

License:Open Source License

protected static double dotProd(Instance inst1, double[] weights, int classIndex) {
    double result = 0;

    int n1 = inst1.numValues();
    int n2 = weights.length - 1;

    for (int p1 = 0, p2 = 0; p1 < n1 && p2 < n2;) {
        int ind1 = inst1.index(p1);
        int ind2 = p2;
        if (ind1 == ind2) {
            if (ind1 != classIndex && !inst1.isMissingSparse(p1)) {
                result += inst1.valueSparse(p1) * weights[p2];
            }//  w ww.  j  ava2 s . co  m
            p1++;
            p2++;
        } else if (ind1 > ind2) {
            p2++;
        } else {
            p1++;
        }
    }
    return (result);
}

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
 *///ww  w . j a va2s  .c  o  m
@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.SPegasos.java

License:Open Source License

/**
 * Trains the classifier with the given instance.
 *
 * @param instance the new training instance to include in the model
 *///from w  w w.j av  a  2 s  .co  m
@Override
public void trainOnInstanceImpl(Instance instance) {

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

        double learningRate = 1.0 / (m_lambda * m_t);
        //double scale = 1.0 - learningRate * m_lambda;
        double scale = 1.0 - 1.0 / m_t;
        double y = (instance.classValue() == 0) ? -1 : 1;
        double wx = dotProd(instance, m_weights, instance.classIndex());
        double z = y * (wx + m_weights[m_weights.length - 1]);

        for (int j = 0; j < m_weights.length - 1; j++) {
            if (j != instance.classIndex()) {
                m_weights[j] *= scale;
            }
        }

        if (m_loss == LOGLOSS || (z < 1)) {
            double loss = dloss(z);
            int n1 = instance.numValues();
            for (int p1 = 0; p1 < n1; p1++) {
                int indS = instance.index(p1);
                if (indS != instance.classIndex() && !instance.isMissingSparse(p1)) {
                    double m = learningRate * loss * (instance.valueSparse(p1) * y);
                    m_weights[indS] += m;
                }
            }

            // update the bias
            m_weights[m_weights.length - 1] += learningRate * loss * y;
        }

        double norm = 0;
        for (int k = 0; k < m_weights.length - 1; k++) {
            if (k != instance.classIndex()) {
                norm += (m_weights[k] * m_weights[k]);
            }
        }

        double scale2 = Math.min(1.0, (1.0 / (m_lambda * norm)));
        if (scale2 < 1.0) {
            scale2 = Math.sqrt(scale2);
            for (int j = 0; j < m_weights.length - 1; j++) {
                if (j != instance.classIndex()) {
                    m_weights[j] *= scale2;
                }
            }
        }
        m_t++;
    }
}

From source file:moa.classifiers.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  .j  a v  a2s .c om
@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 double[numAttributes][m_numClasses];
        for (double[] wordTotal : m_wordTotalForClass) {
            Arrays.fill(wordTotal, laplace);
        }
        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);
        }
    }
}

From source file:moa.classifiers.NaiveBayesMultinomial.java

License:Open Source License

/**
 * Calculates the class membership probabilities for the given test
 * instance.//  www  . j a  v 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++) {
            probOfClassGivenDoc[c] += wordCount * Math.log(m_wordTotalForClass[index][c]);
        }
    }

    return Utils.logs2probs(probOfClassGivenDoc);
}

From source file:moa.clusterers.clustream.Clustream.java

License:Apache License

@Override
public void trainOnInstanceImpl(Instance instance) {
    int dim = instance.numValues();
    timestamp++;/*from   w  w  w  . j  a va  2 s .co  m*/
    // 0. Initialize
    if (!initialized) {
        if (buffer.size() < bufferSize) {
            buffer.add(new ClustreamKernel(instance, dim, timestamp, t, m));
            return;
        }

        int k = kernels.length;
        //System.err.println("k="+k+" bufferSize="+bufferSize);
        assert (k <= bufferSize);

        ClustreamKernel[] centers = new ClustreamKernel[k];
        for (int i = 0; i < k; i++) {
            centers[i] = buffer.get(i); // TODO: make random!
        }
        Clustering kmeans_clustering = kMeans(k, centers, buffer);
        //         Clustering kmeans_clustering = kMeans(k, buffer);

        for (int i = 0; i < kmeans_clustering.size(); i++) {
            kernels[i] = new ClustreamKernel(new DenseInstance(1.0, centers[i].getCenter()), dim, timestamp, t,
                    m);
        }

        buffer.clear();
        initialized = true;
        return;
    }

    // 1. Determine closest kernel
    ClustreamKernel closestKernel = null;
    double minDistance = Double.MAX_VALUE;
    for (int i = 0; i < kernels.length; i++) {
        //System.out.println(i+" "+kernels[i].getWeight()+" "+kernels[i].getDeviation());
        double distance = distance(instance.toDoubleArray(), kernels[i].getCenter());
        if (distance < minDistance) {
            closestKernel = kernels[i];
            minDistance = distance;
        }
    }

    // 2. Check whether instance fits into closestKernel
    double radius = 0.0;
    if (closestKernel.getWeight() == 1) {
        // Special case: estimate radius by determining the distance to the
        // next closest cluster
        radius = Double.MAX_VALUE;
        double[] center = closestKernel.getCenter();
        for (int i = 0; i < kernels.length; i++) {
            if (kernels[i] == closestKernel) {
                continue;
            }

            double distance = distance(kernels[i].getCenter(), center);
            radius = Math.min(distance, radius);
        }
    } else {
        radius = closestKernel.getRadius();
    }

    if (minDistance < radius) {
        // Date fits, put into kernel and be happy
        closestKernel.insert(instance, timestamp);
        return;
    }

    // 3. Date does not fit, we need to free
    // some space to insert a new kernel
    long threshold = timestamp - timeWindow; // Kernels before this can be forgotten

    // 3.1 Try to forget old kernels
    for (int i = 0; i < kernels.length; i++) {
        if (kernels[i].getRelevanceStamp() < threshold) {
            kernels[i] = new ClustreamKernel(instance, dim, timestamp, t, m);
            return;
        }
    }

    // 3.2 Merge closest two kernels
    int closestA = 0;
    int closestB = 0;
    minDistance = Double.MAX_VALUE;
    for (int i = 0; i < kernels.length; i++) {
        double[] centerA = kernels[i].getCenter();
        for (int j = i + 1; j < kernels.length; j++) {
            double dist = distance(centerA, kernels[j].getCenter());
            if (dist < minDistance) {
                minDistance = dist;
                closestA = i;
                closestB = j;
            }
        }
    }
    assert (closestA != closestB);

    kernels[closestA].add(kernels[closestB]);
    kernels[closestB] = new ClustreamKernel(instance, dim, timestamp, t, m);
}

From source file:moa.clusterers.clustream.ClustreamKernel.java

License:Apache License

public void insert(Instance instance, long timestamp) {
    N++;//from   www .  jav a  2s . com
    LST += timestamp;
    SST += timestamp * timestamp;

    for (int i = 0; i < instance.numValues(); i++) {
        LS[i] += instance.value(i);
        SS[i] += instance.value(i) * instance.value(i);
    }
}

From source file:moa.clusterers.clustream.WithKmeans.java

License:Apache License

@Override
public void trainOnInstanceImpl(Instance instance) {
    int dim = instance.numValues();
    timestamp++;/*from  w  ww . ja  va 2 s  . c o m*/
    // 0. Initialize
    if (!initialized) {
        if (buffer.size() < bufferSize) {
            buffer.add(new ClustreamKernel(instance, dim, timestamp, t, m));
            return;
        } else {
            for (int i = 0; i < buffer.size(); i++) {
                kernels[i] = new ClustreamKernel(new DenseInstance(1.0, buffer.get(i).getCenter()), dim,
                        timestamp, t, m);
            }

            buffer.clear();
            initialized = true;
            return;
        }
    }

    // 1. Determine closest kernel
    ClustreamKernel closestKernel = null;
    double minDistance = Double.MAX_VALUE;
    for (int i = 0; i < kernels.length; i++) {
        //System.out.println(i+" "+kernels[i].getWeight()+" "+kernels[i].getDeviation());
        double distance = distance(instance.toDoubleArray(), kernels[i].getCenter());
        if (distance < minDistance) {
            closestKernel = kernels[i];
            minDistance = distance;
        }
    }

    // 2. Check whether instance fits into closestKernel
    double radius = 0.0;
    if (closestKernel.getWeight() == 1) {
        // Special case: estimate radius by determining the distance to the
        // next closest cluster
        radius = Double.MAX_VALUE;
        double[] center = closestKernel.getCenter();
        for (int i = 0; i < kernels.length; i++) {
            if (kernels[i] == closestKernel) {
                continue;
            }

            double distance = distance(kernels[i].getCenter(), center);
            radius = Math.min(distance, radius);
        }
    } else {
        radius = closestKernel.getRadius();
    }

    if (minDistance < radius) {
        // Date fits, put into kernel and be happy
        closestKernel.insert(instance, timestamp);
        return;
    }

    // 3. Date does not fit, we need to free
    // some space to insert a new kernel
    long threshold = timestamp - timeWindow; // Kernels before this can be forgotten

    // 3.1 Try to forget old kernels
    for (int i = 0; i < kernels.length; i++) {
        if (kernels[i].getRelevanceStamp() < threshold) {
            kernels[i] = new ClustreamKernel(instance, dim, timestamp, t, m);
            return;
        }
    }

    // 3.2 Merge closest two kernels
    int closestA = 0;
    int closestB = 0;
    minDistance = Double.MAX_VALUE;
    for (int i = 0; i < kernels.length; i++) {
        double[] centerA = kernels[i].getCenter();
        for (int j = i + 1; j < kernels.length; j++) {
            double dist = distance(centerA, kernels[j].getCenter());
            if (dist < minDistance) {
                minDistance = dist;
                closestA = i;
                closestB = j;
            }
        }
    }
    assert (closestA != closestB);

    kernels[closestA].add(kernels[closestB]);
    kernels[closestB] = new ClustreamKernel(instance, dim, timestamp, t, m);
}