Search in sources :

Example 41 with DenseVector

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

the class ExpectedRecallFeatureExtractor method extractFeatures.

@Override
public Vector extractFeatures(PredictionCandidate predictionCandidate) {
    MultiLabel prediction = predictionCandidate.multiLabel;
    double expectation = 0;
    List<Pair<MultiLabel, Double>> sparseJoint = predictionCandidate.sparseJoint;
    for (Pair<MultiLabel, Double> pair : sparseJoint) {
        MultiLabel multiLabel = pair.getFirst();
        double prob = pair.getSecond();
        expectation += Recall.recall(multiLabel, prediction) * prob;
    }
    Vector vector = new DenseVector(1);
    vector.set(0, expectation);
    return vector;
}
Also used : MultiLabel(edu.neu.ccs.pyramid.dataset.MultiLabel) DenseVector(org.apache.mahout.math.DenseVector) Vector(org.apache.mahout.math.Vector) DenseVector(org.apache.mahout.math.DenseVector) Pair(edu.neu.ccs.pyramid.util.Pair)

Example 42 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)

Example 43 with DenseVector

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

the class InstanceFeatureExtractor method extractFeatures.

@Override
public Vector extractFeatures(PredictionCandidate predictionCandidate) {
    int size = featureIndices.size();
    Vector vector = new DenseVector(size);
    for (int j = 0; j < size; j++) {
        int index = featureIndices.get(j);
        vector.set(j, predictionCandidate.x.get(index));
    }
    return vector;
}
Also used : DenseVector(org.apache.mahout.math.DenseVector) Vector(org.apache.mahout.math.Vector) DenseVector(org.apache.mahout.math.DenseVector)

Example 44 with DenseVector

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

the class PriorFeatureExtractor method extractFeatures.

@Override
public Vector extractFeatures(PredictionCandidate prediction) {
    Vector vector = new DenseVector(1);
    double prior = priors.getOrDefault(prediction.multiLabel, 0.0);
    vector.set(0, prior);
    return vector;
}
Also used : DenseVector(org.apache.mahout.math.DenseVector) Vector(org.apache.mahout.math.Vector) DenseVector(org.apache.mahout.math.DenseVector)

Example 45 with DenseVector

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

the class ExpectedF1FeatureExtractor method extractFeatures.

@Override
public Vector extractFeatures(PredictionCandidate predictionCandidate) {
    MultiLabel prediction = predictionCandidate.multiLabel;
    double expectation = 0;
    List<Pair<MultiLabel, Double>> sparseJoint = predictionCandidate.sparseJoint;
    for (Pair<MultiLabel, Double> pair : sparseJoint) {
        MultiLabel multiLabel = pair.getFirst();
        double prob = pair.getSecond();
        expectation += FMeasure.f1(prediction, multiLabel) * prob;
    }
    Vector vector = new DenseVector(1);
    vector.set(0, expectation);
    return vector;
}
Also used : MultiLabel(edu.neu.ccs.pyramid.dataset.MultiLabel) DenseVector(org.apache.mahout.math.DenseVector) Vector(org.apache.mahout.math.Vector) DenseVector(org.apache.mahout.math.DenseVector) Pair(edu.neu.ccs.pyramid.util.Pair)

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