Package org.grouplens.lenskit.vectors

Examples of org.grouplens.lenskit.vectors.MutableSparseVector


    public void predict(long uid, @Nonnull MutableSparseVector predictions) {
        logger.debug("predicting {} items for {}", predictions.keyDomain().size(), uid);
        OrdRecModel params = new OrdRecModel(quantizer);
        SparseVector ratings = makeUserVector(uid, userEventDao);
        LongSet keySet = LongUtils.setUnion(ratings.keySet(), predictions.keyDomain());
        MutableSparseVector scores = MutableSparseVector.create(keySet);
        itemScorer.score(uid, scores);
        params.train(ratings, scores);
        logger.debug("trained parameters for {}: {}", uid, params);

        Vector probabilities = Vector.createLength(params.getLevelCount());
        Long2ObjectMap<IVector> distChannel = null;
        if (reportDistribution) {
            distChannel = predictions.addChannel(RATING_PROBABILITY_CHANNEL);
        }

        for (VectorEntry e: predictions.view(VectorEntry.State.EITHER)) {
            long iid = e.getKey();
            double score = scores.get(iid);
            params.getProbDistribution(score, probabilities);

            int mlIdx = probabilities.maxElementIndex();

            predictions.set(e, quantizer.getIndexValue(mlIdx));
View Full Code Here


        assertThat(scorer.score(15, 30), equalTo(5.0));
    }

    @Test
    public void testMultiple() {
        MutableSparseVector msv = MutableSparseVector.create(10, 30, 39);
        scorer.score(42, msv);
        assertThat(msv.size(), equalTo(2));
        assertThat(msv.get(39), equalTo(3.5));
        assertThat(msv.getChannel(FallbackItemScorer.SCORE_SOURCE_SYMBOL).get(39),
                   equalTo(ScoreSource.PRIMARY));
        assertThat(msv.get(30), equalTo(4.0));
        assertThat(msv.getChannel(FallbackItemScorer.SCORE_SOURCE_SYMBOL).get(30),
                   equalTo(ScoreSource.BASELINE));
        assertThat(msv.unsetKeySet(), contains(10L));
    }
View Full Code Here

                .addScore(42, 11, 4.8)
                .addScore(42, 12, 8.2)
                .build();

        OrdRecRatingPredictor ordrec = new OrdRecRatingPredictor(scorer, userDAO, qtz);
        MutableSparseVector predictItem = MutableSparseVector.create(10, 11, 12);
        ordrec.predict(42, predictItem);
        assertThat(predictItem.get(10), equalTo(1.0));
        assertThat(predictItem.get(11), equalTo(2.0));
        assertThat(predictItem.get(12), equalTo(3.0));

    }
View Full Code Here

                .addScore(42, 11, 1.9)
                .addScore(42, 12, 3.1)
                .build();

        OrdRecRatingPredictor ordrec = new OrdRecRatingPredictor(scorer, userDAO, qtz);
        MutableSparseVector predictItem = MutableSparseVector.create(10, 11, 12);
        ordrec.predict(42, predictItem);
        assertThat(predictItem.get(10), equalTo(1.0));
        assertThat(predictItem.get(11), equalTo(2.0));
        assertThat(predictItem.get(12), equalTo(3.0));

    }
View Full Code Here

                .addScore(42, 12, 1.75)
                .build();


        OrdRecRatingPredictor ordrec = new OrdRecRatingPredictor(scorer, userDAO, qtz);
        MutableSparseVector predictItem = MutableSparseVector.create(10, 11, 12);
        ordrec.predict(42, predictItem);
        assertThat(predictItem.get(10), equalTo(1.0));
        assertThat(predictItem.get(11), equalTo(2.0));
        assertThat(predictItem.get(12), equalTo(3.0));

    }
View Full Code Here

public class IndexerTest {

    @Test
    public void testConvertArrayToVector(){
        Indexer ind = new Indexer();
        MutableSparseVector vector = MutableSparseVector.create();
        double[] values = {1.0, 2.0, 3.0};
        assertThat(ind.getObjectCount(), equalTo(0));
       
        ind.internId(0);
        ind.internId(1);
        ind.internId(2);
        assertThat(ind.getObjectCount(), equalTo(3));
       
        vector = ind.convertArrayToVector(values);
        assertThat(vector.get(0), equalTo(1.0));
        assertThat(vector.get(1), equalTo(2.0));
        assertThat(vector.get(2), equalTo(3.0));
    }
View Full Code Here

        if (history == null) {
            history = History.forUser(user);
        }
        SparseVector summary = summarizer.summarize(history);
        VectorTransformation transform = normalizer.makeTransformation(user, summary);
        MutableSparseVector normed = summary.mutableCopy();
        transform.apply(normed);

        scores.clear();
        algorithm.scoreItems(model, normed, scores, scorer);
View Full Code Here

                    logger.trace("processing {} ratings for item {}", item.size(), item);
                }
                List<Rating> ratings = FluentIterable.from(item)
                                                     .filter(Rating.class)
                                                     .toList();
                MutableSparseVector vector = Ratings.itemRatingVector(ratings);
                normalizer.normalize(item.getItemId(), vector, vector);
                for (VectorEntry e: vector) {
                    long user = e.getKey();
                    LongList uis = userItems.get(user);
                    if (uis == null) {
                        // lists are nice and fast, we only see each item once
                        uis = new LongArrayList();
                        userItems.put(user, uis);
                    }
                    uis.add(item.getItemId());
                }
                itemVectors.put(item.getItemId(), vector.freeze());
            }
        } finally {
            itemCursor.close();
        }
View Full Code Here

        public UserSnapshot get() {
            Long2ObjectMap<MutableSparseVector> vectors = new Long2ObjectOpenHashMap<MutableSparseVector>();
            Cursor<? extends UserHistory<? extends Event>> users = userEventDAO.streamEventsByUser(summarizer.eventTypeWanted());
            try {
                for (UserHistory<? extends Event> user: users) {
                    MutableSparseVector uvec = summarizer.summarize(user).mutableCopy();
                    vectors.put(user.getUserId(), uvec);
                }
            } finally {
                users.close();
            }

            Long2ObjectMap<LongList> itemUserLists = new Long2ObjectOpenHashMap<LongList>();
            LongKeyDomain domain = LongKeyDomain.fromCollection(vectors.keySet()).compactCopy(true);
            assert domain.size() == domain.domainSize();
            ImmutableList.Builder<ImmutableSparseVector> vecs = ImmutableList.builder();
            ImmutableList.Builder<ImmutableSparseVector> nvecs = ImmutableList.builder();
            for (LongIterator uiter = domain.activeSetView().iterator(); uiter.hasNext();) {
                final long user = uiter.nextLong();
                MutableSparseVector vec = vectors.get(user);
                // save user's original vector
                ImmutableSparseVector userVector = vec.immutable();
                vecs.add(userVector);
                // normalize user vector
                normalizer.normalize(user, userVector, vec);
                // and save normalized vector
                nvecs.add(vec.immutable());
                for (LongIterator iiter = userVector.keySet().iterator(); iiter.hasNext();) {
                    final long item = iiter.nextLong();
                    LongList itemUsers = itemUserLists.get(item);
                    if (itemUsers == null) {
                        itemUsers = new LongArrayList();
View Full Code Here

        Long2ObjectMap<? extends Collection<Neighbor>> neighborhoods =
                findNeighbors(history, scores.keyDomain());
        Long2ObjectMap<SparseVector> normedUsers =
                normalizeNeighborRatings(neighborhoods.values());

        MutableSparseVector sizeChan = scores.addChannelVector(NEIGHBORHOOD_SIZE_SYMBOL);
        MutableSparseVector weightChan = scores.addChannelVector(NEIGHBORHOOD_WEIGHT_SYMBOL);
        for (VectorEntry e : scores.view(VectorEntry.State.EITHER)) {
            final long item = e.getKey();
            double sum = 0;
            double weight = 0;
            int count = 0;
            Collection<Neighbor> nbrs = neighborhoods.get(item);
            if (nbrs != null) {
                for (Neighbor n : nbrs) {
                    weight += abs(n.similarity);
                    sum += n.similarity * normedUsers.get(n.user).get(item);
                    count += 1;
                }
            }
           
            if (count >= minNeighborCount) {
                if (logger.isTraceEnabled()) {
                    logger.trace("Total neighbor weight for item {} is {} from {} neighbors",
                                 item, weight, count);
                }
                scores.set(e, sum / weight);
            } else {
                scores.unset(e);
            }
            sizeChan.set(e, count);
            weightChan.set(e,weight);
        }

        // Denormalize and return the results
        SparseVector urv = RatingVectorUserHistorySummarizer.makeRatingVector(history);
        VectorTransformation vo = normalizer.makeTransformation(history.getUserId(), urv);
View Full Code Here

TOP

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

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.