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]);
}
}
}
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;
}
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;
}
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;
}
Aggregations