/** * Returns the neighborhood of a user/index. * * @param idx user/index whose neighborhood is calculated * @return stream of user/item-similarity pairs. */ @Override public Stream<Tuple2id> getNeighbors(int idx) { IntDoubleTopN topN = new IntDoubleTopN(k); sim.similarElems(idx).forEach(topN::add); return topN.stream(); } }
/** * Tries to add an integer-double pair to the heap. * * @param key integer to be added * @param value double to be added * @return true if the pair was added to the heap, false otherwise */ public boolean add(int key, double value) { return add(tuple(key, value)); }
@Override public int[] rerankPermutation(Recommendation<U, I> recommendation, int maxLength) { List<Tuple2od<I>> items = recommendation.getItems(); int M = items.size(); int N = min(maxLength, M); IntDoubleTopN topN = new IntDoubleTopN(N); for (int i = 0; i < M; i++) { topN.add(M - i, likelihood(items.get(i)) * prior(items.get(i).v1)); } topN.sort(); return topN.reverseStream() .mapToInt(e -> M - e.v1) .toArray(); }
@Override public int[] rerankPermutation(Recommendation<U, I> recommendation, int maxLength) { List<Tuple2od<I>> items = recommendation.getItems(); int M = items.size(); int N = min(maxLength, M); if (variance == 0.0) { return getBasePerm(N); } NormalDistribution dist = new NormalDistribution(0.0, sqrt(variance)); IntDoubleTopN topN = new IntDoubleTopN(N); for (int i = 0; i < M; i++) { topN.add(M - i, log(i + 1) + dist.sample()); } topN.sort(); return topN.stream() .mapToInt(e -> M - e.v1) .toArray(); } }
@Override public FastRecommendation getRecommendation(int uidx, int maxLength, IntPredicate filter) { IntDoubleTopN topN = new IntDoubleTopN(min(maxLength, numItems())); for (int iidx = 0; iidx < numItems(); iidx++) { if (filter.test(iidx)) { topN.add(iidx, score(topicModel, uidx, iidx)); } } topN.sort(); List<Tuple2id> items = topN.reverseStream() .collect(toList()); return new FastRecommendation(uidx, items); }
@Override public FastRecommendation getRecommendation(int uidx, int maxLength, IntPredicate filter) { if (maxLength == 0) { maxLength = numItems(); } IntDoubleTopN topN = new IntDoubleTopN(maxLength); getAllIidx() .filter(filter) .forEach(iidx -> topN.add(iidx, fm.predict(uidx, new IdxPref(iidx, NaN)))); topN.sort(); List<Tuple2id> items = topN.reverseStream() .collect(toList()); return new FastRecommendation(uidx, items); }
@Override public FastRecommendation getRecommendation(int uidx, int maxLength, IntPredicate filter) { if (uidx == -1) { return new FastRecommendation(uidx, new ArrayList<>(0)); } Int2DoubleMap scoresMap = getScoresMap(uidx); final IntDoubleTopN topN = new IntDoubleTopN(min(maxLength, scoresMap.size())); scoresMap.int2DoubleEntrySet().forEach(e -> { int iidx = e.getIntKey(); double score = e.getDoubleValue(); if (filter.test(iidx)) { topN.add(iidx, score); } }); topN.sort(); List<Tuple2id> items = topN.reverseStream() .collect(toList()); return new FastRecommendation(uidx, items); }
@Override public FastRecommendation getRecommendation(int uidx, int maxLength, IntPredicate filter) { DoubleMatrix1D pu; pu = factorization.getUserVector(uidx2user(uidx)); if (pu == null) { return new FastRecommendation(uidx, new ArrayList<>()); } IntDoubleTopN topN = new IntDoubleTopN(min(maxLength, factorization.numItems())); DoubleMatrix1D r = factorization.getItemMatrix().zMult(pu, null); for (int iidx = 0; iidx < r.size(); iidx++) { if (filter.test(iidx)) { topN.add(iidx, r.getQuick(iidx)); } } topN.sort(); List<Tuple2id> items = topN.reverseStream() .collect(toList()); return new FastRecommendation(uidx, items); }
}); IntDoubleTopN topN = new IntDoubleTopN(N); for (int i = 0; i < M; i++) { topN.add(M - i, value(items.get(i), relStats, novMap, novStats)); topN.sort(); return topN.reverseStream() .mapToInt(e -> M - e.v1) .toArray();