public void testRecommender()
        {
            IRecommender             recommender = new RandomRecommender(getDataModel());
            IList <IRecommendedItem> recommended = recommender.Recommend(1, 1);

            Assert.NotNull(recommended);
            Assert.AreEqual(1, recommended.Count);
            IRecommendedItem firstRecommended = recommended[0];

            Assert.AreEqual(2, firstRecommended.GetItemID());
            recommender.Refresh(null);
            Assert.AreEqual(2, firstRecommended.GetItemID());
        }
Exemple #2
0
        public void testRecommender()
        {
            IRecommender             recommender = new ItemAverageRecommender(getDataModel());
            IList <IRecommendedItem> recommended = recommender.Recommend(1, 1);

            Assert.NotNull(recommended);
            Assert.AreEqual(1, recommended.Count);
            IRecommendedItem firstRecommended = recommended[0];

            Assert.AreEqual(2, firstRecommended.GetItemID());
            Assert.AreEqual(0.53333336f, firstRecommended.GetValue(), EPSILON);
            recommender.Refresh(null);
            Assert.AreEqual(2, firstRecommended.GetItemID());
            Assert.AreEqual(0.53333336f, firstRecommended.GetValue(), EPSILON);
        }
Exemple #3
0
        public void testRecommender()
        {
            IRecommender             recommender = buildRecommender();
            IList <IRecommendedItem> recommended = recommender.Recommend(1, 1);

            Assert.NotNull(recommended);
            Assert.AreEqual(1, recommended.Count);
            IRecommendedItem firstRecommended = recommended[0];

            Assert.AreEqual(2, firstRecommended.GetItemID());
            Assert.AreEqual(0.1f, firstRecommended.GetValue(), EPSILON);
            recommender.Refresh(null);
            Assert.AreEqual(2, firstRecommended.GetItemID());
            Assert.AreEqual(0.1f, firstRecommended.GetValue(), EPSILON);
        }
Exemple #4
0
        public override bool Equals(object o)
        {
            if (!(o is GenericRecommendedItem))
            {
                return(false);
            }
            IRecommendedItem other = (IRecommendedItem)o;

            return(itemID == other.GetItemID() && value == other.GetValue());
        }
        public void testMostSimilarToMultipleExcludeIfNotSimilarToAll()
        {
            IItemBasedRecommender   recommender = buildRecommender2();
            List <IRecommendedItem> similar     = recommender.MostSimilarItems(new long[] { 3, 4 }, 2);

            Assert.NotNull(similar);
            Assert.AreEqual(1, similar.Count);
            IRecommendedItem first = similar[0];

            Assert.AreEqual(0, first.GetItemID());
            Assert.AreEqual(0.2f, first.GetValue(), EPSILON);
        }
Exemple #6
0
        public void testBestRating()
        {
            IRecommender             recommender = buildRecommender();
            IList <IRecommendedItem> recommended = recommender.Recommend(1, 1);

            Assert.NotNull(recommended);
            Assert.AreEqual(1, recommended.Count);
            IRecommendedItem firstRecommended = recommended[0];

            // item one should be recommended because it has a greater rating/score
            Assert.AreEqual(2, firstRecommended.GetItemID());
            Assert.AreEqual(0.1f, firstRecommended.GetValue(), EPSILON);
        }
        public void testMostSimilarToMultipleDontExcludeIfNotSimilarToAll()
        {
            IItemBasedRecommender   recommender = buildRecommender2();
            List <IRecommendedItem> similar     = recommender.MostSimilarItems(new long[] { 1, 2, 4 }, 10, false);

            Assert.NotNull(similar);
            Assert.AreEqual(2, similar.Count);
            IRecommendedItem first  = similar[0];
            IRecommendedItem second = similar[1];

            Assert.AreEqual(0, first.GetItemID());
            Assert.AreEqual(0.933333333f, first.GetValue(), EPSILON);
            Assert.AreEqual(3, second.GetItemID());
            Assert.AreEqual(-0.2f, second.GetValue(), EPSILON);
        }
        public void testMostSimilarToMultiple()
        {
            IItemBasedRecommender   recommender = buildRecommender2();
            List <IRecommendedItem> similar     = recommender.MostSimilarItems(new long[] { 0, 1 }, 2);

            Assert.NotNull(similar);
            Assert.AreEqual(2, similar.Count);
            IRecommendedItem first  = similar[0];
            IRecommendedItem second = similar[1];

            Assert.AreEqual(2, first.GetItemID());
            Assert.AreEqual(0.85f, first.GetValue(), EPSILON);
            Assert.AreEqual(3, second.GetItemID());
            Assert.AreEqual(-0.3f, second.GetValue(), EPSILON);
        }
        public void testMostSimilar()
        {
            IItemBasedRecommender   recommender = buildRecommender();
            List <IRecommendedItem> similar     = recommender.MostSimilarItems(0, 2);

            Assert.NotNull(similar);
            Assert.AreEqual(2, similar.Count);
            IRecommendedItem first  = similar[0];
            IRecommendedItem second = similar[1];

            Assert.AreEqual(1, first.GetItemID());
            Assert.AreEqual(1.0f, first.GetValue(), EPSILON);
            Assert.AreEqual(2, second.GetItemID());
            Assert.AreEqual(0.5f, second.GetValue(), EPSILON);
        }
        public void testRecommendedBecause()
        {
            IItemBasedRecommender   recommender        = buildRecommender2();
            List <IRecommendedItem> recommendedBecause = recommender.RecommendedBecause(1, 4, 3);

            Assert.NotNull(recommendedBecause);
            Assert.AreEqual(3, recommendedBecause.Count);
            IRecommendedItem first  = recommendedBecause[0];
            IRecommendedItem second = recommendedBecause[1];
            IRecommendedItem third  = recommendedBecause[2];

            Assert.AreEqual(2, first.GetItemID());
            Assert.AreEqual(0.99f, first.GetValue(), EPSILON);
            Assert.AreEqual(3, second.GetItemID());
            Assert.AreEqual(0.4f, second.GetValue(), EPSILON);
            Assert.AreEqual(0, third.GetItemID());
            Assert.AreEqual(0.2f, third.GetValue(), EPSILON);
        }
Exemple #11
0
        public IRStatistics Evaluate(IRecommenderBuilder recommenderBuilder,
                                     IDataModelBuilder dataModelBuilder,
                                     IDataModel dataModel,
                                     IDRescorer rescorer,
                                     int at,
                                     double relevanceThreshold,
                                     double evaluationPercentage)
        {
            //Preconditions.checkArgument(recommenderBuilder != null, "recommenderBuilder is null");
            //Preconditions.checkArgument(dataModel != null, "dataModel is null");
            //Preconditions.checkArgument(at >= 1, "at must be at least 1");
            //Preconditions.checkArgument(evaluationPercentage > 0.0 && evaluationPercentage <= 1.0,
            //    "Invalid evaluationPercentage: " + evaluationPercentage + ". Must be: 0.0 < evaluationPercentage <= 1.0");

            int             numItems  = dataModel.GetNumItems();
            IRunningAverage precision = new FullRunningAverage();
            IRunningAverage recall    = new FullRunningAverage();
            IRunningAverage fallOut   = new FullRunningAverage();
            IRunningAverage nDCG      = new FullRunningAverage();
            int             numUsersRecommendedFor      = 0;
            int             numUsersWithRecommendations = 0;

            var it = dataModel.GetUserIDs();

            while (it.MoveNext())
            {
                long userID = it.Current;

                if (random.nextDouble() >= evaluationPercentage)
                {
                    // Skipped
                    continue;
                }

                var stopWatch = new System.Diagnostics.Stopwatch();
                stopWatch.Start();

                IPreferenceArray prefs = dataModel.GetPreferencesFromUser(userID);

                // List some most-preferred items that would count as (most) "relevant" results
                double    theRelevanceThreshold = Double.IsNaN(relevanceThreshold) ? computeThreshold(prefs) : relevanceThreshold;
                FastIDSet relevantItemIDs       = dataSplitter.GetRelevantItemsIDs(userID, at, theRelevanceThreshold, dataModel);

                int numRelevantItems = relevantItemIDs.Count();
                if (numRelevantItems <= 0)
                {
                    continue;
                }

                FastByIDMap <IPreferenceArray> trainingUsers = new FastByIDMap <IPreferenceArray>(dataModel.GetNumUsers());
                var it2 = dataModel.GetUserIDs();
                while (it2.MoveNext())
                {
                    dataSplitter.ProcessOtherUser(userID, relevantItemIDs, trainingUsers, it2.Current, dataModel);
                }

                IDataModel trainingModel = dataModelBuilder == null ? new GenericDataModel(trainingUsers)
          : dataModelBuilder.BuildDataModel(trainingUsers);
                try {
                    trainingModel.GetPreferencesFromUser(userID);
                } catch (NoSuchUserException nsee) {
                    continue; // Oops we excluded all prefs for the user -- just move on
                }

                int size = numRelevantItems + trainingModel.GetItemIDsFromUser(userID).Count();
                if (size < 2 * at)
                {
                    // Really not enough prefs to meaningfully evaluate this user
                    continue;
                }

                IRecommender recommender = recommenderBuilder.BuildRecommender(trainingModel);

                int intersectionSize = 0;
                var recommendedItems = recommender.Recommend(userID, at, rescorer);
                foreach (IRecommendedItem recommendedItem in recommendedItems)
                {
                    if (relevantItemIDs.Contains(recommendedItem.GetItemID()))
                    {
                        intersectionSize++;
                    }
                }

                int numRecommendedItems = recommendedItems.Count;

                // Precision
                if (numRecommendedItems > 0)
                {
                    precision.AddDatum((double)intersectionSize / (double)numRecommendedItems);
                }

                // Recall
                recall.AddDatum((double)intersectionSize / (double)numRelevantItems);

                // Fall-out
                if (numRelevantItems < size)
                {
                    fallOut.AddDatum((double)(numRecommendedItems - intersectionSize)
                                     / (double)(numItems - numRelevantItems));
                }

                // nDCG
                // In computing, assume relevant IDs have relevance 1 and others 0
                double cumulativeGain = 0.0;
                double idealizedGain  = 0.0;
                for (int i = 0; i < numRecommendedItems; i++)
                {
                    IRecommendedItem item     = recommendedItems[i];
                    double           discount = 1.0 / log2(i + 2.0); // Classical formulation says log(i+1), but i is 0-based here
                    if (relevantItemIDs.Contains(item.GetItemID()))
                    {
                        cumulativeGain += discount;
                    }
                    // otherwise we're multiplying discount by relevance 0 so it doesn't do anything

                    // Ideally results would be ordered with all relevant ones first, so this theoretical
                    // ideal list starts with number of relevant items equal to the total number of relevant items
                    if (i < numRelevantItems)
                    {
                        idealizedGain += discount;
                    }
                }
                if (idealizedGain > 0.0)
                {
                    nDCG.AddDatum(cumulativeGain / idealizedGain);
                }

                // Reach
                numUsersRecommendedFor++;
                if (numRecommendedItems > 0)
                {
                    numUsersWithRecommendations++;
                }

                stopWatch.Stop();

                log.Info("Evaluated with user {} in {}ms", userID, stopWatch.ElapsedMilliseconds);
                log.Info("Precision/recall/fall-out/nDCG/reach: {} / {} / {} / {} / {}",
                         precision.GetAverage(), recall.GetAverage(), fallOut.GetAverage(), nDCG.GetAverage(),
                         (double)numUsersWithRecommendations / (double)numUsersRecommendedFor);
            }

            return(new IRStatisticsImpl(
                       precision.GetAverage(),
                       recall.GetAverage(),
                       fallOut.GetAverage(),
                       nDCG.GetAverage(),
                       (double)numUsersWithRecommendations / (double)numUsersRecommendedFor));
        }