/** * Samples a multiple indices with replacement from an array of cumulative * proportions. * * @param random * The random number generator to use. * @param cumulativeProportions * The array of cumulative proportions. The entries must be * non-negative and monotonically increasing. * @param sampleSize * The number of samples to draw from the cumulative proportions. * @return * An array of indices of sampled with replacement according to * the given cumulative proportions. */ public static int[] sampleIndicesFromCumulativeProportions( final Random random, final double[] cumulativeProportions, final int sampleSize) { final int[] result = new int[sampleSize]; for (int i = 0; i < sampleSize; i++) { result[i] = sampleIndexFromCumulativeProportions( random, cumulativeProportions); } return result; }
final int randomIndex = sampleIndexFromProportions( random, proportions, proportionSum); return new int[] { randomIndex }; return sampleIndicesFromCumulativeProportions( random, cumulativeProportions, sampleSize);
/** * Samples a a given number of items from a list with replacement. * * @param <DataType> * The type of data in the list. * @param random * The random number generator. * @param data * The list to sample from. * @param sampleSize * The sample size. Must be positive. * @return * An array list of the given size sampled with replacement from the * given data. */ public static <DataType> ArrayList<DataType> sampleWithReplacement( final Random random, final List<? extends DataType> data, final int sampleSize) { final ArrayList<DataType> result = new ArrayList<DataType>(sampleSize); sampleWithReplacementInto(random, data, sampleSize, result); return result; }
@Override public ArrayList<ClusterType> initializeClusters(int numClusters, Collection<? extends DataType> elements) { List<? extends DataType> elementsList = elements instanceof List ? (List<? extends DataType>) elements : new ArrayList<>(elements); ArrayList<DataType> representativePoints = DiscreteSamplingUtil.sampleWithReplacement(random, elementsList, numClusters); ArrayList<ClusterType> clusters = representativePoints.stream() .map(point -> creator.createCluster(Arrays.asList(point))) .collect(Collectors.toCollection(ArrayList::new)); return clusters; }
/** * Samples a random index according to the given proportions. Note that * calling this requires calculating the sum of the proportions first, so * if it is known in advance, it is more efficient to call the version of * this method that takes the sum of the proportions as a parameter. * * @param random * The random number generator to use. * @param proportions * The array of proportions. All entries must be greater than or * equal to zero. * @return * A random index sampled according to the given proportions. */ public static int sampleIndexFromProportions( final Random random, final double[] proportions) { // To sample from the proportions we will need the sum. double proportionSum = 0.0; for (double value : proportions) { proportionSum += value; } return sampleIndexFromProportions(random, proportions, proportionSum); }
subSet = DiscreteSamplingUtil.sampleWithoutReplacement( random, dataList, dataSampleSize);
@Override public ArrayList<ClusterType> initializeClusters(int numClusters, Collection<? extends DataType> elements) { List<? extends DataType> elementsList = elements instanceof List ? (List<? extends DataType>) elements : new ArrayList<>(elements); ArrayList<DataType> representativePoints = DiscreteSamplingUtil.sampleWithReplacement(random, elementsList, numClusters); ArrayList<ClusterType> clusters = representativePoints.stream() .map(point -> creator.createCluster(Arrays.asList(point))) .collect(Collectors.toCollection(ArrayList::new)); return clusters; }
/** * Samples a random index according to the given proportions. Note that * calling this requires calculating the sum of the proportions first, so * if it is known in advance, it is more efficient to call the version of * this method that takes the sum of the proportions as a parameter. * * @param random * The random number generator to use. * @param proportions * The array of proportions. All entries must be greater than or * equal to zero. * @return * A random index sampled according to the given proportions. */ public static int sampleIndexFromProportions( final Random random, final double[] proportions) { // To sample from the proportions we will need the sum. double proportionSum = 0.0; for (double value : proportions) { proportionSum += value; } return sampleIndexFromProportions(random, proportions, proportionSum); }
subSet = DiscreteSamplingUtil.sampleWithoutReplacement( random, dataList, dataSampleSize);
/** * Samples a multiple indices with replacement from an array of cumulative * proportions. * * @param random * The random number generator to use. * @param cumulativeProportions * The array of cumulative proportions. The entries must be * non-negative and monotonically increasing. * @param sampleSize * The number of samples to draw from the cumulative proportions. * @return * An array of indices of sampled with replacement according to * the given cumulative proportions. */ public static int[] sampleIndicesFromCumulativeProportions( final Random random, final double[] cumulativeProportions, final int sampleSize) { final int[] result = new int[sampleSize]; for (int i = 0; i < sampleSize; i++) { result[i] = sampleIndexFromCumulativeProportions( random, cumulativeProportions); } return result; }
final int randomIndex = sampleIndexFromProportions( random, proportions, proportionSum); return new int[] { randomIndex }; return sampleIndicesFromCumulativeProportions( random, cumulativeProportions, sampleSize);
@Override public ArrayList<ClusterType> initializeClusters(int numClusters, Collection<? extends DataType> elements) { List<? extends DataType> elementsList = elements instanceof List ? (List<? extends DataType>) elements : new ArrayList<>(elements); ArrayList<DataType> representativePoints = DiscreteSamplingUtil.sampleWithReplacement(random, elementsList, numClusters); ArrayList<ClusterType> clusters = representativePoints.stream() .map(point -> creator.createCluster(Arrays.asList(point))) .collect(Collectors.toCollection(ArrayList::new)); return clusters; }
/** * Samples a a given number of items from a list with replacement. * * @param <DataType> * The type of data in the list. * @param random * The random number generator. * @param data * The list to sample from. * @param sampleSize * The sample size. Must be positive. * @return * An array list of the given size sampled with replacement from the * given data. */ public static <DataType> ArrayList<DataType> sampleWithReplacement( final Random random, final List<? extends DataType> data, final int sampleSize) { final ArrayList<DataType> result = new ArrayList<DataType>(sampleSize); sampleWithReplacementInto(random, data, sampleSize, result); return result; }
/** * Samples a random index according to the given proportions. Note that * calling this requires calculating the sum of the proportions first, so * if it is known in advance, it is more efficient to call the version of * this method that takes the sum of the proportions as a parameter. * * @param random * The random number generator to use. * @param proportions * The array of proportions. All entries must be greater than or * equal to zero. * @return * A random index sampled according to the given proportions. */ public static int sampleIndexFromProportions( final Random random, final double[] proportions) { // To sample from the proportions we will need the sum. double proportionSum = 0.0; for (double value : proportions) { proportionSum += value; } return sampleIndexFromProportions(random, proportions, proportionSum); }
subSet = DiscreteSamplingUtil.sampleWithoutReplacement( random, dataList, dataSampleSize);
/** * Samples a multiple indices with replacement from an array of cumulative * proportions. * * @param random * The random number generator to use. * @param cumulativeProportions * The array of cumulative proportions. The entries must be * non-negative and monotonically increasing. * @param sampleSize * The number of samples to draw from the cumulative proportions. * @return * An array of indices of sampled with replacement according to * the given cumulative proportions. */ public static int[] sampleIndicesFromCumulativeProportions( final Random random, final double[] cumulativeProportions, final int sampleSize) { final int[] result = new int[sampleSize]; for (int i = 0; i < sampleSize; i++) { result[i] = sampleIndexFromCumulativeProportions( random, cumulativeProportions); } return result; }
final int randomIndex = sampleIndexFromProportions( random, proportions, proportionSum); return new int[] { randomIndex }; return sampleIndicesFromCumulativeProportions( random, cumulativeProportions, sampleSize);
= DiscreteSamplingUtil.sampleWithReplacement(random, dataIndices, minibatchSize);
/** * Samples a a given number of items from a list with replacement. * * @param <DataType> * The type of data in the list. * @param random * The random number generator. * @param data * The list to sample from. * @param sampleSize * The sample size. Must be positive. * @return * An array list of the given size sampled with replacement from the * given data. */ public static <DataType> ArrayList<DataType> sampleWithReplacement( final Random random, final List<? extends DataType> data, final int sampleSize) { final ArrayList<DataType> result = new ArrayList<DataType>(sampleSize); sampleWithReplacementInto(random, data, sampleSize, result); return result; }
return DiscreteSamplingUtil.sampleIndexFromCumulativeProportions(this.random, topicCumulativeProportions);