org.mymedialite.ratingprediction
Class UserKNN

java.lang.Object
  extended by org.mymedialite.ratingprediction.RatingPredictor
      extended by org.mymedialite.ratingprediction.IncrementalRatingPredictor
          extended by org.mymedialite.ratingprediction.KNN
              extended by org.mymedialite.ratingprediction.UserKNN
All Implemented Interfaces:
java.lang.Cloneable, IRecommender, IUserSimilarityProvider, IIncrementalRatingPredictor, IRatingPredictor
Direct Known Subclasses:
UserAttributeKNN, UserKNNCosine, UserKNNPearson

public abstract class UserKNN
extends KNN
implements IUserSimilarityProvider

Weighted user-based kNN.


Field Summary
protected  SparseBooleanMatrix data_user
          boolean matrix indicating which user rated which item.
 
Fields inherited from class org.mymedialite.ratingprediction.KNN
baseline_predictor, correlation, k
 
Fields inherited from class org.mymedialite.ratingprediction.IncrementalRatingPredictor
updateItems, updateUsers
 
Fields inherited from class org.mymedialite.ratingprediction.RatingPredictor
maxItemID, maxRating, maxUserID, minRating, ratings
 
Constructor Summary
UserKNN()
           
 
Method Summary
 void addRating(int user_id, int item_id, double rating)
          Add a new rating and perform incremental training
 void addUser(int user_id)
           
 int[] getMostSimilarUsers(int user_id, int n)
          get the most similar users.
 float getUserSimilarity(int user_id1, int user_id2)
          get the similarity between two users.
 double predict(int user_id, int item_id)
          Predict the rating of a given user for a given item.
 void removeRating(int user_id, int item_id)
          Remove an existing rating and perform "incremental" training
protected abstract  void retrainUser(int user_id)
          Retrain model for a given user.
 void setRatings(IRatings ratings)
           
 void updateRating(int user_id, int item_id, double rating)
          Update an existing rating and perform incremental training
 
Methods inherited from class org.mymedialite.ratingprediction.KNN
getRegI, getRegU, loadModel, saveModel, setRegI, setRegU
 
Methods inherited from class org.mymedialite.ratingprediction.IncrementalRatingPredictor
addItem, getUpdateItems, getUpdateUsers, removeItem, removeUser, setUpdateItems, setUpdateUsers
 
Methods inherited from class org.mymedialite.ratingprediction.RatingPredictor
canPredict, clone, getMaxRating, getMinRating, getRatings, setMaxRating, setMinRating, toString, train
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.mymedialite.ratingprediction.IRatingPredictor
getMaxRating, getMinRating, setMaxRating, setMinRating
 
Methods inherited from interface org.mymedialite.IRecommender
canPredict, toString, train
 

Field Detail

data_user

protected SparseBooleanMatrix data_user
boolean matrix indicating which user rated which item.

Constructor Detail

UserKNN

public UserKNN()
Method Detail

setRatings

public void setRatings(IRatings ratings)
Overrides:
setRatings in class KNN

predict

public double predict(int user_id,
                      int item_id)
Predict the rating of a given user for a given item. If the user or the item are not known to the recommender, a suitable average rating is returned. To avoid this behavior for unknown entities, use CanPredict() to check before.

Specified by:
predict in interface IRecommender
Specified by:
predict in class RatingPredictor
Parameters:
user_id - the user ID
item_id - the item ID
Returns:
the predicted rating

retrainUser

protected abstract void retrainUser(int user_id)
Retrain model for a given user.

Parameters:
user_id - the user ID

addRating

public void addRating(int user_id,
                      int item_id,
                      double rating)
Description copied from interface: IIncrementalRatingPredictor
Add a new rating and perform incremental training

Specified by:
addRating in interface IIncrementalRatingPredictor
Overrides:
addRating in class IncrementalRatingPredictor
Parameters:
user_id - the ID of the user who performed the rating
item_id - the ID of the rated item
rating - the rating value

updateRating

public void updateRating(int user_id,
                         int item_id,
                         double rating)
Description copied from interface: IIncrementalRatingPredictor
Update an existing rating and perform incremental training

Specified by:
updateRating in interface IIncrementalRatingPredictor
Overrides:
updateRating in class IncrementalRatingPredictor
Parameters:
user_id - the ID of the user who performed the rating
item_id - the ID of the rated item
rating - the rating value

removeRating

public void removeRating(int user_id,
                         int item_id)
Description copied from interface: IIncrementalRatingPredictor
Remove an existing rating and perform "incremental" training

Specified by:
removeRating in interface IIncrementalRatingPredictor
Overrides:
removeRating in class IncrementalRatingPredictor
Parameters:
user_id - the ID of the user who performed the rating
item_id - the ID of the rated item

addUser

public void addUser(int user_id)
Overrides:
addUser in class IncrementalRatingPredictor

getUserSimilarity

public float getUserSimilarity(int user_id1,
                               int user_id2)
Description copied from interface: IUserSimilarityProvider
get the similarity between two users.

Specified by:
getUserSimilarity in interface IUserSimilarityProvider
Parameters:
user_id1 - the ID of the first user
user_id2 - the ID of the second user
Returns:
the user similarity; higher means more similar

getMostSimilarUsers

public int[] getMostSimilarUsers(int user_id,
                                 int n)
Description copied from interface: IUserSimilarityProvider
get the most similar users.

Specified by:
getMostSimilarUsers in interface IUserSimilarityProvider
Parameters:
user_id - the ID of the user
n - the number of similar users to return
Returns:
the users most similar to a given user