org.mymedialite.ratingprediction
Class UserItemBaseline

java.lang.Object
  extended by org.mymedialite.ratingprediction.RatingPredictor
      extended by org.mymedialite.ratingprediction.IncrementalRatingPredictor
          extended by org.mymedialite.ratingprediction.UserItemBaseline
All Implemented Interfaces:
java.lang.Cloneable, IIterativeModel, IRecommender, IIncrementalRatingPredictor, IRatingPredictor

public class UserItemBaseline
extends IncrementalRatingPredictor
implements IIterativeModel

Baseline method for rating prediction Uses the average rating value, plus a regularized user and item bias for prediction. The method is described in section 2.1 of Yehuda Koren: Factor in the Neighbors: Scalable and Accurate Collaborative Filtering, Transactions on Knowledge Discovery from Data (TKDD), 2009. One difference is that we support several iterations of alternating optimization, instead of just one. This recommender supports incremental updates.


Field Summary
protected  double globalAverage
          The global rating average
protected  double[] itemBiases
          The item biases
 int numIter
          The number of iterations
 double regI
          Regularization parameter for the item biases
 double regU
          Regularization parameter for the user biases
protected  double[] userBiases
          The user biases
 
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
UserItemBaseline()
          Default constructor
 
Method Summary
 void addItem(int itemID)
           
 void addRating(int userID, int itemID, double rating)
          Add a new rating and perform incremental training
 void addUser(int userID)
           
 double computeLoss()
          Compute the current loss of the model
 int getNumIter()
          Get the number of iterations to run the training
 void iterate()
          Run one iteration (= pass over the training data)
 void loadModel(java.lang.String filename)
          Get the model parameters from a file
 double predict(int userID, int itemID)
          Predict the rating or score for a given user-item combination.
 void removeRating(int userID, int itemID)
          Remove an existing rating and perform "incremental" training
protected  void retrainItem(int itemID)
           
protected  void retrainUser(int userID)
           
 void saveModel(java.lang.String filename)
          Save the model parameters to a file
 void setNumIter(int numIter)
          Set the number of iterations to run the training
 java.lang.String toString()
          Return a string representation of the recommender
 void train()
          Learn the model parameters of the recommender from the training data
 void updateRating(int userID, int itemID, double rating)
          Update an existing rating and perform incremental training
 
Methods inherited from class org.mymedialite.ratingprediction.IncrementalRatingPredictor
getUpdateItems, getUpdateUsers, removeItem, removeUser, setUpdateItems, setUpdateUsers
 
Methods inherited from class org.mymedialite.ratingprediction.RatingPredictor
canPredict, clone, getMaxRating, getMinRating, getRatings, setMaxRating, setMinRating, setRatings
 
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
 

Field Detail

regU

public double regU
Regularization parameter for the user biases


regI

public double regI
Regularization parameter for the item biases


numIter

public int numIter
The number of iterations


globalAverage

protected double globalAverage
The global rating average


userBiases

protected double[] userBiases
The user biases


itemBiases

protected double[] itemBiases
The item biases

Constructor Detail

UserItemBaseline

public UserItemBaseline()
Default constructor

Method Detail

getNumIter

public int getNumIter()
Description copied from interface: IIterativeModel
Get the number of iterations to run the training

Specified by:
getNumIter in interface IIterativeModel
Returns:
The number of iterations

setNumIter

public void setNumIter(int numIter)
Description copied from interface: IIterativeModel
Set the number of iterations to run the training

Specified by:
setNumIter in interface IIterativeModel
Parameters:
numIter - The number of iterations

iterate

public void iterate()
Description copied from interface: IIterativeModel
Run one iteration (= pass over the training data)

Specified by:
iterate in interface IIterativeModel

predict

public double predict(int userID,
                      int itemID)
Description copied from interface: IRecommender
Predict the rating or score for a given user-item combination.

Specified by:
predict in interface IRecommender
Specified by:
predict in class RatingPredictor
Parameters:
userID - the user ID
itemID - the item ID
Returns:
the predicted score/rating for the given user-item combination

train

public void train()
Description copied from interface: IRecommender
Learn the model parameters of the recommender from the training data

Specified by:
train in interface IRecommender
Specified by:
train in class RatingPredictor

retrainUser

protected void retrainUser(int userID)

retrainItem

protected void retrainItem(int itemID)

addRating

public void addRating(int userID,
                      int itemID,
                      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:
userID - the ID of the user who performed the rating
itemID - the ID of the rated item
rating - the rating value

updateRating

public void updateRating(int userID,
                         int itemID,
                         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:
userID - the ID of the user who performed the rating
itemID - the ID of the rated item
rating - the rating value

removeRating

public void removeRating(int userID,
                         int itemID)
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:
userID - the ID of the user who performed the rating
itemID - the ID of the rated item

addUser

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

addItem

public void addItem(int itemID)
Overrides:
addItem in class IncrementalRatingPredictor

saveModel

public void saveModel(java.lang.String filename)
               throws java.io.IOException
Description copied from interface: IRecommender
Save the model parameters to a file

Specified by:
saveModel in interface IRecommender
Specified by:
saveModel in class RatingPredictor
Parameters:
filename - the file to write to
Throws:
java.io.IOException

loadModel

public void loadModel(java.lang.String filename)
               throws java.io.IOException
Description copied from interface: IRecommender
Get the model parameters from a file

Specified by:
loadModel in interface IRecommender
Specified by:
loadModel in class RatingPredictor
Parameters:
filename - the file to read from
Throws:
java.io.IOException

computeLoss

public double computeLoss()
Description copied from interface: IIterativeModel
Compute the current loss of the model

Specified by:
computeLoss in interface IIterativeModel
Returns:
the current loss; -1 if not implemented

toString

public java.lang.String toString()
Description copied from interface: IRecommender
Return a string representation of the recommender

Specified by:
toString in interface IRecommender
Overrides:
toString in class RatingPredictor
Returns:
the class name and all hyperparameters, separated by space characters.