Search in sources :

Example 6 with EnumeratedIntegerDistribution

use of org.apache.commons.math3.distribution.EnumeratedIntegerDistribution in project pyramid by cheng-li.

the class BMDistribution method sample.

List<MultiLabel> sample(int numSamples) {
    List<MultiLabel> list = new ArrayList<>();
    double[] proportions = Arrays.stream(logProportions).map(Math::exp).toArray();
    double[][] classProbs = new double[numComponents][numLabels];
    for (int k = 0; k < numComponents; k++) {
        for (int l = 0; l < numLabels; l++) {
            classProbs[k][l] = Math.exp(logClassProbs[k][l][1]);
        }
    }
    int[] components = IntStream.range(0, numComponents).toArray();
    EnumeratedIntegerDistribution enumeratedIntegerDistribution = new EnumeratedIntegerDistribution(components, proportions);
    BernoulliDistribution[][] bernoulliDistributions = new BernoulliDistribution[numComponents][numLabels];
    for (int k = 0; k < numComponents; k++) {
        for (int l = 0; l < numLabels; l++) {
            bernoulliDistributions[k][l] = new BernoulliDistribution(classProbs[k][l]);
        }
    }
    for (int num = 0; num < numSamples; num++) {
        MultiLabel multiLabel = new MultiLabel();
        int k = enumeratedIntegerDistribution.sample();
        for (int l = 0; l < numLabels; l++) {
            int v = bernoulliDistributions[k][l].sample();
            if (v == 1) {
                multiLabel.addLabel(l);
            }
        }
        list.add(multiLabel);
    }
    return list;
}
Also used : EnumeratedIntegerDistribution(org.apache.commons.math3.distribution.EnumeratedIntegerDistribution) MultiLabel(edu.neu.ccs.pyramid.dataset.MultiLabel) BernoulliDistribution(edu.neu.ccs.pyramid.util.BernoulliDistribution) ArrayList(java.util.ArrayList)

Aggregations

EnumeratedIntegerDistribution (org.apache.commons.math3.distribution.EnumeratedIntegerDistribution)6 DenseVector (org.apache.mahout.math.DenseVector)3 Vector (org.apache.mahout.math.Vector)3 MultiLabel (edu.neu.ccs.pyramid.dataset.MultiLabel)2 MultiLabelClfDataSet (edu.neu.ccs.pyramid.dataset.MultiLabelClfDataSet)2 IntegerDistribution (org.apache.commons.math3.distribution.IntegerDistribution)2 DataSet (edu.neu.ccs.pyramid.dataset.DataSet)1 BernoulliDistribution (edu.neu.ccs.pyramid.util.BernoulliDistribution)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 java.util.concurrent (java.util.concurrent)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1