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