org.apache.mahout.cf.taste.impl.recommender
Class GenericItemBasedRecommender

java.lang.Object
  extended by org.apache.mahout.cf.taste.impl.recommender.AbstractRecommender
      extended by org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender
All Implemented Interfaces:
Refreshable, ItemBasedRecommender, Recommender
Direct Known Subclasses:
GenericBooleanPrefItemBasedRecommender

public class GenericItemBasedRecommender
extends AbstractRecommender
implements ItemBasedRecommender

A simple Recommender which uses a given DataModel and ItemSimilarity to produce recommendations. This class represents Taste's support for item-based recommenders.

The ItemSimilarity is the most important point to discuss here. Item-based recommenders are useful because they can take advantage of something to be very fast: they base their computations on item similarity, not user similarity, and item similarity is relatively static. It can be precomputed, instead of re-computed in real time.

Thus it's strongly recommended that you use GenericItemSimilarity with pre-computed similarities if you're going to use this class. You can use PearsonCorrelationSimilarity too, which computes similarities in real-time, but will probably find this painfully slow for large amounts of data.


Nested Class Summary
static class GenericItemBasedRecommender.MostSimilarEstimator
           
 
Constructor Summary
GenericItemBasedRecommender(DataModel dataModel, ItemSimilarity similarity)
           
GenericItemBasedRecommender(DataModel dataModel, ItemSimilarity similarity, CandidateItemsStrategy candidateItemsStrategy, MostSimilarItemsCandidateItemsStrategy mostSimilarItemsCandidateItemsStrategy)
           
 
Method Summary
protected  float doEstimatePreference(long userID, PreferenceArray preferencesFromUser, long itemID)
           
 float estimatePreference(long userID, long itemID)
           
protected static MostSimilarItemsCandidateItemsStrategy getDefaultMostSimilarItemsCandidateItemsStrategy()
           
 ItemSimilarity getSimilarity()
           
 List<RecommendedItem> mostSimilarItems(long[] itemIDs, int howMany)
           
 List<RecommendedItem> mostSimilarItems(long[] itemIDs, int howMany, boolean excludeItemIfNotSimilarToAll)
           
 List<RecommendedItem> mostSimilarItems(long[] itemIDs, int howMany, Rescorer<LongPair> rescorer)
           
 List<RecommendedItem> mostSimilarItems(long[] itemIDs, int howMany, Rescorer<LongPair> rescorer, boolean excludeItemIfNotSimilarToAll)
           
 List<RecommendedItem> mostSimilarItems(long itemID, int howMany)
           
 List<RecommendedItem> mostSimilarItems(long itemID, int howMany, Rescorer<LongPair> rescorer)
           
 List<RecommendedItem> recommend(long userID, int howMany, IDRescorer rescorer)
           
 List<RecommendedItem> recommendedBecause(long userID, long itemID, int howMany)
           Lists the items that were most influential in recommending a given item to a given user.
 void refresh(Collection<Refreshable> alreadyRefreshed)
           Triggers "refresh" -- whatever that means -- of the implementation.
 String toString()
           
 
Methods inherited from class org.apache.mahout.cf.taste.impl.recommender.AbstractRecommender
getAllOtherItems, getDataModel, getDefaultCandidateItemsStrategy, recommend, removePreference, setPreference
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.apache.mahout.cf.taste.recommender.Recommender
getDataModel, recommend, removePreference, setPreference
 

Constructor Detail

GenericItemBasedRecommender

public GenericItemBasedRecommender(DataModel dataModel,
                                   ItemSimilarity similarity,
                                   CandidateItemsStrategy candidateItemsStrategy,
                                   MostSimilarItemsCandidateItemsStrategy mostSimilarItemsCandidateItemsStrategy)

GenericItemBasedRecommender

public GenericItemBasedRecommender(DataModel dataModel,
                                   ItemSimilarity similarity)
Method Detail

getDefaultMostSimilarItemsCandidateItemsStrategy

protected static MostSimilarItemsCandidateItemsStrategy getDefaultMostSimilarItemsCandidateItemsStrategy()

getSimilarity

public ItemSimilarity getSimilarity()

recommend

public List<RecommendedItem> recommend(long userID,
                                       int howMany,
                                       IDRescorer rescorer)
                                throws TasteException
Specified by:
recommend in interface Recommender
Parameters:
userID - user for which recommendations are to be computed
howMany - desired number of recommendations
rescorer - rescoring function to apply before final list of recommendations is determined
Returns:
List of recommended RecommendedItems, ordered from most strongly recommend to least
Throws:
TasteException - if an error occurs while accessing the DataModel

estimatePreference

public float estimatePreference(long userID,
                                long itemID)
                         throws TasteException
Specified by:
estimatePreference in interface Recommender
Parameters:
userID - user ID whose preference is to be estimated
itemID - item ID to estimate preference for
Returns:
an estimated preference if the user has not expressed a preference for the item, or else the user's actual preference for the item. If a preference cannot be estimated, returns Double.NaN
Throws:
TasteException - if an error occurs while accessing the DataModel

mostSimilarItems

public List<RecommendedItem> mostSimilarItems(long itemID,
                                              int howMany)
                                       throws TasteException
Specified by:
mostSimilarItems in interface ItemBasedRecommender
Parameters:
itemID - ID of item for which to find most similar other items
howMany - desired number of most similar items to find
Returns:
items most similar to the given item, ordered from most similar to least
Throws:
TasteException - if an error occurs while accessing the DataModel

mostSimilarItems

public List<RecommendedItem> mostSimilarItems(long itemID,
                                              int howMany,
                                              Rescorer<LongPair> rescorer)
                                       throws TasteException
Specified by:
mostSimilarItems in interface ItemBasedRecommender
Parameters:
itemID - ID of item for which to find most similar other items
howMany - desired number of most similar items to find
rescorer - Rescorer which can adjust item-item similarity estimates used to determine most similar items
Returns:
itemss most similar to the given item, ordered from most similar to least
Throws:
TasteException - if an error occurs while accessing the DataModel

mostSimilarItems

public List<RecommendedItem> mostSimilarItems(long[] itemIDs,
                                              int howMany)
                                       throws TasteException
Specified by:
mostSimilarItems in interface ItemBasedRecommender
Parameters:
itemIDs - IDs of item for which to find most similar other items
howMany - desired number of most similar items to find estimates used to determine most similar items
Returns:
items most similar to the given items, ordered from most similar to least
Throws:
TasteException - if an error occurs while accessing the DataModel

mostSimilarItems

public List<RecommendedItem> mostSimilarItems(long[] itemIDs,
                                              int howMany,
                                              Rescorer<LongPair> rescorer)
                                       throws TasteException
Specified by:
mostSimilarItems in interface ItemBasedRecommender
Parameters:
itemIDs - IDs of item for which to find most similar other items
howMany - desired number of most similar items to find
rescorer - Rescorer which can adjust item-item similarity estimates used to determine most similar items
Returns:
items most similar to the given items, ordered from most similar to least
Throws:
TasteException - if an error occurs while accessing the DataModel

mostSimilarItems

public List<RecommendedItem> mostSimilarItems(long[] itemIDs,
                                              int howMany,
                                              boolean excludeItemIfNotSimilarToAll)
                                       throws TasteException
Specified by:
mostSimilarItems in interface ItemBasedRecommender
Parameters:
itemIDs - IDs of item for which to find most similar other items
howMany - desired number of most similar items to find
excludeItemIfNotSimilarToAll - exclude an item if it is not similar to each of the input items
Returns:
items most similar to the given items, ordered from most similar to least
Throws:
TasteException - if an error occurs while accessing the DataModel

mostSimilarItems

public List<RecommendedItem> mostSimilarItems(long[] itemIDs,
                                              int howMany,
                                              Rescorer<LongPair> rescorer,
                                              boolean excludeItemIfNotSimilarToAll)
                                       throws TasteException
Specified by:
mostSimilarItems in interface ItemBasedRecommender
Parameters:
itemIDs - IDs of item for which to find most similar other items
howMany - desired number of most similar items to find
rescorer - Rescorer which can adjust item-item similarity estimates used to determine most similar items
excludeItemIfNotSimilarToAll - exclude an item if it is not similar to each of the input items
Returns:
items most similar to the given items, ordered from most similar to least
Throws:
TasteException - if an error occurs while accessing the DataModel

recommendedBecause

public List<RecommendedItem> recommendedBecause(long userID,
                                                long itemID,
                                                int howMany)
                                         throws TasteException
Description copied from interface: ItemBasedRecommender

Lists the items that were most influential in recommending a given item to a given user. Exactly how this is determined is left to the implementation, but, generally this will return items that the user prefers and that are similar to the given item.

This returns a List of RecommendedItem which is a little misleading since it's returning recommending items, but, I thought it more natural to just reuse this class since it encapsulates an item and value. The value here does not necessarily have a consistent interpretation or expected range; it will be higher the more influential the item was in the recommendation.

Specified by:
recommendedBecause in interface ItemBasedRecommender
Parameters:
userID - ID of user who was recommended the item
itemID - ID of item that was recommended
howMany - maximum number of items to return
Returns:
List of RecommendedItem, ordered from most influential in recommended the given item to least
Throws:
TasteException - if an error occurs while accessing the DataModel

doEstimatePreference

protected float doEstimatePreference(long userID,
                                     PreferenceArray preferencesFromUser,
                                     long itemID)
                              throws TasteException
Throws:
TasteException

refresh

public void refresh(Collection<Refreshable> alreadyRefreshed)
Description copied from interface: Refreshable

Triggers "refresh" -- whatever that means -- of the implementation. The general contract is that any Refreshable should always leave itself in a consistent, operational state, and that the refresh atomically updates internal state from old to new.

Specified by:
refresh in interface Refreshable
Parameters:
alreadyRefreshed - Refreshables that are known to have already been refreshed as a result of an initial call to a Refreshable.refresh(Collection) method on some object. This ensure that objects in a refresh dependency graph aren't refreshed twice needlessly.

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2008–2014 The Apache Software Foundation. All rights reserved.