/** * Construct a new least-squares scorer. * * @param ubs the user biases * @param ibs the item biases * @param mean the global mean rating */ public LeastSquaresItemScorer(Long2DoubleMap ubs, Long2DoubleMap ibs, double mean) { this.userBiases = LongUtils.frozenMap(ubs); this.itemBiases = LongUtils.frozenMap(ibs); this.globalMean = mean; }
@Override public Long2DoubleMap truncate(Long2DoubleMap v) { return LongUtils.frozenMap(v); } }
/** * Construct a new item-item model. * * @param nbrs The item neighborhoods. The item neighborhood lists are not copied. */ public SimilarityMatrixModel(Map<Long,Long2DoubleMap> nbrs) { itemDomain = SortedKeyIndex.fromCollection(nbrs.keySet()); int n = itemDomain.size(); assert n == nbrs.size(); ImmutableList.Builder<Long2DoubleMap> neighbors = ImmutableList.builder(); for (int i = 0; i < n; i++) { neighbors.add(LongUtils.frozenMap(nbrs.get(itemDomain.getKey(i)))); } neighborhoods = neighbors.build(); }
@Override public BiConsumer<Map<Long, Long2DoubleMap>, IdBox<Long2DoubleMap>> accumulator() { return (acc, row) -> { Long2DoubleMap r2; if (modelSize <= 0) { r2 = LongUtils.frozenMap(row.getValue()); } else { Long2DoubleAccumulator racc = new TopNLong2DoubleAccumulator(modelSize); racc.putAll(row.getValue()); r2 = racc.finishMap(); } Long2DoubleMap res = acc.putIfAbsent(row.getId(), r2); assert res == null; }; }
final long item = items.getKey(i); Long2DoubleMap ratings = itemRatingData.get(item); itemRatings[i] = LongUtils.frozenMap(ratings);
row = truncator.truncate(row); matrix.add(LongUtils.frozenMap(row));
@Override protected Neighbor computeNext() { while (neighborIter.hasNext()) { final long neighbor = neighborIter.nextLong(); Long2DoubleMap rawRatings = getUserRatingVector(neighbor); if (rawRatings != null) { rawRatings = LongUtils.frozenMap(rawRatings); InvertibleFunction<Long2DoubleMap, Long2DoubleMap> xform = similarityNormalizer.makeTransformation(neighbor, rawRatings); Long2DoubleMap nbrRatings = xform.apply(rawRatings); final double sim = similarity.similarity(user, userVector, neighbor, nbrRatings); if (acceptSimilarity(sim)) { // we have found a neighbor Long2DoubleMap ratings; if (scoreNormalizer.equals(similarityNormalizer)) { ratings = nbrRatings; } else { ratings = scoreNormalizer.makeTransformation(neighbor, rawRatings).apply(rawRatings); } return new Neighbor(neighbor, ratings, sim); } } } // no neighbor found, done return endOfData(); } }
/** * Construct a new least-squares scorer. * * @param ubs the user biases * @param ibs the item biases * @param mean the global mean rating */ public LeastSquaresItemScorer(Long2DoubleMap ubs, Long2DoubleMap ibs, double mean) { this.userBiases = LongUtils.frozenMap(ubs); this.itemBiases = LongUtils.frozenMap(ibs); this.globalMean = mean; }
@Override public Long2DoubleMap truncate(Long2DoubleMap v) { return LongUtils.frozenMap(v); } }
try (ObjectStream<IdBox<Long2DoubleMap>> users = rvDAO.streamUsers()) { for (IdBox<Long2DoubleMap> user : users) { Long2DoubleMap uvec = LongUtils.frozenMap(user.getValue()); vectors.put(user.getId(), uvec);
@Test public void testUnitVector() { for (Map<Long,Double> map: someMaps(longs(), doubles(-100, 100))) { if (map.isEmpty()) { continue; } Long2DoubleMap vec = LongUtils.frozenMap(map); double norm = Vectors.euclideanNorm(vec); Long2DoubleMap unit = Vectors.unitVector(vec); assertThat(unit.size(), equalTo(vec.size())); assertThat(unit.keySet(), equalTo(vec.keySet())); assertThat(Vectors.euclideanNorm(unit), closeTo(1.0, 1.0e-6)); Long2DoubleMaps.fastForEach(unit, e -> { assertThat(e.getDoubleValue() * norm, closeTo(vec.get(e.getLongKey()), 1.0e-6)); }); } } }
/** * Construct a new item-item model. * * @param nbrs The item neighborhoods. The item neighborhood lists are not copied. */ public SimilarityMatrixModel(Map<Long,Long2DoubleMap> nbrs) { itemDomain = SortedKeyIndex.fromCollection(nbrs.keySet()); int n = itemDomain.size(); assert n == nbrs.size(); ImmutableList.Builder<Long2DoubleMap> neighbors = ImmutableList.builder(); for (int i = 0; i < n; i++) { neighbors.add(LongUtils.frozenMap(nbrs.get(itemDomain.getKey(i)))); } neighborhoods = neighbors.build(); }
@Override public BiConsumer<Map<Long, Long2DoubleMap>, IdBox<Long2DoubleMap>> accumulator() { return (acc, row) -> { Long2DoubleMap r2; if (modelSize <= 0) { r2 = LongUtils.frozenMap(row.getValue()); } else { Long2DoubleAccumulator racc = new TopNLong2DoubleAccumulator(modelSize); racc.putAll(row.getValue()); r2 = racc.finishMap(); } Long2DoubleMap res = acc.putIfAbsent(row.getId(), r2); assert res == null; }; }
final long item = items.getKey(i); Long2DoubleMap ratings = itemRatingData.get(item); itemRatings[i] = LongUtils.frozenMap(ratings);
row = truncator.truncate(row); matrix.add(LongUtils.frozenMap(row));
@Override protected Neighbor computeNext() { while (neighborIter.hasNext()) { final long neighbor = neighborIter.nextLong(); Long2DoubleMap rawRatings = getUserRatingVector(neighbor); if (rawRatings != null) { rawRatings = LongUtils.frozenMap(rawRatings); InvertibleFunction<Long2DoubleMap, Long2DoubleMap> xform = similarityNormalizer.makeTransformation(neighbor, rawRatings); Long2DoubleMap nbrRatings = xform.apply(rawRatings); final double sim = similarity.similarity(user, userVector, neighbor, nbrRatings); if (acceptSimilarity(sim)) { // we have found a neighbor Long2DoubleMap ratings; if (scoreNormalizer.equals(similarityNormalizer)) { ratings = nbrRatings; } else { ratings = scoreNormalizer.makeTransformation(neighbor, rawRatings).apply(rawRatings); } return new Neighbor(neighbor, ratings, sim); } } } // no neighbor found, done return endOfData(); } }
try (ObjectStream<IdBox<Long2DoubleMap>> users = rvDAO.streamUsers()) { for (IdBox<Long2DoubleMap> user : users) { Long2DoubleMap uvec = LongUtils.frozenMap(user.getValue()); vectors.put(user.getId(), uvec);