Package org.grouplens.lenskit.vectors

Examples of org.grouplens.lenskit.vectors.SparseVector


    }

    @Override
    public Iterable<Neighbor> getCandidateNeighbors(UserHistory<? extends Event> user, LongSet items) {
        final long uid = user.getUserId();
        SparseVector urs = RatingVectorUserHistorySummarizer.makeRatingVector(user);
        final ImmutableSparseVector nratings = normalizer.normalize(user.getUserId(), urs, null)
                                                   .freeze();
        final LongSet candidates = findCandidateNeighbors(uid, nratings, items);
        logger.debug("found {} candidate neighbors for {}", candidates.size(), uid);
        return new Iterable<Neighbor>() {
View Full Code Here


        Stopwatch timer = Stopwatch.createStarted();

        for (int i = 0; i < nitems; i++) {
            assert matrix.size() == i;
            final long rowItem = itemDomain.getKey(i);
            final SparseVector vec1 = buildContext.itemVector(rowItem);

            // Take advantage of sparsity if we can
            LongIterator neighbors = iterationStrategy.neighborIterator(buildContext, rowItem, false);
            currentRow.fill(0);

            // Compute similarities and populate the vector
            while (neighbors.hasNext()) {
                final long colItem = neighbors.nextLong();
                final SparseVector vec2 = buildContext.itemVector(colItem);
                assert currentRow.containsKey(colItem);
                currentRow.set(colItem, similarity.similarity(rowItem, vec1, colItem, vec2));
            }

            // Remove the current item (it is not its own neighbor)
View Full Code Here

        SparseVector[] itemRatings = new SparseVector[n];

        for (int i = 0; i < n; i++) {
            final long item = items.getKey(i);
            ScoredIdListBuilder ratings = itemRatingData.get(item);
            SparseVector v = ratings.buildVector();
            assert v.size() == ratings.size();
            itemRatings[i] = v;
            // release some memory
            ratings.clear();
        }
View Full Code Here

        // initialize the transposed array to collect item vector data
        Cursor<UserHistory<Event>> users = userEventDAO.streamEventsByUser();
        try {
            for (UserHistory<Event> user : users) {
                long uid = user.getUserId();
                SparseVector summary = userSummarizer.summarize(user);
                MutableSparseVector normed = summary.mutableCopy();
                normalizer.normalize(uid, summary, normed);

                for (VectorEntry rating : normed) {
                    final long item = rating.getKey();
                    // get the item's rating accumulator
                    ScoredIdListBuilder ivect = itemRatings.get(item);
                    if (ivect == null) {
                        ivect = ScoredIds.newListBuilder(100);
                        itemRatings.put(item, ivect);
                    }
                    ivect.add(uid, rating.getValue());
                }

                // get the item's candidate set
                userItems.put(uid, LongUtils.packedSet(summary.keySet()));
            }
        } finally {
            users.close();
        }
    }
View Full Code Here

    @Test
    public void testItemScorerNoRating() {
        long[] items = {7, 8};
        ItemItemScorer scorer = session.get(ItemItemScorer.class);
        assertThat(scorer, notNullValue());
        SparseVector scores = scorer.score(5, LongArrayList.wrap(items));
        assertThat(scores, notNullValue());
        assertThat(scores.size(), equalTo(1));
        assertThat(scores.get(7), not(notANumber()));
        assertThat(scores.containsKey(8), equalTo(false));
    }
View Full Code Here

    @Test
    public void testItemScorerChannels() {
        long[] items = {7, 8};
        ItemItemScorer scorer = session.get(ItemItemScorer.class);
        assertThat(scorer, notNullValue());
        SparseVector scores = scorer.score(5, LongArrayList.wrap(items));
        assertThat(scores, notNullValue());
        assertThat(scores.size(), equalTo(1));
        assertThat(scores.get(7), not(notANumber()));
        assertThat(scores.getChannelVector(ItemItemScorer.NEIGHBORHOOD_SIZE_SYMBOL).
                get(7), closeTo(1.0, 1.0e-5));
        assertThat(scores.containsKey(8), equalTo(false));

        long[] items2 = {7, 8, 9};
        scorer = session.get(ItemItemScorer.class);
        assertThat(scorer, notNullValue());
        scores = scorer.score(2, LongArrayList.wrap(items2));
        assertThat(scores.getChannelVector(ItemItemScorer.NEIGHBORHOOD_SIZE_SYMBOL).
                get(9), closeTo(3.0, 1.0e-5))// 1, 7, 8
    }
View Full Code Here

        long[] userIds = {101, 102, 103, 104};
        double[] ratings1 = {4.0, 3.0, 2.5, 2.0};
        double[] ratings2 = {3.0, 2.5, 4.0, 1.0};
        double[] ratings3 = {5.0, 3.5, 0.5, 1.0};
        double[] ratings4 = {4.5, 3.0, 3.5, 1.5};
        SparseVector v1 = MutableSparseVector.wrap(userIds, ratings1);
        SparseVector v2 = MutableSparseVector.wrap(userIds, ratings2);
        SparseVector v3 = MutableSparseVector.wrap(userIds, ratings3);
        SparseVector v4 = MutableSparseVector.wrap(userIds, ratings4);

        SparseVector[] ratings = { v1, v2, v3, v4 };
        ItemItemBuildContext context = new ItemItemBuildContext(items, ratings,
                                                                new Long2ObjectOpenHashMap<LongSortedSet>());
View Full Code Here

        LongKeyDomain items = LongKeyDomain.create(1,2,3,4);

        long[] userIds = {101, 102, 103, 104};
        double[] ratings1 = {4.0, 3.0, 2.5, 2.0};
        double[] ratings4 = {4.5, 3.0, 3.5, 1.5};
        SparseVector v1 = MutableSparseVector.wrap(userIds, ratings1);
        SparseVector v4 = MutableSparseVector.wrap(userIds, ratings4);

        SparseVector[] ratingMap = {
                v1,
                MutableSparseVector.create(),
                MutableSparseVector.create(),
View Full Code Here

    public void predict(long user, @Nonnull MutableSparseVector scores) {
        itemScorer.score(user, scores);
        if (baselineScorer != null) {
            LongSet unset = scores.unsetKeySet();
            if (!unset.isEmpty()) {
                SparseVector bscores = baselineScorer.score(user, unset);
                scores.set(bscores);
            }
        }
        quantize(scores);
    }
View Full Code Here

     * {@inheritDoc}
     * <p>Delegates to {@link #score(long, Collection)}.
     */
    @Override
    public double score(long user, long item) {
        SparseVector v = score(user, LongLists.singleton(item));
        return v.get(item, Double.NaN);
    }
View Full Code Here

TOP

Related Classes of org.grouplens.lenskit.vectors.SparseVector

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.