Item-based recommendation is based on item similarity. In mahout, it means that itemsimilarity is used to implement similarity measurement, rather than usersimilarity. They use similar users and similar items.
Item-based: understanding users' preferences and searching for similar items
User-based: find similar users and learn what they like.
If the number of items is much smaller than the number of users, item-based recommendations will improve performance.
Code:
Package mahout; import Java. io. file; import Java. util. list; import Org. apache. mahout. cf. taste. impl. model. file. filedatamodel; import Org. apache. mahout. cf. taste. impl. neighborhood. nearestnuserneighborhood; import Org. apache. mahout. cf. taste. impl. recommender. genericitembasedrecommender; import Org. apache. mahout. cf. taste. impl. recommender. genericuserbasedrecommender; import Org. apache. mahout. cf. taste. model. datamodel; import Org. apache. mahout. cf. taste. neighborhood. userneighborhood; import Org. apache. mahout. cf. taste. recommender. recommendeditem; import Org. apache. mahout. cf. taste. recommender. recommender; import Org. apache. mahout. cf. taste. similarity. itemsimilarity; import Org. apache. mahout. cf. taste. similarity. usersimilarity; import Org. apache. mahout. cf. taste. impl. similarity. pearsoncorrelationsimilarity;/*** user-based recommendation Program * @ author administrator **/public class recommenderintro {public static void main (string [] ARGs) throws exception {// load data files for storage and provide all the preferences required for computing, user and item data datamodel model = new filedatamodel (new file ("Data/intro.csv"); // user similarity, which gives the similarity between two users and has multiple measurement methods, you can change itemsimilarity similarity = new pearsoncorrelationsimilarity (model) directly from a user to an item; // user neighbor, A group of users that are most similar to a given user // userneighborhood neighborhood = new nearestnuserneighborhood (2, // similarity, model); /// recommendation engine, merge these components, recommendation Implementation // recommender = new genericuserbasedrecommender (model, // neighborhood, similarity); // recommender recommender1 = new recommendation (model, similarity) based on items ); // recommended item 1, 1, list <recommendeditem> recommendeditems = recommender1.recommend (1, 1); // output for (recommendeditem item: recommendeditems) {system. out. println (item );}}}
1 item recommended for user 1:
Output result:
14/08/05 12:26:44 INFO file.FileDataModel: Creating FileDataModel for file data\intro.csv14/08/05 12:26:44 INFO file.FileDataModel: Reading file info...14/08/05 12:26:44 INFO file.FileDataModel: Read lines: 2114/08/05 12:26:44 INFO model.GenericDataModel: Processed 5 usersRecommendedItem[item:104, value:5.0]
Compared with user-based recommendations, 104 items are recommended.