public void TestIUF() { List<User> users = new List<User>(5); users.Add(GetUser("test1", 0.1)); users.Add(GetUser("test2", 0.2, 0.3)); users.Add(GetUser("test3", 0.4, 0.5, 0.6)); users.Add(GetUser("test4", 0.7, 0.8, 0.9, 1.0)); users.Add(GetUser("test5", 1.0, 1.0, 1.0, 1.0, 1.0)); GenericDataModel dummy = new GenericDataModel(users); InverseUserFrequency iuf = new InverseUserFrequency(dummy, 10.0); User user = dummy.GetUser("test5"); for (int i = 0; i < 5; i++) { Preference pref = user.GetPreferenceFor(i.ToString()); Assert.IsNotNull(pref); Assert.AreEqual(Math.Log(5.0 / (double)(5 - i)) / Math.Log(iuf.LogBase), iuf.GetTransformedValue(pref), EPSILON); } // Make sure this doesn't throw an exception iuf.Refresh(); }
public IRStatistics Evaluate(RecommenderBuilder recommenderBuilder, DataModel dataModel, int at, double relevanceThreshold, double evaluationPercentage) { if (recommenderBuilder == null) { throw new ArgumentNullException("recommenderBuilder is null"); } if (dataModel == null) { throw new ArgumentNullException("dataModel is null"); } if (at < 1) { throw new ArgumentException("at must be at least 1"); } if (double.IsNaN(evaluationPercentage) || evaluationPercentage <= 0.0 || evaluationPercentage > 1.0) { throw new ArgumentException("Invalid evaluationPercentage: " + evaluationPercentage); } if (double.IsNaN(relevanceThreshold)) { throw new ArgumentException("Invalid relevanceThreshold: " + evaluationPercentage); } RunningAverage precision = new FullRunningAverage(); RunningAverage recall = new FullRunningAverage(); foreach (User user in dataModel.GetUsers()) { Object id = user.ID; if (random.NextDouble() < evaluationPercentage) { ICollection<Item> relevantItems = new HashedSet<Item>(/* at */); Preference[] prefs = user.GetPreferencesAsArray(); foreach (Preference pref in prefs) { if (pref.Value >= relevanceThreshold) { relevantItems.Add(pref.Item); } } int numRelevantItems = relevantItems.Count; if (numRelevantItems > 0) { ICollection<User> trainingUsers = new List<User>(dataModel.GetNumUsers()); foreach (User user2 in dataModel.GetUsers()) { if (id.Equals(user2.ID)) { ICollection<Preference> trainingPrefs = new List<Preference>(); prefs = user2.GetPreferencesAsArray(); foreach (Preference pref in prefs) { if (!relevantItems.Contains(pref.Item)) { trainingPrefs.Add(pref); } } if (trainingPrefs.Count > 0) { User trainingUser = new GenericUser<String>(id.ToString(), trainingPrefs); trainingUsers.Add(trainingUser); } } else { trainingUsers.Add(user2); } } DataModel trainingModel = new GenericDataModel(trainingUsers); Recommender recommender = recommenderBuilder.BuildRecommender(trainingModel); try { trainingModel.GetUser(id); } catch (NoSuchElementException) { continue; // Oops we excluded all prefs for the user -- just move on } int intersectionSize = 0; foreach (RecommendedItem recommendedItem in recommender.Recommend(id, at)) { if (relevantItems.Contains(recommendedItem.Item)) { intersectionSize++; } } precision.AddDatum((double) intersectionSize / (double) at); recall.AddDatum((double) intersectionSize / (double) numRelevantItems); } } } return new IRStatisticsImpl(precision.Average, recall.Average); }