Taste: user's neighbours and their similarity

2009-03-18 Thread Otis Gospodnetic

Hi,

Is there a way to get a collection of neighbours for a given user?  I'm 
referring to the same neighbour collection that recommendations are derived 
from.  I didn't see a way, so I simply made NearestNUserNeighborhood.Estimator 
public (diff below), so I could do something like this:

  public CollectionSimilarUser getHood(Object userID) throws TasteException {
User theUser = recommender.getDataModel().getUser(userID);
TopItems.EstimatorUser estimator = new 
NearestNUserNeighborhood.Estimator(similarity, theUser, minSimilarity);
CollectionUser neighbors = hood.getUserNeighborhood(userID);
CollectionSimilarUser similarHood = new 
ArrayListSimilarUser(neighbors.size());
System.out.println(Neighbors for user:  + userID + :  + 
neighbors.size());
for (User user : neighbors) {
  SimilarUser su = new SimilarUser(user, estimator.estimate(user));
  similarHood.add(su);
}
return similarHood;
  }

This gives me the needed collection:

[SimilarUser[user:User[id:U2], similarity:0.7084]]


$ svn diff  
core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/NearestNUserNeighborhood.java
Index: 
core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/NearestNUserNeighborhood.java
===
--- 
core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/NearestNUserNeighborhood.java
   (revision 755664)
+++ 
core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/NearestNUserNeighborhood.java
   (working copy)
@@ -109,12 +109,12 @@
 return NearestNUserNeighborhood;
   }
 
-  private static class Estimator implements TopItems.EstimatorUser {
+  public static class Estimator implements TopItems.EstimatorUser {
 private final UserSimilarity userSimilarityImpl;
 private final User theUser;
 private final double minSim;
 
-private Estimator(UserSimilarity userSimilarityImpl, User theUser, double 
minSim) {
+public Estimator(UserSimilarity userSimilarityImpl, User theUser, double 
minSim) {
   this.userSimilarityImpl = userSimilarityImpl;
   this.theUser = theUser;
   this.minSim = minSim;



Is there an existing way to get the neighbours + similarity information?  If 
not, is the above change OK?

Thanks,
Otis
--
Sematext -- http://sematext.com/ -- Lucene - Solr - Nutch



Re: Taste: user's neighbours and their similarity

2009-03-18 Thread Sean Owen
How about the method UserBasedRecommender.mostSimilarUsers()? or a bit
more directly, UserNeighborhood.getUserNeighborhood()? (They are
arguably kind of redundant but it's 'for historical reasons' and low
on my list of design sins.) These in turn largely use
TopItems.getTopUsers() and you apparently already see all this so:

I suppose you are interested in the latter since it reports some
measure of similarity as well as the users themselves.

You want to just refactor getTopUsers() there so a version is also
provided that gives you the SimilarUser objects instead of just the
Users? OK by me and perhaps a bit more general than putting code in
NearestNUserNeighborhood.

On Wed, Mar 18, 2009 at 9:04 PM, Otis Gospodnetic
otis_gospodne...@yahoo.com wrote:

 Hi,

 Is there a way to get a collection of neighbours for a given user?  I'm 
 referring to the same neighbour collection that recommendations are derived 
 from.  I didn't see a way, so I simply made 
 NearestNUserNeighborhood.Estimator public (diff below), so I could do 
 something like this:

  public CollectionSimilarUser getHood(Object userID) throws TasteException {
    User theUser = recommender.getDataModel().getUser(userID);
    TopItems.EstimatorUser estimator = new 
 NearestNUserNeighborhood.Estimator(similarity, theUser, minSimilarity);
    CollectionUser neighbors = hood.getUserNeighborhood(userID);
    CollectionSimilarUser similarHood = new 
 ArrayListSimilarUser(neighbors.size());
    System.out.println(Neighbors for user:  + userID + :  + 
 neighbors.size());
    for (User user : neighbors) {
      SimilarUser su = new SimilarUser(user, estimator.estimate(user));
      similarHood.add(su);
    }
    return similarHood;
  }

 This gives me the needed collection:

 [SimilarUser[user:User[id:U2], similarity:0.7084]]


 $ svn diff  
 core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/NearestNUserNeighborhood.java
 Index: 
 core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/NearestNUserNeighborhood.java
 ===
 --- 
 core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/NearestNUserNeighborhood.java
        (revision 755664)
 +++ 
 core/src/main/java/org/apache/mahout/cf/taste/impl/neighborhood/NearestNUserNeighborhood.java
        (working copy)
 @@ -109,12 +109,12 @@
     return NearestNUserNeighborhood;
   }

 -  private static class Estimator implements TopItems.EstimatorUser {
 +  public static class Estimator implements TopItems.EstimatorUser {
     private final UserSimilarity userSimilarityImpl;
     private final User theUser;
     private final double minSim;

 -    private Estimator(UserSimilarity userSimilarityImpl, User theUser, 
 double minSim) {
 +    public Estimator(UserSimilarity userSimilarityImpl, User theUser, double 
 minSim) {
       this.userSimilarityImpl = userSimilarityImpl;
       this.theUser = theUser;
       this.minSim = minSim;



 Is there an existing way to get the neighbours + similarity information?  If 
 not, is the above change OK?

 Thanks,
 Otis
 --
 Sematext -- http://sematext.com/ -- Lucene - Solr - Nutch