Exemple #1
0
        public virtual IPreferenceArray GetPreferencesForItem(long itemID)
        {
            if (tempPrefs == null)
            {
                return(_delegate.GetPreferencesForItem(itemID));
            }
            IPreferenceArray delegatePrefs = null;

            try {
                delegatePrefs = _delegate.GetPreferencesForItem(itemID);
            } catch (NoSuchItemException nsie) {
                // OK. Probably an item that only the anonymous user has
                //if (log.isDebugEnabled()) {
                log.Debug("Item {} unknown", itemID);
                //}
            }
            for (int i = 0; i < tempPrefs.Length(); i++)
            {
                if (tempPrefs.GetItemID(i) == itemID)
                {
                    return(cloneAndMergeInto(delegatePrefs, itemID, tempPrefs.GetUserID(i), tempPrefs.GetValue(i)));
                }
            }
            if (delegatePrefs == null)
            {
                // No, didn't find it among the anonymous user prefs
                throw new NoSuchItemException(itemID);
            }
            return(delegatePrefs);
        }
Exemple #2
0
            public double averateRating(long itemID)
            {
                IPreferenceArray prefs = dataModel.GetPreferencesForItem(itemID);
                IRunningAverage  avg   = new FullRunningAverage();

                foreach (IPreference pref in prefs)
                {
                    avg.AddDatum(pref.GetValue());
                }
                return(avg.GetAverage());
            }
 protected override FastIDSet doGetCandidateItems(long[] preferredItemIDs, IDataModel dataModel) {
   FastIDSet possibleItemsIDs = new FastIDSet();
   foreach (long itemID in preferredItemIDs) {
     IPreferenceArray itemPreferences = dataModel.GetPreferencesForItem(itemID);
     int numUsersPreferringItem = itemPreferences.Length();
     for (int index = 0; index < numUsersPreferringItem; index++) {
       possibleItemsIDs.AddAll(dataModel.GetItemIDsFromUser(itemPreferences.GetUserID(index)));
     }
   }
   possibleItemsIDs.RemoveAll(preferredItemIDs);
   return possibleItemsIDs;
 }
        public void testPreferencesForItem()
        {
            IPreferenceArray prefs = model.GetPreferencesForItem(456);

            Assert.NotNull(prefs);
            IPreference pref1 = prefs.Get(0);

            Assert.AreEqual(123, pref1.GetUserID());
            Assert.AreEqual(456, pref1.GetItemID());
            IPreference pref2 = prefs.Get(1);

            Assert.AreEqual(456, pref2.GetUserID());
            Assert.AreEqual(456, pref2.GetItemID());
            Assert.AreEqual(2, prefs.Length());
        }
        protected override FastIDSet doGetCandidateItems(long[] preferredItemIDs, IDataModel dataModel)
        {
            FastIDSet possibleItemsIDs = new FastIDSet();

            foreach (long itemID in preferredItemIDs)
            {
                IPreferenceArray itemPreferences = dataModel.GetPreferencesForItem(itemID);
                int numUsersPreferringItem       = itemPreferences.Length();
                for (int index = 0; index < numUsersPreferringItem; index++)
                {
                    possibleItemsIDs.AddAll(dataModel.GetItemIDsFromUser(itemPreferences.GetUserID(index)));
                }
            }
            possibleItemsIDs.RemoveAll(preferredItemIDs);
            return(possibleItemsIDs);
        }
Exemple #6
0
        protected override FastIDSet doGetCandidateItems(long[] preferredItemIDs, IDataModel dataModel)
        {
            var preferredItemIDsIterator = ((IEnumerable <long>)preferredItemIDs).GetEnumerator();

            if (preferredItemIDs.Length > maxItems)
            {
                double samplingRate = (double)maxItems / preferredItemIDs.Length;
                log.Info("preferredItemIDs.Length {0}, samplingRate {1}", preferredItemIDs.Length, samplingRate);
                preferredItemIDsIterator =
                    new SamplinglongPrimitiveIterator(preferredItemIDsIterator, samplingRate);
            }
            FastIDSet possibleItemsIDs = new FastIDSet();

            while (preferredItemIDsIterator.MoveNext())
            {
                long             itemID = preferredItemIDsIterator.Current;
                IPreferenceArray prefs  = dataModel.GetPreferencesForItem(itemID);
                int prefsLength         = prefs.Length();
                if (prefsLength > maxUsersPerItem)
                {
                    var sampledPrefs =
                        new FixedSizeSamplingIterator <IPreference>(maxUsersPerItem, prefs.GetEnumerator());
                    while (sampledPrefs.MoveNext())
                    {
                        addSomeOf(possibleItemsIDs, dataModel.GetItemIDsFromUser(sampledPrefs.Current.GetUserID()));
                    }
                }
                else
                {
                    for (int i = 0; i < prefsLength; i++)
                    {
                        addSomeOf(possibleItemsIDs, dataModel.GetItemIDsFromUser(prefs.GetUserID(i)));
                    }
                }
            }
            possibleItemsIDs.RemoveAll(preferredItemIDs);
            return(possibleItemsIDs);
        }
        public override double ItemSimilarity(long itemID1, long itemID2)
        {
            IDataModel       dataModel = getDataModel();
            IPreferenceArray xPrefs    = dataModel.GetPreferencesForItem(itemID1);
            IPreferenceArray yPrefs    = dataModel.GetPreferencesForItem(itemID2);
            int xLength = xPrefs.Length();
            int yLength = yPrefs.Length();

            if (xLength == 0 || yLength == 0)
            {
                return(Double.NaN);
            }

            long xIndex     = xPrefs.GetUserID(0);
            long yIndex     = yPrefs.GetUserID(0);
            int  xPrefIndex = 0;
            int  yPrefIndex = 0;

            double sumX       = 0.0;
            double sumX2      = 0.0;
            double sumY       = 0.0;
            double sumY2      = 0.0;
            double sumXY      = 0.0;
            double sumXYdiff2 = 0.0;
            int    count      = 0;

            // No, pref inferrers and transforms don't apply here. I think.

            while (true)
            {
                int compare = xIndex <yIndex ? -1 : xIndex> yIndex ? 1 : 0;
                if (compare == 0)
                {
                    // Both users expressed a preference for the item
                    double x = xPrefs.GetValue(xPrefIndex);
                    double y = yPrefs.GetValue(yPrefIndex);
                    sumXY += x * y;
                    sumX  += x;
                    sumX2 += x * x;
                    sumY  += y;
                    sumY2 += y * y;
                    double diff = x - y;
                    sumXYdiff2 += diff * diff;
                    count++;
                }
                if (compare <= 0)
                {
                    if (++xPrefIndex == xLength)
                    {
                        break;
                    }
                    xIndex = xPrefs.GetUserID(xPrefIndex);
                }
                if (compare >= 0)
                {
                    if (++yPrefIndex == yLength)
                    {
                        break;
                    }
                    yIndex = yPrefs.GetUserID(yPrefIndex);
                }
            }

            double result;

            if (centerData)
            {
                // See comments above on these computations
                double n     = (double)count;
                double meanX = sumX / n;
                double meanY = sumY / n;
                // double centeredSumXY = sumXY - meanY * sumX - meanX * sumY + n * meanX * meanY;
                double centeredSumXY = sumXY - meanY * sumX;
                // double centeredSumX2 = sumX2 - 2.0 * meanX * sumX + n * meanX * meanX;
                double centeredSumX2 = sumX2 - meanX * sumX;
                // double centeredSumY2 = sumY2 - 2.0 * meanY * sumY + n * meanY * meanY;
                double centeredSumY2 = sumY2 - meanY * sumY;
                result = computeResult(count, centeredSumXY, centeredSumX2, centeredSumY2, sumXYdiff2);
            }
            else
            {
                result = computeResult(count, sumXY, sumX2, sumY2, sumXYdiff2);
            }

            if (!Double.IsNaN(result))
            {
                result = normalizeWeightResult(result, count, cachedNumUsers);
            }
            return(result);
        }
  protected override FastIDSet doGetCandidateItems(long[] preferredItemIDs, IDataModel dataModel) {
    var preferredItemIDsIterator = ((IEnumerable<long>)preferredItemIDs).GetEnumerator();
    if (preferredItemIDs.Length > maxItems) {
      double samplingRate = (double) maxItems / preferredItemIDs.Length;
      log.Info("preferredItemIDs.Length {0}, samplingRate {1}", preferredItemIDs.Length, samplingRate);
      preferredItemIDsIterator = 
          new SamplinglongPrimitiveIterator(preferredItemIDsIterator, samplingRate);
    }
    FastIDSet possibleItemsIDs = new FastIDSet();
    while (preferredItemIDsIterator.MoveNext()) {
      long itemID = preferredItemIDsIterator.Current;
      IPreferenceArray prefs = dataModel.GetPreferencesForItem(itemID);
      int prefsLength = prefs.Length();
	  if (prefsLength > maxUsersPerItem) {
        var sampledPrefs =
			new FixedSizeSamplingIterator<IPreference>(maxUsersPerItem, prefs.GetEnumerator());
        while (sampledPrefs.MoveNext()) {
          addSomeOf(possibleItemsIDs, dataModel.GetItemIDsFromUser(sampledPrefs.Current.GetUserID()));
        }
      } else {
        for (int i = 0; i < prefsLength; i++) {
          addSomeOf(possibleItemsIDs, dataModel.GetItemIDsFromUser(prefs.GetUserID(i)));
        }
      }
    }
    possibleItemsIDs.RemoveAll(preferredItemIDs);
    return possibleItemsIDs;
  }
Exemple #9
0
        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 IPreferenceArray GetPreferencesForItem(long itemID)
 {
     return(_delegate.GetPreferencesForItem(itemID));
 }