/** * Create a new map with existing data. * @param data Use {@link #create(Map)} instead, as it can avoid copying maps that are already packed. */ @Deprecated public Long2DoubleSortedArrayMap(Map<Long,Double> data) { Long2DoubleFunction vf = LongUtils.asLong2DoubleFunction(data); keys = SortedKeyIndex.fromCollection(data.keySet()); int size = keys.size(); values = new double[size]; for (int i = 0; i < size; i++) { values[i] = vf.get(keys.getKey(i)); } }
/** * Create a new map with existing data. * @param data Use {@link #create(Map)} instead, as it can avoid copying maps that are already packed. */ @Deprecated public Long2DoubleSortedArrayMap(Map<Long,Double> data) { Long2DoubleFunction vf = LongUtils.asLong2DoubleFunction(data); keys = SortedKeyIndex.fromCollection(data.keySet()); int size = keys.size(); values = new double[size]; for (int i = 0; i < size; i++) { values[i] = vf.get(keys.getKey(i)); } }
public Long2DoubleSortedMap clampVector(Map<Long,Double> scores) { SortedKeyIndex keys = SortedKeyIndex.fromCollection(scores.keySet()); Long2DoubleFunction baseVals = LongUtils.asLong2DoubleFunction(scores); double[] values = new double[keys.size()]; for (int i = 0; i < values.length; i++) { long item = keys.getKey(i); values[i] = clampValue(baseVals.get(item)); } return Long2DoubleSortedArrayMap.wrap(keys, values); }
@Nonnull @Override public MetricResult measureUser(TestUser user, ResultMap predictions, MeanAccumulator context) { if (predictions == null || predictions.isEmpty()) { return MetricResult.empty(); } Long2DoubleMap ratings = user.getTestRatings(); long[] ideal = ratings.keySet().toLongArray(); LongArrays.quickSort(ideal, LongComparators.oppositeComparator(LongUtils.keyValueComparator(ratings))); long[] actual = LongUtils.asLongSet(predictions.keySet()).toLongArray(); LongArrays.quickSort(actual, LongComparators.oppositeComparator( LongUtils.keyValueComparator( LongUtils.asLong2DoubleFunction(predictions.scoreMap())))); double idealGain = computeDCG(ideal, ratings); double gain = computeDCG(actual, ratings); logger.debug("user {} has gain of {} (ideal {})", user.getUserId(), gain, idealGain); double score = gain / idealGain; context.add(score); ImmutableMap.Builder<String,Double> results = ImmutableMap.builder(); return MetricResult.fromMap(results.put(columnName, score) .put(columnName + ".Raw", gain) .build()); }