public void TestMostSimilar() { ItemBasedRecommender recommender = buildRecommender(); IList <RecommendedItem> similar = recommender.MostSimilarItems("0", 2); Assert.IsNotNull(similar); Assert.AreEqual(2, similar.Count); RecommendedItem first = similar[0]; RecommendedItem second = similar[1]; Assert.AreEqual("1", first.Item.ID); Assert.AreEqual(1.0, first.Value, EPSILON); Assert.AreEqual("2", second.Item.ID); Assert.AreEqual(0.5, second.Value, EPSILON); }
public void testRecommendedBecause() { ItemBasedRecommender recommender = buildRecommender2(); IList <RecommendedItem> recommendedBecause = recommender.RecommendedBecause("test1", "4", 3); Assert.IsNotNull(recommendedBecause); Assert.AreEqual(3, recommendedBecause.Count); RecommendedItem first = recommendedBecause[0]; RecommendedItem second = recommendedBecause[1]; RecommendedItem third = recommendedBecause[2]; Assert.AreEqual("2", first.Item.ID); Assert.AreEqual(0.99, first.Value, EPSILON); Assert.AreEqual("3", second.Item.ID); Assert.AreEqual(0.4, second.Value, EPSILON); Assert.AreEqual("0", third.Item.ID); Assert.AreEqual(0.2, third.Value, EPSILON); }
public List <MovieViewModel> GetSuggestions(string userId, int numSuggestions, UserMovieRatingsTable table) { int userIndex = table.UserIndexToId.IndexOf(userId); UserMoviesRatings user = table.Users[userIndex]; List <Suggestion> suggestions = new List <Suggestion>(); List <UserMoviesRatings> neighbors = GetNearestNeighbors(userId, user, 5, table); if (neighbors.ToList()[0].Score <= 0.5) { ItemBasedRecommender itemBasedRecommender = new ItemBasedRecommender(_mapper, _movieRepository); return(itemBasedRecommender.GetSuggestions(userId, 10, table)); } for (int movieIndex = 0; movieIndex < table.MovieIndexToId.Count; movieIndex++) { if (user.MovieRatings[movieIndex] != 0) { double score = 0.0; int count = 0; for (var u = 0; u < neighbors.Count; u++) { if (neighbors[u].MovieRatings[movieIndex] != 0) { score += neighbors[u].MovieRatings[movieIndex] - ((u + 1.0) / 100.0); count++; } } if (count > 0) { score /= count; } suggestions.Add(new Suggestion(userId, table.MovieIndexToId[movieIndex], score)); } } suggestions.Sort((c, n) => n.Rating.CompareTo(c.Rating)); List <Suggestion> suggestionsList = suggestions.Take(numSuggestions).ToList(); List <MovieViewModel> movieViewModels = new List <MovieViewModel>(); foreach (var suggestion in suggestionsList) { Movie movie = _movieRepository.GetById(suggestion.MovieId); movieViewModels.Add(_mapper.Map <MovieViewModel>(movie)); } return(movieViewModels); }
public void TestMostSimilarToMultiple() { ItemBasedRecommender recommender = buildRecommender2(); List <Object> itemIDs = new List <Object>(2); itemIDs.Add("0"); itemIDs.Add("1"); IList <RecommendedItem> similar = recommender.MostSimilarItems(itemIDs, 2); Assert.IsNotNull(similar); Assert.AreEqual(2, similar.Count); RecommendedItem first = similar[0]; RecommendedItem second = similar[1]; Assert.AreEqual("2", first.Item.ID); Assert.AreEqual(0.85, first.Value, EPSILON); Assert.AreEqual("3", second.Item.ID); Assert.AreEqual(-0.3, second.Value, EPSILON); }
protected BatchItemSimilarities(ItemBasedRecommender recommender, int similarItemsPerItem) { this.recommender = recommender; this.similarItemsPerItem = similarItemsPerItem; }
/// @param recommender recommender to use /// @param similarItemsPerItem number of similar items to compute per item /// @param batchSize size of item batches sent to worker threads public MultithreadedBatchItemSimilarities(ItemBasedRecommender recommender, int similarItemsPerItem, int batchSize) : base(recommender, similarItemsPerItem) { this.batchSize = batchSize; }
/// @param recommender recommender to use /// @param similarItemsPerItem number of similar items to compute per item public MultithreadedBatchItemSimilarities(ItemBasedRecommender recommender, int similarItemsPerItem) : this(recommender, similarItemsPerItem, DEFAULT_BATCH_SIZE) { }