Search in sources :

Example 6 with RandomAccessSparseVector

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

the class SerializableVector method readObject.

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    if (type == Type.DENSE) {
        double[] values = (double[]) in.readObject();
        vector = new DenseVector(values);
    } else if (type == Type.SPARSE_RANDOM) {
        int[] indices = (int[]) in.readObject();
        double[] values = (double[]) in.readObject();
        vector = new RandomAccessSparseVector(size);
        for (int i = 0; i < indices.length; i++) {
            vector.set(indices[i], values[i]);
        }
    } else if (type == Type.SPARSE_SEQUENTIAL) {
        int[] indices = (int[]) in.readObject();
        double[] values = (double[]) in.readObject();
        vector = new SequentialAccessSparseVector(size);
        for (int i = 0; i < indices.length; i++) {
            vector.set(indices[i], values[i]);
        }
    }
}
Also used : RandomAccessSparseVector(org.apache.mahout.math.RandomAccessSparseVector) DenseVector(org.apache.mahout.math.DenseVector) SequentialAccessSparseVector(org.apache.mahout.math.SequentialAccessSparseVector)

Example 7 with RandomAccessSparseVector

use of org.apache.mahout.math.RandomAccessSparseVector 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 8 with RandomAccessSparseVector

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

the class Vectors method conatenateToSparseRandom.

public static Vector conatenateToSparseRandom(List<Vector> vectors) {
    int size = 0;
    for (Vector vector : vectors) {
        size += vector.size();
    }
    Vector concatenated = new RandomAccessSparseVector(size);
    int offset = 0;
    for (Vector vector : vectors) {
        for (Vector.Element nonZeros : vector.nonZeroes()) {
            int index = nonZeros.index();
            double value = nonZeros.get();
            concatenated.set(index + offset, value);
        }
        offset += vector.size();
    }
    return concatenated;
}
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)

Example 9 with RandomAccessSparseVector

use of org.apache.mahout.math.RandomAccessSparseVector 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)9 RandomAccessSparseVector (org.apache.mahout.math.RandomAccessSparseVector)9 Vector (org.apache.mahout.math.Vector)8 SequentialAccessSparseVector (org.apache.mahout.math.SequentialAccessSparseVector)7 DataBag (org.apache.pig.data.DataBag)1 Tuple (org.apache.pig.data.Tuple)1