Ejemplo n.º 1
0
        public void testCorrelation()
        {
            IDataModel dataModel = getDataModel(
                new long[] { 1, 2, 3, 4, 5 },
                new Double?[][] {
                new double?[] { 1.0, 1.0 },
                new double?[] { 1.0, null, 1.0 },
                new double?[] { null, null, 1.0, 1.0, 1.0 },
                new double?[] { 1.0, 1.0, 1.0, 1.0, 1.0 },
                new double?[] { null, 1.0, 1.0, 1.0, 1.0 },
            });

            LogLikelihoodSimilarity similarity = new LogLikelihoodSimilarity(dataModel);

            assertCorrelationEquals(0.12160727029227925, similarity.ItemSimilarity(1, 0));
            assertCorrelationEquals(0.12160727029227925, similarity.ItemSimilarity(0, 1));

            assertCorrelationEquals(0.5423213660693732, similarity.ItemSimilarity(1, 2));
            assertCorrelationEquals(0.5423213660693732, similarity.ItemSimilarity(2, 1));

            assertCorrelationEquals(0.6905400104897509, similarity.ItemSimilarity(2, 3));
            assertCorrelationEquals(0.6905400104897509, similarity.ItemSimilarity(3, 2));

            assertCorrelationEquals(0.8706358464330881, similarity.ItemSimilarity(3, 4));
            assertCorrelationEquals(0.8706358464330881, similarity.ItemSimilarity(4, 3));
        }
        public void testCorrelation()
        {
            IDataModel dataModel = getDataModel(
            new long[] {1, 2, 3, 4, 5},
            new Double?[][] {
                    new double?[]{1.0, 1.0},
                    new double?[]{1.0, null, 1.0},
                    new double?[]{null, null, 1.0, 1.0, 1.0},
                    new double?[]{1.0, 1.0, 1.0, 1.0, 1.0},
                    new double?[]{null, 1.0, 1.0, 1.0, 1.0},
            });

            LogLikelihoodSimilarity similarity = new LogLikelihoodSimilarity(dataModel);

            assertCorrelationEquals(0.12160727029227925, similarity.ItemSimilarity(1, 0));
            assertCorrelationEquals(0.12160727029227925, similarity.ItemSimilarity(0, 1));

            assertCorrelationEquals(0.5423213660693732, similarity.ItemSimilarity(1, 2));
            assertCorrelationEquals(0.5423213660693732, similarity.ItemSimilarity(2, 1));

            assertCorrelationEquals(0.6905400104897509, similarity.ItemSimilarity(2, 3));
            assertCorrelationEquals(0.6905400104897509, similarity.ItemSimilarity(3, 2));

            assertCorrelationEquals(0.8706358464330881, similarity.ItemSimilarity(3, 4));
            assertCorrelationEquals(0.8706358464330881, similarity.ItemSimilarity(4, 3));
        }
        public ActionResult Recommend(string filmIdsJson)
        {
            var filmIds = (new JavaScriptSerializer()).Deserialize<long[]>(filmIdsJson);
            var pathToDataFile =
                    Path.Combine(System.Web.HttpRuntime.AppDomainAppPath, "data/albums.dat");

            if (dataModel == null) {
                try
                {
                    dataModel = new FileDataModel(pathToDataFile, false, FileDataModel.DEFAULT_MIN_RELOAD_INTERVAL_MS, false);
                }
                catch (Exception e)
                {
                    var exe = e.ToString();
                }
            }

            var plusAnonymModel = new PlusAnonymousUserDataModel(dataModel);
            var prefArr = new GenericUserPreferenceArray(filmIds.Length);
            prefArr.SetUserID(0, PlusAnonymousUserDataModel.TEMP_USER_ID);
            for (int i = 0; i < filmIds.Length; i++) {
                prefArr.SetItemID(i, filmIds[i]);
                prefArr.SetValue(i, 5); // lets assume max rating
            }
            plusAnonymModel.SetTempPrefs(prefArr);

            var similarity = new LogLikelihoodSimilarity(plusAnonymModel);
            var neighborhood = new NearestNUserNeighborhood(15, similarity, plusAnonymModel);
            var recommender = new GenericBooleanPrefUserBasedRecommender(plusAnonymModel, neighborhood, similarity);
            var recommendedItems = recommender.Recommend(PlusAnonymousUserDataModel.TEMP_USER_ID, 5, null);
            return Json( recommendedItems.Select(ri => new Dictionary<string, object>() {
                {"id", ri.GetItemID() },
                {"rating", ri.GetValue() },
            }).ToArray() );
        }
Ejemplo n.º 4
0
        public void testNoSimilarity()
        {
            IDataModel dataModel = getDataModel(
                new long[] { 1, 2, 3, 4 },
                new Double?[][] {
                new double?[] { 1.0, null, 1.0, 1.0 },
                new double?[] { 1.0, null, 1.0, 1.0 },
                new double?[] { null, 1.0, 1.0, 1.0 },
                new double?[] { null, 1.0, 1.0, 1.0 },
            });

            LogLikelihoodSimilarity similarity = new LogLikelihoodSimilarity(dataModel);

            assertCorrelationEquals(Double.NaN, similarity.ItemSimilarity(1, 0));
            assertCorrelationEquals(Double.NaN, similarity.ItemSimilarity(0, 1));

            assertCorrelationEquals(0.0, similarity.ItemSimilarity(2, 3));
            assertCorrelationEquals(0.0, similarity.ItemSimilarity(3, 2));
        }
        public void testNoSimilarity()
        {
            IDataModel dataModel = getDataModel(
            new long[] {1, 2, 3, 4},
            new Double?[][] {
                new double?[]{1.0, null, 1.0, 1.0},
                new double?[]{1.0, null, 1.0, 1.0},
                new double?[]{null, 1.0, 1.0, 1.0},
                new double?[]{null, 1.0, 1.0, 1.0},
            });

            LogLikelihoodSimilarity similarity = new LogLikelihoodSimilarity(dataModel);

            assertCorrelationEquals(Double.NaN, similarity.ItemSimilarity(1, 0));
            assertCorrelationEquals(Double.NaN, similarity.ItemSimilarity(0, 1));

            assertCorrelationEquals(0.0, similarity.ItemSimilarity(2, 3));
            assertCorrelationEquals(0.0, similarity.ItemSimilarity(3, 2));
        }