Package org.grouplens.lenskit.vectors

Examples of org.grouplens.lenskit.vectors.MutableSparseVector


     * @param offset  An offset to subtract from each value prior to averaging.
     * @param damping The damping term (see {@link #idMeanOffsets(double)}).
     * @return The vector of means.
     */
    public ImmutableSparseVector computeIdMeans(double offset, double damping) {
        MutableSparseVector v = MutableSparseVector.create(sums);
        for (VectorEntry e: v) {
            final int n = counts.get(e.getKey());
            // if n <= 0, how did we get this item?
            assert n > 0;
            // compute a total to subtract offset from each accumulated value
            final double noff = n * offset;
            v.set(e, (e.getValue() - noff) / (n + damping));
        }
        return v.freeze();
    }
View Full Code Here


        keySet = LongUtils.packedSet(1, 3, 4, 6);
    }

    @Test
    public void testScale() {
        MutableSparseVector v = MutableSparseVector.create(keySet);
        v.set(1, 1);
        v.set(4, 1);
        assertThat(norm.normalize(v.immutable(), v), sameInstance(v));
        assertThat(v.norm(), closeTo(1, 1.0e-6));
        assertThat(v.size(), equalTo(2));
        assertThat(v.get(1), closeTo(1 / sqrt(2), 1.0e-6));
        assertThat(v.get(4), closeTo(1 / sqrt(2), 1.0e-6));
    }
View Full Code Here

        assertThat(v.get(4), closeTo(1 / sqrt(2), 1.0e-6));
    }

    @Test
    public void testScaleOther() {
        MutableSparseVector v = MutableSparseVector.create(keySet);
        v.set(1, 1);
        v.set(4, 1);
        MutableSparseVector ref = MutableSparseVector.create(keySet);
        ref.set(1, 1);
        ref.set(6, 1);
        ref.set(3, 2);

        VectorTransformation tx = norm.makeTransformation(ref.immutable());
        assertThat(tx.apply(v), sameInstance(v));
        assertThat(v.norm(), closeTo(sqrt(2.0 / 6), 1.0e-6));
        assertThat(v.size(), equalTo(2));
        assertThat(v.get(1), closeTo(1 / sqrt(6), 1.0e-6));
        assertThat(v.get(4), closeTo(1 / sqrt(6), 1.0e-6));
View Full Code Here

public class MeanCenteringVectorNormalizerTest {
    VectorNormalizer norm = new MeanCenteringVectorNormalizer();

    @Test
    public void testEmptyReference() {
        MutableSparseVector msv = MutableSparseVector.create(4L);
        msv.set(4, 3.5);
        norm.normalize(SparseVector.empty(), msv);
        assertThat(msv.get(4), closeTo(3.5, 1.0e-5));
    }
View Full Code Here

        assertThat(msv.get(4), closeTo(3.5, 1.0e-5));
    }

    @Test
    public void testSameReference() {
        MutableSparseVector msv = MutableSparseVector.create(4L);
        msv.set(4, 3.5);
        norm.normalize(msv, msv);
        assertThat(msv.get(4), closeTo(0, 1.0e-5));
    }
View Full Code Here

        assertThat(msv.get(4), closeTo(0, 1.0e-5));
    }

    @Test
    public void testTransform() {
        MutableSparseVector reference = MutableSparseVector.create(4L, 5L);
        reference.set(4, 3.5);
        reference.set(5, 2.5);
        VectorTransformation tx = norm.makeTransformation(reference);

        MutableSparseVector msv = reference.mutableCopy();
        tx.apply(msv);
        assertThat(msv.get(4), closeTo(0.5, 1.0e-5));
        assertThat(msv.get(5), closeTo(-0.5, 1.0e-5));

        msv.set(4, 2);
        tx.unapply(msv);
        assertThat(msv.get(4), closeTo(5, 1.0e-5));
    }
View Full Code Here

    @Test
    public void testMakeTransformation() {
        MeanVarianceNormalizer urvn;
        urvn = new MeanVarianceNormalizer();
        VectorTransformation trans = urvn.makeTransformation(userRatings);
        MutableSparseVector nUR = userRatings.mutableCopy();
        final double mean = 2.0;
        final double stdev = Math.sqrt(8.0 / 3.0);
        trans.apply(nUR);
        //Test apply
        Assert.assertEquals((0.0 - mean) / stdev, nUR.get(0L), MIN_DOUBLE_PRECISION);
        Assert.assertEquals((2.0 - mean) / stdev, nUR.get(1L), MIN_DOUBLE_PRECISION);
        Assert.assertEquals((4.0 - mean) / stdev, nUR.get(2L), MIN_DOUBLE_PRECISION);
        trans.unapply(nUR);
        //Test unapply
        Assert.assertEquals(0.0, nUR.get(0L), MIN_DOUBLE_PRECISION);
        Assert.assertEquals(2.0, nUR.get(1L), MIN_DOUBLE_PRECISION);
        Assert.assertEquals(4.0, nUR.get(2L), MIN_DOUBLE_PRECISION);
    }
View Full Code Here

    @Test
    public void testUniformRatings() {
        MeanVarianceNormalizer urvn;
        urvn = new MeanVarianceNormalizer();
        VectorTransformation trans = urvn.makeTransformation(uniformUserRatings);
        MutableSparseVector nUR = userRatings.mutableCopy();
        trans.apply(nUR);
        //Test apply
        Assert.assertEquals(0.0, nUR.get(0L), MIN_DOUBLE_PRECISION);
        Assert.assertEquals(0.0, nUR.get(1L), MIN_DOUBLE_PRECISION);
        Assert.assertEquals(0.0, nUR.get(2L), MIN_DOUBLE_PRECISION);
        trans.unapply(nUR);
        //Test unapply
        Assert.assertEquals(2.0, nUR.get(0L), MIN_DOUBLE_PRECISION);
        Assert.assertEquals(2.0, nUR.get(1L), MIN_DOUBLE_PRECISION);
        Assert.assertEquals(2.0, nUR.get(2L), MIN_DOUBLE_PRECISION);
    }
View Full Code Here

    @Test
    public void testSmoothingDetailed() {
        MeanVarianceNormalizer urvn = new MeanVarianceNormalizer.Builder(dao, 3.0).get();

        VectorTransformation trans = urvn.makeTransformation(userRatings);
        MutableSparseVector nUR = userRatings.mutableCopy();
        final double mean = 2.0;
        final double stdev = Math.sqrt(7.0 / 3.0);
        trans.apply(nUR);
        //Test apply
        Assert.assertEquals((0.0 - mean) / stdev, nUR.get(0L), MIN_DOUBLE_PRECISION);
        Assert.assertEquals((2.0 - mean) / stdev, nUR.get(1L), MIN_DOUBLE_PRECISION);
        Assert.assertEquals((4.0 - mean) / stdev, nUR.get(2L), MIN_DOUBLE_PRECISION);
        trans.unapply(nUR);
        //Test unapply
        Assert.assertEquals(0.0, nUR.get(0L), MIN_DOUBLE_PRECISION);
        Assert.assertEquals(2.0, nUR.get(1L), MIN_DOUBLE_PRECISION);
        Assert.assertEquals(4.0, nUR.get(2L), MIN_DOUBLE_PRECISION);
    }
View Full Code Here

    private final Symbol bazSym = Symbol.of("baz");
    private final TypedSymbol<String> tsym = TypedSymbol.of(String.class, "test.wombat");
   
    @Test
    public void testSparseVectorScoredIds() {
        MutableSparseVector sv = MutableSparseVector.create(LongUtils.packedSet(1,2,4));
        sv.set(1,1.0);
        sv.set(4,16.0);
       
        MutableSparseVector foo = sv.addChannelVector(fooSym);
        foo.set(1,2.0);
        foo.set(4,5.0);
       
        MutableSparseVector bar = sv.addChannelVector(barSym);
        bar.set(1,3.0);
       
        MutableSparseVector baz = sv.addChannelVector(bazSym);
        baz.set(2, 100.0);

        Long2ObjectMap<String> wombat = sv.addChannel(tsym);
        wombat.put(1, "hello");
        wombat.put(4, "goodbye");
       
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.