/** * {@inheritDoc} */ public double Evaluate(RecommenderBuilder recommenderBuilder, DataModel dataModel, double trainingPercentage, double evaluationPercentage) { if (recommenderBuilder == null) { throw new ArgumentNullException("recommenderBuilder is null"); } if (dataModel == null) { throw new ArgumentNullException("dataModel is null"); } if (double.IsNaN(trainingPercentage) || trainingPercentage <= 0.0 || trainingPercentage >= 1.0) { throw new ArgumentException("Invalid trainingPercentage: " + trainingPercentage); } if (double.IsNaN(evaluationPercentage) || evaluationPercentage <= 0.0 || evaluationPercentage > 1.0) { throw new ArgumentException("Invalid evaluationPercentage: " + evaluationPercentage); } log.Info("Beginning evaluation using " + trainingPercentage + " of " + dataModel); int numUsers = dataModel.GetNumUsers(); ICollection<User> trainingUsers = new List<User>(1 + (int) (trainingPercentage * (double) numUsers)); IDictionary<User, ICollection<Preference>> testUserPrefs = new Dictionary<User, ICollection<Preference>>(1 + (int) ((1.0 - trainingPercentage) * (double) numUsers)); foreach (User user in dataModel.GetUsers()) { if (random.NextDouble() < evaluationPercentage) { ICollection<Preference> trainingPrefs = new List<Preference>(); ICollection<Preference> testPrefs = new List<Preference>(); Preference[] prefs = user.GetPreferencesAsArray(); foreach (Preference pref in prefs) { Item itemCopy = new GenericItem<String>(pref.Item.ID.ToString()); Preference newPref = new GenericPreference(null, itemCopy, pref.Value); if (random.NextDouble() < trainingPercentage) { trainingPrefs.Add(newPref); } else { testPrefs.Add(newPref); } } if (log.IsDebugEnabled) { log.Debug("Training against " + trainingPrefs.Count + " preferences"); log.Debug("Evaluating accuracy of " + testPrefs.Count + " preferences"); } if (trainingPrefs.Count > 0) { User trainingUser = new GenericUser<String>(user.ID.ToString(), trainingPrefs); trainingUsers.Add(trainingUser); if (testPrefs.Count > 0) { testUserPrefs.Add(trainingUser, testPrefs); } } } } DataModel trainingModel = new GenericDataModel(trainingUsers); Recommender recommender = recommenderBuilder.BuildRecommender(trainingModel); double result = GetEvaluation(testUserPrefs, recommender); log.Info("Evaluation result: " + result); return result; }
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); }
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)); }
/** * {@inheritDoc} */ public double Evaluate(RecommenderBuilder recommenderBuilder, DataModel dataModel, double trainingPercentage, double evaluationPercentage) { if (recommenderBuilder == null) { throw new ArgumentNullException("recommenderBuilder is null"); } if (dataModel == null) { throw new ArgumentNullException("dataModel is null"); } if (double.IsNaN(trainingPercentage) || trainingPercentage <= 0.0 || trainingPercentage >= 1.0) { throw new ArgumentException("Invalid trainingPercentage: " + trainingPercentage); } if (double.IsNaN(evaluationPercentage) || evaluationPercentage <= 0.0 || evaluationPercentage > 1.0) { throw new ArgumentException("Invalid evaluationPercentage: " + evaluationPercentage); } log.Info("Beginning evaluation using " + trainingPercentage + " of " + dataModel); int numUsers = dataModel.GetNumUsers(); ICollection <User> trainingUsers = new List <User>(1 + (int)(trainingPercentage * (double)numUsers)); IDictionary <User, ICollection <Preference> > testUserPrefs = new Dictionary <User, ICollection <Preference> >(1 + (int)((1.0 - trainingPercentage) * (double)numUsers)); foreach (User user in dataModel.GetUsers()) { if (random.NextDouble() < evaluationPercentage) { ICollection <Preference> trainingPrefs = new List <Preference>(); ICollection <Preference> testPrefs = new List <Preference>(); Preference[] prefs = user.GetPreferencesAsArray(); foreach (Preference pref in prefs) { Item itemCopy = new GenericItem <String>(pref.Item.ID.ToString()); Preference newPref = new GenericPreference(null, itemCopy, pref.Value); if (random.NextDouble() < trainingPercentage) { trainingPrefs.Add(newPref); } else { testPrefs.Add(newPref); } } if (log.IsDebugEnabled) { log.Debug("Training against " + trainingPrefs.Count + " preferences"); log.Debug("Evaluating accuracy of " + testPrefs.Count + " preferences"); } if (trainingPrefs.Count > 0) { User trainingUser = new GenericUser <String>(user.ID.ToString(), trainingPrefs); trainingUsers.Add(trainingUser); if (testPrefs.Count > 0) { testUserPrefs.Add(trainingUser, testPrefs); } } } } DataModel trainingModel = new GenericDataModel(trainingUsers); Recommender recommender = recommenderBuilder.BuildRecommender(trainingModel); double result = GetEvaluation(testUserPrefs, recommender); log.Info("Evaluation result: " + result); return(result); }