@Override public boolean contains(final Object o) { if (!(o instanceof Map.Entry)) return false; if (o instanceof Long2DoubleMap.Entry) { final Long2DoubleMap.Entry e = (Long2DoubleMap.Entry) o; final long k = e.getLongKey(); return map.containsKey(k) && (Double.doubleToLongBits(map.get(k)) == Double.doubleToLongBits(e.getDoubleValue())); } final Map.Entry<?, ?> e = (Map.Entry<?, ?>) o; final Object key = e.getKey(); if (key == null || !(key instanceof Long)) return false; final long k = ((Long) (key)).longValue(); final Object value = e.getValue(); if (value == null || !(value instanceof Double)) return false; return map.containsKey(k) && (Double.doubleToLongBits(map.get(k)) == Double .doubleToLongBits(((Double) (value)).doubleValue())); }
/** * {@inheritDoc} * * @deprecated Please use the corresponding type-specific method instead. */ @Deprecated @Override default Double setValue(final Double value) { return Double.valueOf(setValue((value).doubleValue())); } }
long leftNode = seedWithWeight.getLongKey(); double weight = seedWithWeight.getDoubleValue(); NodeMetadataMultiSegmentIterator edgeIterator = (NodeMetadataMultiSegmentIterator) graph.getLeftNodeEdges(leftNode);
if (salsaInternalState.getBipartiteGraph().getLeftNodeDegree(entry.getLongKey()) > 0) { totalWeight += entry.getDoubleValue(); nonZeroSeedSet.add(entry.getLongKey());
@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); } }
long iid = r.getLongKey(); int idx = bld.add(uid, iid, r.getDoubleValue()); imap.put(iid, idx);
Long2DoubleMap offsets = new Long2DoubleOpenHashMap(sums.size()); for (Long2DoubleMap.Entry e: sums.long2DoubleEntrySet()) { long user = e.getLongKey(); double usum = e.getDoubleValue(); double ucount = counts.get(user); usum += damping * mean;
while (iter.hasNext()) { Long2DoubleMap.Entry rating = iter.next(); final long item = rating.getLongKey(); itemRatings.put(item, ivect); ivect.put(uid, rating.getDoubleValue());
int averageWeight = 0; for (Long2DoubleMap.Entry entry: seedsWithWeights.long2DoubleEntrySet()) { long seed = entry.getLongKey(); double seedWeight = entry.getDoubleValue(); indexArray[index] = seed; int finalWeight = (int) (Math.round(MULTIPER_FOR_ALIASTABLE * seedWeight
private void collectRightNodeInfo(Request request) { for (Long2DoubleMap.Entry entry: request.getLeftSeedNodesWithWeight().long2DoubleEntrySet()) { long leftNode = entry.getLongKey(); EdgeIterator edgeIterator = leftIndexedBipartiteGraph.getLeftNodeEdges(leftNode); if (edgeIterator == null) { double weight = entry.getDoubleValue(); seenEdgesPerNode.clear();
long leftNode = entry.getLongKey(); EdgeIterator edgeIterator = leftIndexedBipartiteGraph.getLeftNodeEdges(leftNode); if (edgeIterator == null) { double weight = entry.getDoubleValue(); seenEdgesPerNode.clear();
/** * Compute the dot product of two maps. This method assumes any value missing in one map is 0, so it is the dot * product of the values of common keys. * @param v1 The first vector. * @param v2 The second vector. * @return The sum of the products of corresponding values in the two vectors. */ public static double dotProduct(Long2DoubleMap v1, Long2DoubleMap v2) { if (v1.size() > v2.size()) { // compute dot product the other way around for speed return dotProduct(v2, v1); } if (v1 instanceof Long2DoubleSortedArrayMap && v2 instanceof Long2DoubleSortedArrayMap) { return dotProduct((Long2DoubleSortedArrayMap) v1, (Long2DoubleSortedArrayMap) v2); } else { double result = 0; Long2DoubleFunction v2d = adaptDefaultValue(v2, 0.0); Iterator<Long2DoubleMap.Entry> iter = fastEntryIterator(v1); while (iter.hasNext()) { Long2DoubleMap.Entry e = iter.next(); long k = e.getLongKey(); result += e.getDoubleValue() * v2d.get(k); // since default is 0 } return result; } }
/** * Score a single item into an accumulator. * @param scores The reference scores. * @param item The item to score. * @param accum The accumulator. */ protected void scoreItem(Long2DoubleMap scores, long item, ItemItemScoreAccumulator accum) { Long2DoubleMap allNeighbors = model.getNeighbors(item); Long2DoubleAccumulator acc; if (neighborhoodSize > 0) { // FIXME Abstract accumulator selection logic acc = new TopNLong2DoubleAccumulator(neighborhoodSize); } else { acc = new UnlimitedLong2DoubleAccumulator(); } for (Long2DoubleMap.Entry nbr: allNeighbors.long2DoubleEntrySet()) { if (scores.containsKey(nbr.getLongKey())) { acc.put(nbr.getLongKey(), nbr.getDoubleValue()); } } Long2DoubleMap neighborhood = acc.finishMap(); scorer.score(item, neighborhood, scores, accum); } }
protected void scoreItem(Long2DoubleMap userData, long item, ItemItemScoreAccumulator accum) { // find the usable neighbors Long2DoubleSortedArrayMap allNeighbors = Long2DoubleSortedArrayMap.create(model.getNeighbors(item)); Long2DoubleMap neighborhood = allNeighbors.subMap(userData.keySet()); if (neighborhoodSize > 0) { if (logger.isTraceEnabled()) { logger.trace("truncating {} neighbors to {}", neighborhood.size(), neighborhoodSize); } Long2DoubleAccumulator acc = new TopNLong2DoubleAccumulator(neighborhoodSize); for (Long2DoubleMap.Entry e: neighborhood.long2DoubleEntrySet()) { acc.put(e.getLongKey(), e.getDoubleValue()); } neighborhood = acc.finishMap(); } assert neighborhoodSize <= 0 || neighborhood.size() <= neighborhoodSize; if (neighborhood.size() < minNeighbors) { return; } if (logger.isTraceEnabled()) { logger.trace("scoring item {} with {} of {} neighbors", item, neighborhood.size(), allNeighbors.size()); } scorer.score(item, neighborhood, userData, accum); } }
@Nonnull @Override public Map<String, Object> measureUser(TestUser testUser) { Long2DoubleMap ratings = testUser.getTestRatings(); ResultMap results = predictor.predictWithDetails(testUser.getUserId(), ratings.keySet()); // Measure the user results Map<String,Object> row = new HashMap<>(); for (MetricContext<?> mc: predictMetricContexts) { row.putAll(mc.measureUser(testUser, results).getValues()); } // Write all attempted predictions for (Long2DoubleMap.Entry e: ratings.long2DoubleEntrySet()) { Result pred = results.get(e.getLongKey()); try { if (writer != null) { writer.writeRow(testUser.getUserId(), e.getLongKey(), e.getDoubleValue(), pred != null ? pred.getScore() : null); } } catch (IOException ex) { throw new EvaluationException("error writing prediction row", ex); } } return row; }
/** * 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(); }
@Override public boolean equals(final Object o) { if (!(o instanceof Map.Entry)) return false; if (o instanceof Long2DoubleMap.Entry) { final Long2DoubleMap.Entry e = (Long2DoubleMap.Entry) o; return ((key) == (e.getLongKey())) && (Double.doubleToLongBits(value) == Double.doubleToLongBits(e.getDoubleValue())); } final Map.Entry<?, ?> e = (Map.Entry<?, ?>) o; final Object key = e.getKey(); if (key == null || !(key instanceof Long)) return false; final Object value = e.getValue(); if (value == null || !(value instanceof Double)) return false; return ((this.key) == (((Long) (key)).longValue())) && (Double.doubleToLongBits(this.value) == Double .doubleToLongBits(((Double) (value)).doubleValue())); } @Override
@Override public BinaryOperator<Long2ObjectMap<Long2DoubleAccumulator>> combiner() { return (am1, am2) -> { for (Long2ObjectMap.Entry<Long2DoubleAccumulator> e: Long2ObjectMaps.fastIterable(am2)) { long item = e.getLongKey(); Long2DoubleAccumulator a2 = e.getValue(); Long2DoubleAccumulator a1 = am1.get(item); if (a1 == null) { am1.put(item, a2); } else { for (Long2DoubleMap.Entry ae: Long2DoubleMaps.fastIterable(a2.finishMap())) { a1.put(ae.getLongKey(), ae.getDoubleValue()); } } } return am1; }; }
/** {@inheritDoc} */ @SuppressWarnings({"unchecked", "deprecation"}) @Override public void putAll(final Map<? extends Long, ? extends Double> m) { if (m instanceof Long2DoubleMap) { ObjectIterator<Long2DoubleMap.Entry> i = Long2DoubleMaps.fastIterator((Long2DoubleMap) m); while (i.hasNext()) { final Long2DoubleMap.Entry e = i.next(); put(e.getLongKey(), e.getDoubleValue()); } } else { int n = m.size(); final Iterator<? extends Map.Entry<? extends Long, ? extends Double>> i = m.entrySet().iterator(); Map.Entry<? extends Long, ? extends Double> e; while (n-- != 0) { e = i.next(); put(e.getKey(), e.getValue()); } } } /**
@Override public boolean remove(final Object o) { if (!(o instanceof Map.Entry)) return false; if (o instanceof Long2DoubleMap.Entry) { final Long2DoubleMap.Entry e = (Long2DoubleMap.Entry) o; return map.remove(e.getLongKey(), e.getDoubleValue()); } Map.Entry<?, ?> e = (Map.Entry<?, ?>) o; final Object key = e.getKey(); if (key == null || !(key instanceof Long)) return false; final long k = ((Long) (key)).longValue(); final Object value = e.getValue(); if (value == null || !(value instanceof Double)) return false; final double v = ((Double) (value)).doubleValue(); return map.remove(k, v); } @Override