Example usage for weka.core Instance toDoubleArray

List of usage examples for weka.core Instance toDoubleArray

Introduction

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

Prototype

public double[] toDoubleArray();

Source Link

Document

Returns the values of each attribute as an array of doubles.

Usage

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

License:Open Source License

@Override
public void trainOnInstanceImpl(Instance inst) {
    double y_t, m_bias_p1, m_bias_p2, m_bias;
    double[] m_weights_p1, m_weights_p2, m_weights;

    if (this.weights == null) {
        this.weights = new double[inst.numValues()];
        for (int i = 0; i < this.weights.length; i++)
            this.weights[i] = 0.0;
        this.bias = 0.0;
    }//from   w  ww  .j  a v  a2 s .  c o  m
    if (inst.classAttribute().isNominal()) {
        y_t = (inst.classValue() == 0) ? -1 : 1;
    } else {
        y_t = inst.classValue();
    }

    double f_t = dot(inst.toDoubleArray(), this.weights);
    f_t += this.bias;

    if (y_t * f_t < 0) {
        m_weights_p1 = scalar_vector(1.0 - this.stepSizeOption.getValue() * this.learningRateOption.getValue(),
                this.weights);
        m_bias_p1 = (1.0 - this.stepSizeOption.getValue() * this.learningRateOption.getValue()) * this.bias;
        m_weights_p2 = scalar_vector(this.learningRateOption.getValue() * y_t, inst.toDoubleArray());
        m_bias_p2 = this.learningRateOption.getValue() * y_t;
        m_weights = vector_add(m_weights_p1, m_weights_p2);
        m_bias = m_bias_p1 + m_bias_p2;

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

        for (int i = 0; i < m_weights_p1.length; i++)
            this.weights[i] = m_weights[i];
        this.bias = m_weights[m_weights.length - 1];
    } else {
        this.weights = scalar_vector(1.0 - this.stepSizeOption.getValue() * this.learningRateOption.getValue(),
                this.weights);
        this.bias = (1.0 - this.stepSizeOption.getValue() * this.learningRateOption.getValue()) * this.bias;
    }

}

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 {//from w  w  w  .  ja v a2s  .  c  om
        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());
    }/*from   ww w.j  a v a 2  s  . c  om*/

    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.macros.TACNB.java

License:Open Source License

public Instance extendWithOldLabels(Instance instance) {
    if (this.header == null) {
        initHeader(instance.dataset());//from  w  ww . ja  v  a  2s . com
    }
    int numLabels = this.oldLabels.length;
    if (numLabels == 0) {
        return instance;
    }
    double[] x = instance.toDoubleArray();
    double[] x2 = Arrays.copyOfRange(this.oldLabels, 0, numLabels + x.length);
    System.arraycopy(x, 0, x2, numLabels, x.length);
    Instance extendedInstance = new DenseInstance(instance.weight(), x2);
    extendedInstance.setDataset(this.header);
    //System.out.println( extendedInstance);
    return extendedInstance;
}

From source file:moa.cluster.CFCluster.java

License:Apache License

/**
 * Instantiates an empty kernel with the given dimensionality.
 * @param dimensions The number of dimensions of the points that can be in
 * this kernel./*from   ww w .  j av a 2s .c  o m*/
 */
public CFCluster(Instance instance, int dimensions) {
    this(instance.toDoubleArray(), dimensions);
}

From source file:moa.cluster.Riffle.java

License:Apache License

/**
 * Create a new cluster from an exemplar data point
 * @param x /*from  w w w . j a  v  a 2 s  . c o m*/
 */
public Riffle(Instance x) {
    safeInit(x);
    this.numLabeledPoints = (int) Math.ceil(x.weight());
    this.labelFrequencies[(int) x.classValue()] += x.weight();
    this.gtLabelFrequencies[(int) x.classValue()]++;
    for (int i = 0; (i < this.symbolFrequencies.length) && (i < x.numAttributes()); ++i) {
        double value = x.value(i);
        if (this.symbolFrequencies[i] == null) {
            if ((this.parentClusterer != null) && (this.parentClusterer.getUniverse() != null)) {
                this.variances[i] = this.parentClusterer.getUniverse().variances[i];
            } else {
                this.variances[i] = this.initialStandardDeviationOption.getValue();
            }
        } else {
            this.variances[i] = 1;
            this.symbolFrequencies[i][(int) value]++;
        }
    }
    this.numTotalPoints = 1;
    this.setGroundTruth(x.classValue());
    this.setCenter(x.toDoubleArray());
    this.setWeight(x.weight());
    this.setRadius(this.initialStandardDeviationOption.getValue());
    this.runningSumOfSquares = 0.0;
    this.setId(autoindex.getAndIncrement());
}

From source file:moa.cluster.Riffle.java

License:Apache License

/**
 * //from  ww w.  j  a va  2s  .  c o  m
 * @param x data instance in question for comparison to this distribution.
 * @return probability that point x is a member of this cluster
 */
@Override
public final double getInclusionProbability(Instance x) {
    int labelIndex = x.classIndex();
    double[] values = x.toDoubleArray();
    return getInclusionProbability(values, labelIndex);
}

From source file:moa.cluster.Riffle.java

License:Apache License

/**
 * Add instance to this cluster/*  w  w  w  .ja  v  a 2s.  c o  m*/
 * Note that caller function takes care of object recompute() call. We just need to update the specific changes here
 * @param x
 */
final protected void addInstanceGrimson(Instance x) {
    double newPoint[] = x.toDoubleArray();
    double newVariances[] = new double[centroid.length];
    double alpha = this.alphaAdjustmentWeightOption.getValue();
    double deviation;
    double p;
    for (int i = 0; i < centroid.length; ++i) {
        p = getComponentProbability(newPoint[i], i);
        if (this.symbolFrequencies[i] == null) { // update numeric attribute tracking
            rho[i] = p * alpha;
            centroid[i] = (1.0 - rho[i]) * centroid[i] + rho[i] * newPoint[i];
            deviation = newPoint[i] - centroid[i];
            newVariances[i] = (1.0 - rho[i]) * this.variances[i] + rho[i] * deviation * deviation;
        } else { // update nominal attribute tracking
            rho[i] = (this.entropies[i] <= 0) ? alpha : p * alpha;
            for (int j = 0; j < this.symbolFrequencies[i].length; ++j) {
                this.symbolFrequencies[i][j] *= (1.0 - rho[i]);
            }
            this.symbolFrequencies[i][(int) newPoint[i]] += rho[i];
            centroid[i] = weka.core.Utils.maxIndex(this.symbolFrequencies[i]);
            newVariances[i] = 1;
        }
    }
    this.setCenter(centroid);
    this.variances = newVariances;
}

From source file:moa.cluster.Riffle.java

License:Apache License

/**
 * Inverse process of adding instance//from   w  w  w  .  j  a  v a2  s.  c o m
 * Note that caller function takes care of object recompute() call. We just need to update the specific changes here
 * @param x
 */
final protected void removeInstanceGrimson(Instance x) {
    double newPoint[] = x.toDoubleArray();
    double alpha = this.alphaAdjustmentWeightOption.getValue();
    double deviation;
    double p;
    for (int i = 0; i < centroid.length; ++i) {
        p = getComponentProbability(newPoint[i], i);
        if (this.symbolFrequencies[i] == null) { // update numeric attribute tracking
            rho[i] = p * alpha;
            deviation = newPoint[i] - centroid[i];
            if (rho[i] != 1 && newPoint[i] != 0) {
                centroid[i] = centroid[i] / (1.0 - rho[i]) - rho[i] / newPoint[i];
            } // inverse operation from adding
            if (deviation > 0) {
                this.variances[i] = this.variances[i] / (1.0 - rho[i]) - rho[i] / (deviation * deviation);
            } // inverse operation from adding
        } else { // update nominal attribute tracking
            rho[i] = (this.entropies[i] <= 0) ? alpha : p * alpha;
            double symFreq[] = this.symbolFrequencies[i];
            for (int j = 0; j < symFreq.length; ++j) {
                this.symbolFrequencies[i][j] /= (1.0 - rho[i]);
            }
            this.symbolFrequencies[i][(int) newPoint[i]] -= rho[i];
            centroid[i] = weka.core.Utils.maxIndex(symFreq);
        }
    }
    this.setCenter(centroid);
}

From source file:moa.cluster.Riffle.java

License:Apache License

/**
 * Add instance to this cluster Computes cluster statistics using an adaptation of Shepherd's scalar on-line method
 * Note that caller function takes care of object recompute() call. We just need to update the specific changes here
 * @param x//w w w  . j  a  v a2 s .  c  o  m
 */
final protected void addInstanceViaShephard(Instance x) {
    // multi-dimensional extension to Data Analysis 4th Ed Ch. 2 (Shepherd)
    assert (numTotalPoints > 1) : " Too few points to compute metrics";
    double newPoint[] = x.toDoubleArray();
    for (int i = 0; i < centroid.length; ++i) {
        if (this.symbolFrequencies[i] == null) {
            double d = newPoint[i] - centroid[i];
            centroid[i] = centroid[i] + d / (this.numTotalPoints); // 1 / (n + 1) implicit since we already did n++ in parent function
            rho[i] = rho[i] + d * (newPoint[i] - centroid[i]);
            this.variances[i] = rho[i] / (this.numTotalPoints - 1);
        } else {
            int newVal = (int) newPoint[i];
            if (newVal < this.symbolFrequencies[i].length) {
                this.symbolFrequencies[i][(int) newPoint[i]]++;
            }
            centroid[i] = weka.core.Utils.maxIndex(symbolFrequencies[i]);
        }
    }
    this.setCenter(centroid);
}