org.mymedialite.ratingprediction
Class ItemKNN

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.ItemKNN
All Implemented Interfaces:
java.lang.Cloneable, IItemSimilarityProvider, IRecommender, IIncrementalRatingPredictor, IRatingPredictor
Direct Known Subclasses:
ItemAttributeKNN, ItemKNNCosine, ItemKNNPearson

public abstract class ItemKNN
extends KNN
implements IItemSimilarityProvider

Weighted item-based kNN.


Field Summary
protected  SparseBooleanMatrix data_item
          Matrix indicating which item was rated by which user.
protected  Memoizer<java.lang.Integer,java.util.List<java.lang.Integer>> memoizer
          Source of positively correlated entities.
 
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
ItemKNN()
           
 
Method Summary
 void addItem(int item_id)
           
 void addRating(int user_id, int item_id, double rating)
          Add a new rating and perform incremental training
 float getItemSimilarity(int item_id1, int item_id2)
          Get the similarity between two items.
 int[] getMostSimilarItems(int item_id, int n)
          Get the most similar items.
 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 retrainItem(int item_id)
          Retrain model for a given item.
 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
addUser, 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_item

protected SparseBooleanMatrix data_item
Matrix indicating which item was rated by which user.


memoizer

protected Memoizer<java.lang.Integer,java.util.List<java.lang.Integer>> memoizer
Source of positively correlated entities.

Constructor Detail

ItemKNN

public ItemKNN()
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 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

retrainItem

protected abstract void retrainItem(int item_id)
Retrain model for a given item.

Parameters:
item_id - the item 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

addItem

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

getItemSimilarity

public float getItemSimilarity(int item_id1,
                               int item_id2)
Description copied from interface: IItemSimilarityProvider
Get the similarity between two items.

Specified by:
getItemSimilarity in interface IItemSimilarityProvider
Parameters:
item_id1 - the ID of the first item
item_id2 - the ID of the second item
Returns:
the item similarity; higher means more similar

getMostSimilarItems

public int[] getMostSimilarItems(int item_id,
                                 int n)
Description copied from interface: IItemSimilarityProvider
Get the most similar items.

Specified by:
getMostSimilarItems in interface IItemSimilarityProvider
Parameters:
item_id - the ID of the item
n - the number of similar items to return
Returns:
the items most similar to a given item