Package org.grouplens.lenskit.vectors

Examples of org.grouplens.lenskit.vectors.SparseVector


        return new Context(outputLayout.prefixTable(tableWriter, algo, ds));
    }

    @Override
    public Void doMeasureUser(TestUser user, Context context) {
        SparseVector ratings = user.getTestRatings();
        SparseVector predictions = user.getPredictions();
        if (predictions == null) {
            predictions = MutableSparseVector.create();
        }

        LongSortedSet items = ratings.keySet();
        if (!items.containsAll(predictions.keySet())) {
            items = LongUtils.setUnion(items, predictions.keySet());
        }

        logger.debug("outputting {} predictions for user {}", predictions.size(), user.getUserId());
        for (LongIterator iter = items.iterator(); iter.hasNext(); /* no increment */) {
            long item = iter.nextLong();
            Double rating = ratings.containsKey(item) ? ratings.get(item) : null;
            Double pred = predictions.containsKey(item) ? predictions.get(item) : null;
            try {
                List<Object> row = Lists.<Object>newArrayList(user.getUserId(), item, rating, pred);
                for (Pair<Symbol, String> chan: channels) {
                    SparseVector v = predictions.getChannelVector(chan.getLeft());
                    if (v != null && v.containsKey(item)) {
                        row.add(v.get(item));
                    } else {
                        row.add(null);
                    }
                }
                context.writer.writeRow(row);
View Full Code Here


        recommendations = user.getRecommendations(listSize, candidates, exclude);
        if (recommendations == null) {
            return null;
        }

        SparseVector ratings = user.getTestRatings();
        LongList ideal = ratings.keysByValue(true);
        if (ideal.size() > listSize) {
            ideal = ideal.subList(0, listSize);
        }
        double idealGain = computeDCG(ideal, ratings);
View Full Code Here

    public SlopeOneModel get() {
        LongSet items = buildContext.getItems();
        LongIterator outer = items.iterator();
        while (outer.hasNext()) {
            final long item1 = outer.nextLong();
            final SparseVector vec1 = buildContext.itemVector(item1);
            LongIterator inner = items.iterator();
            while (inner.hasNext()) {
                final long item2 = inner.nextLong();
                if (item1 != item2) {
                    SparseVector vec2 = buildContext.itemVector(item2);
                    accumulator.putItemPair(item1, vec1, item2, vec2);
                }
            }
        }
        return new SlopeOneModel(accumulator.buildMatrix());
View Full Code Here

    public void score(long uid, @Nonnull MutableSparseVector scores) {
        UserHistory<Rating> history = dao.getEventsForUser(uid, Rating.class);
        if (history == null) {
            history = History.forUser(uid);
        }
        SparseVector user = RatingVectorUserHistorySummarizer.makeRatingVector(history);

        for (VectorEntry e : scores.view(VectorEntry.State.EITHER)) {
            final long predicteeItem = e.getKey();
            if (!user.containsKey(predicteeItem)) {
                double total = 0;
                int nitems = 0;
                LongIterator ratingIter = user.keySet().iterator();
                while (ratingIter.hasNext()) {
                    long currentItem = ratingIter.nextLong();
                    int nusers = model.getCoratings(predicteeItem, currentItem);
                    if (nusers != 0) {
                        double currentDev = model.getDeviation(predicteeItem, currentItem);
                        total += currentDev + user.get(currentItem);
                        nitems++;
                    }
                }
                if (nitems != 0) {
                    double predValue = total / nitems;
View Full Code Here

    }

    @Test
    public void testMeanBaseline() {
        ItemScorer pred = makeGlobalMean();
        SparseVector pv = pred.score(10L, itemSet(2l));
        assertEquals(RATINGS_DAT_MEAN, pv.get(2l), 0.00001);
    }
View Full Code Here

    @Test
    public void testItemMeanBaseline() {
        ItemScorer pred = new ItemMeanRatingItemScorer.Builder(dao, 0.0).get();
        long[] items = {5, 7, 10};
        double[] values = {3, 6, 4};
        SparseVector map = MutableSparseVector.wrap(items, values).freeze();
        // unseen item, should be global mean
        assertThat(pred.score(10, 2),
                   closeTo(RATINGS_DAT_MEAN, 0.001));
        // seen item - should be item average
        assertThat(pred.score(10, 5),
View Full Code Here

    public double getDeviation(long item1, long item2) {
        if (item1 == item2) {
            return 0;
        } else if (item1 < item2) {
            SparseVector row = matrix.get(item1);
            if (row == null) {
                return Double.NaN;
            } else {
                return row.get(item2);
            }
        } else {
            SparseVector row = matrix.get(item2);
            if (row == null) {
                return Double.NaN;
            } else {
                return -row.get(item1);
            }
        }
    }
View Full Code Here

    public int getCoratings(long item1, long item2) {
        if (item1 == item2) {
            return 0;
        } else if (item1 < item2) {
            SparseVector row = matrix.get(item1);
            if (row == null) {
                return 0;
            } else {
                double coratings = row.getChannelVector(CORATINGS_SYMBOL).get(item2, 0);
                return (int) coratings;
            }
        } else {
            SparseVector row = matrix.get(item2);
            if (row == null) {
                return 0;
            } else {
                double coratings = row.getChannelVector(CORATINGS_SYMBOL).get(item1, 0);
                return (int) coratings;
            }
        }
    }
View Full Code Here

    public void score(long uid, @Nonnull MutableSparseVector scores) {
        UserHistory<Rating> history = dao.getEventsForUser(uid, Rating.class);
        if (history == null) {
            history = History.forUser(uid);
        }
        SparseVector ratings = RatingVectorUserHistorySummarizer.makeRatingVector(history);

        for (VectorEntry e : scores.view(VectorEntry.State.EITHER)) {
            final long predicteeItem = e.getKey();
            if (!ratings.containsKey(predicteeItem)) {
                double total = 0;
                int nusers = 0;
                LongIterator ratingIter = ratings.keySet().iterator();
                while (ratingIter.hasNext()) {
                    long currentItem = ratingIter.nextLong();
                    double currentDev = model.getDeviation(predicteeItem, currentItem);
                    if (!Double.isNaN(currentDev)) {
                        int weight = model.getCoratings(predicteeItem, currentItem);
                        total += (currentDev + ratings.get(currentItem)) * weight;
                        nusers += weight;
                    }
                }
                if (nusers == 0) {
                    scores.unset(e);
View Full Code Here

    }

    @Test
    public void testScoreSet() {
        ItemScorer pred = new ConstantItemScorer(5);
        SparseVector v = pred.score(42, LongUtils.packedSet(1, 2, 3, 5, 7));
        assertThat(v.keySet(), contains(1L, 2L, 3L, 5L, 7L));
        assertThat(v.values(), everyItem(equalTo(5.0)));
    }
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.