/** * Create a long-to-double function from a map, casting if appropriate. Useful to allow unboxed access to maps that * are really fastutil maps. * @param map The map. * @return A function backed by {@code map}, or {@code map} if it is a Fastutil map. * @deprecated see {@link #asLong2DoubleMap(Map)} */ @Deprecated public static Long2DoubleMap asLong2DoubleFunction(final Map<Long,Double> map) { return asLong2DoubleMap(map); }
public Long2DoubleSortedMap clampVector(Map<Long,Double> scores) { SortedKeyIndex keys = SortedKeyIndex.fromCollection(scores.keySet()); Long2DoubleFunction baseVals = LongUtils.asLong2DoubleMap(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, Mean 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.asLong2DoubleMap(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; synchronized (context) { context.increment(score); } ImmutableMap.Builder<String,Double> results = ImmutableMap.builder(); return MetricResult.fromMap(results.put(columnName, score) .put(columnName + ".Raw", gain) .build()); }
@Nonnull @Override public ResultMap scoreWithDetails(long user, @Nonnull Collection<Long> items) { Long2DoubleMap userRatings = rvDAO.userRatingVector(user); if (userRatings.isEmpty()) { Map<Long, Double> scores = baseline.score(user, items); return Results.newResultMap(Iterables.transform(scores.entrySet(), Results.fromEntryFunction())); } else { // score everything, both rated and not, for offsets LongSet allItems = new LongOpenHashSet(userRatings.keySet()); allItems.addAll(items); Map<Long, Double> baseScores = baseline.score(user, allItems); Long2DoubleMap offsets = new Long2DoubleOpenHashMap(); // subtract scores from ratings, yielding offsets Long2DoubleFunction bsf = LongUtils.asLong2DoubleMap(baseScores); for (Long2DoubleMap.Entry e: userRatings.long2DoubleEntrySet()) { double base = bsf.get(e.getLongKey()); offsets.put(e.getLongKey(), e.getDoubleValue() - base); } double meanOffset = Vectors.sum(offsets) / (offsets.size() + damping); // to score: fill with baselines, add user mean offset List<Result> results = new ArrayList<>(items.size()); LongIterator iter = LongIterators.asLongIterator(items.iterator()); while (iter.hasNext()) { long item = iter.nextLong(); results.add(Results.create(item, bsf.get(item) + meanOffset)); } return Results.newResultMap(results); } }
/** * Implement recommendation by calling {@link ItemScorer#score(long, Collection)} and sorting * the results by score. This method uses {@link #getDefaultExcludes(long)} to get the default * exclude set for the user, if none is provided. */ @Override protected List<Long> recommend(long user, int n, LongSet candidates, LongSet exclude) { candidates = getEffectiveCandidates(user, candidates, exclude); logger.debug("Computing {} recommendations for user {} from {} candidates", n, user, candidates.size()); Map<Long, Double> scores = scorer.score(user, candidates); Long2DoubleAccumulator accum; if (n >= 0) { accum = new TopNLong2DoubleAccumulator(n); } else { accum = new UnlimitedLong2DoubleAccumulator(); } Long2DoubleMap map = LongUtils.asLong2DoubleMap(scores); for (Long2DoubleMap.Entry e: Vectors.fastEntries(map)) { accum.put(e.getLongKey(), e.getDoubleValue()); } return accum.finishList(); }
/** * Create a long-to-double function from a map, casting if appropriate. Useful to allow unboxed access to maps that * are really fastutil maps. * @param map The map. * @return A function backed by {@code map}, or {@code map} if it is a Fastutil map. * @deprecated see {@link #asLong2DoubleMap(Map)} */ @Deprecated public static Long2DoubleMap asLong2DoubleFunction(final Map<Long,Double> map) { return asLong2DoubleMap(map); }
@Nonnull @Override public ResultMap scoreWithDetails(long user, @Nonnull Collection<Long> items) { Long2DoubleMap userRatings = rvDAO.userRatingVector(user); if (userRatings.isEmpty()) { Map<Long, Double> scores = baseline.score(user, items); return Results.newResultMap(Iterables.transform(scores.entrySet(), Results.fromEntryFunction())); } else { // score everything, both rated and not, for offsets LongSet allItems = new LongOpenHashSet(userRatings.keySet()); allItems.addAll(items); Map<Long, Double> baseScores = baseline.score(user, allItems); Long2DoubleMap offsets = new Long2DoubleOpenHashMap(); // subtract scores from ratings, yielding offsets Long2DoubleFunction bsf = LongUtils.asLong2DoubleMap(baseScores); for (Long2DoubleMap.Entry e: userRatings.long2DoubleEntrySet()) { double base = bsf.get(e.getLongKey()); offsets.put(e.getLongKey(), e.getDoubleValue() - base); } double meanOffset = Vectors.sum(offsets) / (offsets.size() + damping); // to score: fill with baselines, add user mean offset List<Result> results = new ArrayList<>(items.size()); LongIterator iter = LongIterators.asLongIterator(items.iterator()); while (iter.hasNext()) { long item = iter.nextLong(); results.add(Results.create(item, bsf.get(item) + meanOffset)); } return Results.newResultMap(results); } }
/** * Implement recommendation by calling {@link ItemScorer#score(long, Collection)} and sorting * the results by score. This method uses {@link #getDefaultExcludes(long)} to get the default * exclude set for the user, if none is provided. */ @Override protected List<Long> recommend(long user, int n, LongSet candidates, LongSet exclude) { candidates = getEffectiveCandidates(user, candidates, exclude); logger.debug("Computing {} recommendations for user {} from {} candidates", n, user, candidates.size()); Map<Long, Double> scores = scorer.score(user, candidates); Long2DoubleAccumulator accum; if (n >= 0) { accum = new TopNLong2DoubleAccumulator(n); } else { accum = new UnlimitedLong2DoubleAccumulator(); } Long2DoubleMap map = LongUtils.asLong2DoubleMap(scores); for (Long2DoubleMap.Entry e: Vectors.fastEntries(map)) { accum.put(e.getLongKey(), e.getDoubleValue()); } return accum.finishList(); }