public List <Suggestion> GetSuggest(UserBehavior db, long userId) { IRater rater = new SimpleRater(); IComparer comparer = new CorrelationUserComparer(); recommender = new ItemCollaborativeFilterRecommender(comparer, rater, 50); recommender.Train(db); var suggestion = recommender.GetSuggestions(userId, 500); return(suggestion); }
public static TestResults Test(this IRecommender classifier, UserBehaviorDatabase db, int numSuggestions) { // We're only using the ratings to check for existence of a rating, so we can use a simple rater for everything SimpleRater rater = new SimpleRater(); UserBehaviorTransformer ubt = new UserBehaviorTransformer(db); UserArticleRatingsTable ratings = ubt.GetUserArticleRatingsTable(rater); int correctUsers = 0; double averagePrecision = 0.0; double averageRecall = 0.0; // Get a list of users in this database who interacted with an article for the first time List <int> distinctUsers = db.UserActions.Select(x => x.UserID).Distinct().ToList(); var distinctUserArticles = db.UserActions.GroupBy(x => new { x.UserID, x.ArticleID }); // Now get suggestions for each of these users foreach (int user in distinctUsers) { List <Suggestion> suggestions = classifier.GetSuggestions(user, numSuggestions); bool foundOne = false; int userIndex = ratings.UserIndexToID.IndexOf(user); int userCorrectArticles = 0; int userTotalArticles = distinctUserArticles.Count(x => x.Key.UserID == user); foreach (Suggestion s in suggestions) { int articleIndex = ratings.ArticleIndexToID.IndexOf(s.ArticleID); // If one of the top N suggestions is what the user ended up reading, then we're golden if (ratings.Users[userIndex].ArticleRatings[articleIndex] != 0) { userCorrectArticles++; if (!foundOne) { correctUsers++; foundOne = true; } } } averagePrecision += (double)userCorrectArticles / numSuggestions; averageRecall += (double)userCorrectArticles / userTotalArticles; } averagePrecision /= distinctUsers.Count; averageRecall /= distinctUsers.Count; return(new TestResults(distinctUsers.Count, correctUsers, averageRecall, averagePrecision)); }