Search in sources :

Example 76 with DenseVector

use of org.apache.mahout.math.DenseVector in project pyramid by cheng-li.

the class Vectors method concatenate.

public static Vector concatenate(Vector vector, double number) {
    Vector con = null;
    if (vector instanceof DenseVector) {
        con = new DenseVector(vector.size() + 1);
    }
    if (vector instanceof RandomAccessSparseVector) {
        con = new RandomAccessSparseVector(vector.size() + 1);
    }
    if (vector instanceof SequentialAccessSparseVector) {
        con = new SequentialAccessSparseVector(vector.size() + 1);
    }
    for (Vector.Element nonZeros : vector.nonZeroes()) {
        int index = nonZeros.index();
        double value = nonZeros.get();
        con.set(index, value);
    }
    con.set(con.size() - 1, number);
    return con;
}
Also used : RandomAccessSparseVector(org.apache.mahout.math.RandomAccessSparseVector) DenseVector(org.apache.mahout.math.DenseVector) RandomAccessSparseVector(org.apache.mahout.math.RandomAccessSparseVector) SequentialAccessSparseVector(org.apache.mahout.math.SequentialAccessSparseVector) Vector(org.apache.mahout.math.Vector) DenseVector(org.apache.mahout.math.DenseVector) SequentialAccessSparseVector(org.apache.mahout.math.SequentialAccessSparseVector)

Example 77 with DenseVector

use of org.apache.mahout.math.DenseVector in project pyramid by cheng-li.

the class LogisticRegressionTest method test4.

private static void test4() {
    double[] prior = { 0.3, 0.7 };
    LogisticRegression logisticRegression = new LogisticRegression(2, 10, prior);
    Vector vector = new DenseVector(10);
    for (int d = 0; d < 10; d++) {
        vector.set(d, Math.random());
    }
    System.out.println(Arrays.toString(logisticRegression.predictClassProbs(vector)));
}
Also used : DenseVector(org.apache.mahout.math.DenseVector) Vector(org.apache.mahout.math.Vector) DenseVector(org.apache.mahout.math.DenseVector)

Example 78 with DenseVector

use of org.apache.mahout.math.DenseVector in project pyramid by cheng-li.

the class RegressionSynthesizer method linear.

public static RegDataSet linear() {
    int numData = 50;
    RegDataSet dataSet = RegDataSetBuilder.getBuilder().numDataPoints(numData).numFeatures(16000).dense(true).missingValue(false).build();
    Vector weights = new DenseVector(16000);
    weights.set(0, 0.001);
    weights.set(1, 0.001);
    weights.set(2, 0.001);
    weights.set(3, 0.001);
    for (int i = 0; i < numData; i++) {
        for (int j = 0; j < 16000; j++) {
            BernoulliDistribution bernoulliDistribution = new BernoulliDistribution(0.5);
            int sample = bernoulliDistribution.sample();
            if (sample == 0) {
                dataSet.setFeatureValue(i, j, -1);
            } else {
                dataSet.setFeatureValue(i, j, 1);
            }
        }
        double label = weights.dot(dataSet.getRow(i));
        dataSet.setLabel(i, label);
    }
    return dataSet;
}
Also used : BernoulliDistribution(edu.neu.ccs.pyramid.util.BernoulliDistribution) RegDataSet(edu.neu.ccs.pyramid.dataset.RegDataSet) DenseVector(org.apache.mahout.math.DenseVector) Vector(org.apache.mahout.math.Vector) DenseVector(org.apache.mahout.math.DenseVector)

Example 79 with DenseVector

use of org.apache.mahout.math.DenseVector in project elephant-bird by twitter.

the class VectorWritableConverter method convertDenseVectorDataToVector.

private Vector convertDenseVectorDataToVector(Tuple value) throws IOException {
    Vector v;
    // determine output vector size
    int size = value.size();
    int minSize = size;
    if (cardinality != null && cardinality != size) {
        // cardinality specified on construction overrides instance cardinality
        size = cardinality;
        if (minSize > size) {
            minSize = size;
        }
    }
    // allow conversion of dense vector data to sparse vector
    if (sparse) {
        // this ctor used to pre-alloc space for entries
        v = new RandomAccessSparseVector(size, size);
        for (int i = 0; i < minSize; ++i) {
            v.setQuick(i, ((Number) value.get(i)).doubleValue());
        }
    } else {
        double[] values = new double[size];
        for (int i = 0; i < minSize; ++i) {
            values[i] = ((Number) value.get(i)).doubleValue();
        }
        // this ctor uses values directly, no copying performed
        v = new DenseVector(values, true);
    }
    return v;
}
Also used : RandomAccessSparseVector(org.apache.mahout.math.RandomAccessSparseVector) SequentialAccessSparseVector(org.apache.mahout.math.SequentialAccessSparseVector) DenseVector(org.apache.mahout.math.DenseVector) RandomAccessSparseVector(org.apache.mahout.math.RandomAccessSparseVector) Vector(org.apache.mahout.math.Vector) DenseVector(org.apache.mahout.math.DenseVector)

Aggregations

DenseVector (org.apache.mahout.math.DenseVector)79 Vector (org.apache.mahout.math.Vector)73 MultiLabel (edu.neu.ccs.pyramid.dataset.MultiLabel)9 RandomAccessSparseVector (org.apache.mahout.math.RandomAccessSparseVector)8 MultiLabelClfDataSet (edu.neu.ccs.pyramid.dataset.MultiLabelClfDataSet)7 SequentialAccessSparseVector (org.apache.mahout.math.SequentialAccessSparseVector)6 Pair (edu.neu.ccs.pyramid.util.Pair)4 List (java.util.List)3 IntStream (java.util.stream.IntStream)3 EnumeratedIntegerDistribution (org.apache.commons.math3.distribution.EnumeratedIntegerDistribution)3 LogisticRegression (edu.neu.ccs.pyramid.classification.logistic_regression.LogisticRegression)2 DataSet (edu.neu.ccs.pyramid.dataset.DataSet)2 EmpiricalCDF (edu.neu.ccs.pyramid.util.EmpiricalCDF)2 IntegerDistribution (org.apache.commons.math3.distribution.IntegerDistribution)2 MultivariateNormalDistribution (org.apache.commons.math3.distribution.MultivariateNormalDistribution)2 Classifier (edu.neu.ccs.pyramid.classification.Classifier)1 Weights (edu.neu.ccs.pyramid.classification.logistic_regression.Weights)1 RegDataSet (edu.neu.ccs.pyramid.dataset.RegDataSet)1 ConstantRegressor (edu.neu.ccs.pyramid.regression.ConstantRegressor)1 BernoulliDistribution (edu.neu.ccs.pyramid.util.BernoulliDistribution)1