public virtual IEnumerator <long> GetUserIDs() { if (tempPrefs == null) { return(_delegate.GetUserIDs()); } return(new PlusAnonymousUserlongPrimitiveIterator(_delegate.GetUserIDs(), TEMP_USER_ID)); }
private int countPreferences() { int numPreferences = 0; var userIDs = dataModel.GetUserIDs(); while (userIDs.MoveNext()) { IPreferenceArray preferencesFromUser = dataModel.GetPreferencesFromUser(userIDs.Current); numPreferences += preferencesFromUser.Length(); } return(numPreferences); }
public RandomRecommender(IDataModel dataModel) : base(dataModel) { float maxPref = float.NegativeInfinity; float minPref = float.PositiveInfinity; var userIterator = dataModel.GetUserIDs(); while (userIterator.MoveNext()) { long userID = userIterator.Current; IPreferenceArray prefs = dataModel.GetPreferencesFromUser(userID); for (int i = 0; i < prefs.Length(); i++) { float prefValue = prefs.GetValue(i); if (prefValue < minPref) { minPref = prefValue; } if (prefValue > maxPref) { maxPref = prefValue; } } } this.minPref = minPref; this.maxPref = maxPref; }
private void buildAverageDiffs() { lock (this) { //buildAveragesLock.writeLock().lock(); IDataModel dataModel = GetDataModel(); var it = dataModel.GetUserIDs(); while (it.MoveNext()) { IPreferenceArray prefs = dataModel.GetPreferencesFromUser(it.Current); int size = prefs.Length(); for (int i = 0; i < size; i++) { long itemID = prefs.GetItemID(i); IRunningAverage average = itemAverages.Get(itemID); if (average == null) { average = new FullRunningAverage(); itemAverages.Put(itemID, average); } average.AddDatum(prefs.GetValue(i)); } } } //finally { //buildAveragesLock.writeLock().unlock(); //} }
public void toyExample() { SVDRecommender svdRecommender = new SVDRecommender(dataModel, factorizer); /// a hold out test would be better, but this is just a toy example so we only check that the /// factorization is close to the original matrix IRunningAverage avg = new FullRunningAverage(); var userIDs = dataModel.GetUserIDs(); while (userIDs.MoveNext()) { long userID = userIDs.Current; foreach (IPreference pref in dataModel.GetPreferencesFromUser(userID)) { double rating = pref.GetValue(); double estimate = svdRecommender.EstimatePreference(userID, pref.GetItemID()); double err = rating - estimate; avg.AddDatum(err * err); } } double rmse = Math.Sqrt(avg.GetAverage()); Assert.True(rmse < 0.2); }
public static void Evaluate(IDataModel model1, IDataModel model2, int samples, IRunningAverage tracker, String tag) { printHeader(); var users = model1.GetUserIDs(); while (users.MoveNext()) { long userID = users.Current; IPreferenceArray prefs1 = model1.GetPreferencesFromUser(userID); IPreferenceArray prefs2 = model2.GetPreferencesFromUser(userID); prefs1.SortByValueReversed(); prefs2.SortByValueReversed(); FastIDSet commonSet = new FastIDSet(); long maxItemID = setBits(commonSet, prefs1, samples); FastIDSet otherSet = new FastIDSet(); maxItemID = Math.Max(maxItemID, setBits(otherSet, prefs2, samples)); int max = mask(commonSet, otherSet, maxItemID); max = Math.Min(max, samples); if (max < 2) { continue; } long[] items1 = getCommonItems(commonSet, prefs1, max); long[] items2 = getCommonItems(commonSet, prefs2, max); double variance = scoreCommonSubset(tag, userID, samples, max, items1, items2); tracker.AddDatum(variance); } }
public GenericUserSimilarity(IUserSimilarity otherSimilarity, IDataModel dataModel, int maxToKeep) { long[] userIDs = longIteratorToList(dataModel.GetUserIDs()); IEnumerator <UserUserSimilarity> it = new DataModelSimilaritiesIterator(otherSimilarity, userIDs); var keptSimilarities = TopItems.GetTopUserUserSimilarities(maxToKeep, it); initSimilarityMaps(keptSimilarities.GetEnumerator()); }
/// Exports the simple user IDs and associated item IDs in the data model. /// /// @return a {@link FastByIDMap} mapping user IDs to {@link FastIDSet}s representing /// that user's associated items public static FastByIDMap <FastIDSet> toDataMap(IDataModel dataModel) { FastByIDMap <FastIDSet> data = new FastByIDMap <FastIDSet>(dataModel.GetNumUsers()); var it = dataModel.GetUserIDs(); while (it.MoveNext()) { long userID = it.Current; data.Put(userID, dataModel.GetItemIDsFromUser(userID)); } return(data); }
/// <summary>Exports the simple user IDs and preferences in the data model.</summary> /// <returns>a <see cref="FastByIDMap"/> mapping user IDs to <see cref="IPreferenceArray"/>s representing that user's preferences</returns> public static FastByIDMap <IPreferenceArray> ToDataMap(IDataModel dataModel) { FastByIDMap <IPreferenceArray> data = new FastByIDMap <IPreferenceArray>(dataModel.GetNumUsers()); var it = dataModel.GetUserIDs(); while (it.MoveNext()) { long userID = it.Current; data.Put(userID, dataModel.GetPreferencesFromUser(userID)); } return(data); }
public override long[] GetUserNeighborhood(long userID) { IDataModel dataModel = getDataModel(); IUserSimilarity userSimilarityImpl = getUserSimilarity(); TopItems.IEstimator <long> estimator = new Estimator(userSimilarityImpl, userID, minSimilarity); var userIDs = SamplinglongPrimitiveIterator.MaybeWrapIterator(dataModel.GetUserIDs(), getSamplingRate()); return(TopItems.GetTopUsers(n, userIDs, null, estimator)); }
double getAveragePreference() { IRunningAverage average = new FullRunningAverage(); var it = dataModel.GetUserIDs(); while (it.MoveNext()) { foreach (IPreference pref in dataModel.GetPreferencesFromUser(it.Current)) { average.AddDatum(pref.GetValue()); } } return(average.GetAverage()); }
private void cachePreferences(IDataModel dataModel) { int numPreferences = countPreferences(dataModel); preferences = new IPreference[numPreferences]; var userIDs = dataModel.GetUserIDs(); int index = 0; while (userIDs.MoveNext()) { long userID = userIDs.Current; IPreferenceArray preferencesFromUser = dataModel.GetPreferencesFromUser(userID); foreach (IPreference preference in preferencesFromUser) { preferences[index++] = preference; } } }
public virtual double Evaluate(IRecommenderBuilder recommenderBuilder, IDataModelBuilder dataModelBuilder, IDataModel dataModel, double trainingPercentage, double evaluationPercentage) { //Preconditions.checkNotNull(recommenderBuilder); //Preconditions.checkNotNull(dataModel); //Preconditions.checkArgument(trainingPercentage >= 0.0 && trainingPercentage <= 1.0, // "Invalid trainingPercentage: " + trainingPercentage + ". Must be: 0.0 <= trainingPercentage <= 1.0"); //Preconditions.checkArgument(evaluationPercentage >= 0.0 && evaluationPercentage <= 1.0, // "Invalid evaluationPercentage: " + evaluationPercentage + ". Must be: 0.0 <= evaluationPercentage <= 1.0"); log.Info("Beginning evaluation using {} of {}", trainingPercentage, dataModel); int numUsers = dataModel.GetNumUsers(); FastByIDMap <IPreferenceArray> trainingPrefs = new FastByIDMap <IPreferenceArray>( 1 + (int)(evaluationPercentage * numUsers)); FastByIDMap <IPreferenceArray> testPrefs = new FastByIDMap <IPreferenceArray>( 1 + (int)(evaluationPercentage * numUsers)); var it = dataModel.GetUserIDs(); while (it.MoveNext()) { long userID = it.Current; if (random.nextDouble() < evaluationPercentage) { splitOneUsersPrefs(trainingPercentage, trainingPrefs, testPrefs, userID, dataModel); } } IDataModel trainingModel = dataModelBuilder == null ? new GenericDataModel(trainingPrefs) : dataModelBuilder.BuildDataModel(trainingPrefs); IRecommender recommender = recommenderBuilder.BuildRecommender(trainingModel); double result = getEvaluation(testPrefs, recommender); log.Info("Evaluation result: {}", result); return(result); }
public void testPreferenceShufflerWithSyntheticData() { setUpSyntheticData(); ParallelSGDFactorizer.PreferenceShuffler shuffler = new ParallelSGDFactorizer.PreferenceShuffler(dataModel); shuffler.shuffle(); shuffler.stage(); FastByIDMap <FastByIDMap <bool?> > checkedLst = new FastByIDMap <FastByIDMap <bool?> >(); for (int i = 0; i < shuffler.size(); i++) { IPreference pref = shuffler.get(i); float?value = dataModel.GetPreferenceValue(pref.GetUserID(), pref.GetItemID()); Assert.AreEqual(pref.GetValue(), value.Value, 0.0); if (!checkedLst.ContainsKey(pref.GetUserID())) { checkedLst.Put(pref.GetUserID(), new FastByIDMap <bool?>()); } Assert.IsNull(checkedLst.Get(pref.GetUserID()).Get(pref.GetItemID())); checkedLst.Get(pref.GetUserID()).Put(pref.GetItemID(), true); } var userIDs = dataModel.GetUserIDs(); int index = 0; while (userIDs.MoveNext()) { long userID = userIDs.Current; IPreferenceArray preferencesFromUser = dataModel.GetPreferencesFromUser(userID); foreach (IPreference preference in preferencesFromUser) { Assert.True(checkedLst.Get(preference.GetUserID()).Get(preference.GetItemID()).Value); index++; } } Assert.AreEqual(index, shuffler.size()); }
public static LoadStatistics runLoad(IRecommender recommender, int howMany) { IDataModel dataModel = recommender.GetDataModel(); int numUsers = dataModel.GetNumUsers(); double sampleRate = 1000.0 / numUsers; var userSampler = SamplinglongPrimitiveIterator.MaybeWrapIterator(dataModel.GetUserIDs(), sampleRate); if (userSampler.MoveNext()) { recommender.Recommend(userSampler.Current, howMany); // Warm up } var callables = new List <Action>(); while (userSampler.MoveNext()) { callables.Add(new LoadCallable(recommender, userSampler.Current).call); } AtomicInteger noEstimateCounter = new AtomicInteger(); IRunningAverageAndStdDev timing = new FullRunningAverageAndStdDev(); AbstractDifferenceRecommenderEvaluator.execute(callables, noEstimateCounter, timing); return(new LoadStatistics(timing)); }
private void buildAverageDiffs() { lock (this) { //buildAveragesLock.writeLock().lock(); IDataModel dataModel = GetDataModel(); var it = dataModel.GetUserIDs(); while (it.MoveNext()) { long userID = it.Current; IPreferenceArray prefs = dataModel.GetPreferencesFromUser(userID); int size = prefs.Length(); for (int i = 0; i < size; i++) { long itemID = prefs.GetItemID(i); float value = prefs.GetValue(i); addDatumAndCreateIfNeeded(itemID, value, itemAverages); addDatumAndCreateIfNeeded(userID, value, userAverages); overallAveragePrefValue.AddDatum(value); } } } /*finally { * buildAveragesLock.writeLock().unlock(); * }*/ }
private void buildMappings() { userIDMapping = createIDMapping(dataModel.GetNumUsers(), dataModel.GetUserIDs()); itemIDMapping = createIDMapping(dataModel.GetNumItems(), dataModel.GetItemIDs()); }
public virtual double Evaluate(IRecommenderBuilder recommenderBuilder, IDataModelBuilder dataModelBuilder, IDataModel dataModel, double trainingPercentage, double evaluationPercentage) { //Preconditions.checkNotNull(recommenderBuilder); //Preconditions.checkNotNull(dataModel); //Preconditions.checkArgument(trainingPercentage >= 0.0 && trainingPercentage <= 1.0, // "Invalid trainingPercentage: " + trainingPercentage + ". Must be: 0.0 <= trainingPercentage <= 1.0"); //Preconditions.checkArgument(evaluationPercentage >= 0.0 && evaluationPercentage <= 1.0, // "Invalid evaluationPercentage: " + evaluationPercentage + ". Must be: 0.0 <= evaluationPercentage <= 1.0"); log.Info("Beginning evaluation using {} of {}", trainingPercentage, dataModel); int numUsers = dataModel.GetNumUsers(); FastByIDMap<IPreferenceArray> trainingPrefs = new FastByIDMap<IPreferenceArray>( 1 + (int) (evaluationPercentage * numUsers)); FastByIDMap<IPreferenceArray> testPrefs = new FastByIDMap<IPreferenceArray>( 1 + (int) (evaluationPercentage * numUsers)); var it = dataModel.GetUserIDs(); while (it.MoveNext()) { long userID = it.Current; if (random.nextDouble() < evaluationPercentage) { splitOneUsersPrefs(trainingPercentage, trainingPrefs, testPrefs, userID, dataModel); } } IDataModel trainingModel = dataModelBuilder == null ? new GenericDataModel(trainingPrefs) : dataModelBuilder.BuildDataModel(trainingPrefs); IRecommender recommender = recommenderBuilder.BuildRecommender(trainingModel); double result = getEvaluation(testPrefs, recommender); log.Info("Evaluation result: {}", result); return result; }
/// Exports the simple user IDs and associated item IDs in the data model. /// /// @return a {@link FastByIDMap} mapping user IDs to {@link FastIDSet}s representing /// that user's associated items public static FastByIDMap<FastIDSet> toDataMap(IDataModel dataModel) { FastByIDMap<FastIDSet> data = new FastByIDMap<FastIDSet>(dataModel.GetNumUsers()); var it = dataModel.GetUserIDs(); while (it.MoveNext()) { long userID = it.Current; data.Put(userID, dataModel.GetItemIDsFromUser(userID)); } return data; }
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)); }
private long[] doMostSimilarUsers(int howMany, TopItems.IEstimator <long> estimator) { IDataModel model = GetDataModel(); return(TopItems.GetTopUsers(howMany, model.GetUserIDs(), null, estimator)); }
private int countPreferences(IDataModel dataModel) { int numPreferences = 0; var userIDs = dataModel.GetUserIDs(); while (userIDs.MoveNext()) { IPreferenceArray preferencesFromUser = dataModel.GetPreferencesFromUser(userIDs.Current); numPreferences += preferencesFromUser.Length(); } return numPreferences; }
public override long[] GetUserNeighborhood(long userID) { IDataModel dataModel = getDataModel(); FastIDSet neighborhood = new FastIDSet(); var usersIterable = SamplinglongPrimitiveIterator.MaybeWrapIterator(dataModel .GetUserIDs(), getSamplingRate()); IUserSimilarity userSimilarityImpl = getUserSimilarity(); while (usersIterable.MoveNext()) { long otherUserID = usersIterable.Current; if (userID != otherUserID) { double theSimilarity = userSimilarityImpl.UserSimilarity(userID, otherUserID); if (!Double.IsNaN(theSimilarity) && theSimilarity >= threshold) { neighborhood.Add(otherUserID); } } } return(neighborhood.ToArray()); }
public GenericUserSimilarity(IUserSimilarity otherSimilarity, IDataModel dataModel) { long[] userIDs = longIteratorToList(dataModel.GetUserIDs()); initSimilarityMaps(new DataModelSimilaritiesIterator(otherSimilarity, userIDs)); }
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); }
/// <summary>Exports the simple user IDs and preferences in the data model.</summary> /// <returns>a <see cref="FastByIDMap"/> mapping user IDs to <see cref="IPreferenceArray"/>s representing that user's preferences</returns> public static FastByIDMap<IPreferenceArray> ToDataMap(IDataModel dataModel) { FastByIDMap<IPreferenceArray> data = new FastByIDMap<IPreferenceArray>(dataModel.GetNumUsers()); var it = dataModel.GetUserIDs(); while (it.MoveNext()) { long userID = it.Current; data.Put(userID, dataModel.GetPreferencesFromUser(userID)); } return data; }
public override Factorization Factorize() { log.Info("starting to compute the factorization..."); Features features = new Features(this); /// feature maps necessary for solving for implicit feedback IDictionary <int, double[]> userY = null; IDictionary <int, double[]> itemY = null; if (usesImplicitFeedback) { userY = userFeaturesMapping(dataModel.GetUserIDs(), dataModel.GetNumUsers(), features.getU()); itemY = itemFeaturesMapping(dataModel.GetItemIDs(), dataModel.GetNumItems(), features.getM()); } IList <Task> tasks; for (int iteration = 0; iteration < numIterations; iteration++) { log.Info("iteration {0}", iteration); /// fix M - compute U tasks = new List <Task>(); var userIDsIterator = dataModel.GetUserIDs(); try { ImplicitFeedbackAlternatingLeastSquaresSolver implicitFeedbackSolver = usesImplicitFeedback ? new ImplicitFeedbackAlternatingLeastSquaresSolver(numFeatures, lambda, alpha, itemY) : null; while (userIDsIterator.MoveNext()) { long userID = userIDsIterator.Current; var itemIDsFromUser = dataModel.GetItemIDsFromUser(userID).GetEnumerator(); IPreferenceArray userPrefs = dataModel.GetPreferencesFromUser(userID); tasks.Add(Task.Factory.StartNew(() => { List <double[]> featureVectors = new List <double[]>(); while (itemIDsFromUser.MoveNext()) { long itemID = itemIDsFromUser.Current; featureVectors.Add(features.getItemFeatureColumn(itemIndex(itemID))); } var userFeatures = usesImplicitFeedback ? implicitFeedbackSolver.solve(sparseUserRatingVector(userPrefs)) : AlternatingLeastSquaresSolver.solve(featureVectors, ratingVector(userPrefs), lambda, numFeatures); features.setFeatureColumnInU(userIndex(userID), userFeatures); } )); } } finally { // queue.shutdown(); try { Task.WaitAll(tasks.ToArray(), 1000 * dataModel.GetNumUsers()); } catch (AggregateException e) { log.Warn("Error when computing user features", e); throw e; } } /// fix U - compute M //queue = createQueue(); tasks = new List <Task>(); var itemIDsIterator = dataModel.GetItemIDs(); try { ImplicitFeedbackAlternatingLeastSquaresSolver implicitFeedbackSolver = usesImplicitFeedback ? new ImplicitFeedbackAlternatingLeastSquaresSolver(numFeatures, lambda, alpha, userY) : null; while (itemIDsIterator.MoveNext()) { long itemID = itemIDsIterator.Current; IPreferenceArray itemPrefs = dataModel.GetPreferencesForItem(itemID); tasks.Add(Task.Factory.StartNew(() => { var featureVectors = new List <double[]>(); foreach (IPreference pref in itemPrefs) { long userID = pref.GetUserID(); featureVectors.Add(features.getUserFeatureColumn(userIndex(userID))); } var itemFeatures = usesImplicitFeedback ? implicitFeedbackSolver.solve(sparseItemRatingVector(itemPrefs)) : AlternatingLeastSquaresSolver.solve(featureVectors, ratingVector(itemPrefs), lambda, numFeatures); features.setFeatureColumnInM(itemIndex(itemID), itemFeatures); })); } } finally { try { Task.WaitAll(tasks.ToArray(), 1000 * dataModel.GetNumItems()); //queue.awaitTermination(dataModel.getNumItems(), TimeUnit.SECONDS); } catch (AggregateException e) { log.Warn("Error when computing item features", e); throw e; } } } log.Info("finished computation of the factorization..."); return(createFactorization(features.getU(), features.getM())); }
public override IEnumerator <long> GetUserIDs() { return(_delegate.GetUserIDs()); }