1. Prepare data:
Intro.csv:
1,101, 5.0
1,102, 3.0
1,103, 2.5
2,101, 2.0
2,102, 2.5
2,103, 5.0
2,104, 2.0
3,101, 2.5
3,104, 4.0
3,105, 4.5
3,107, 5.0
4,101, 5.0
4,103, 3.0
4,104, 4.5
4,106, 4.0
5,101, 4.0
5,102, 3.0
5,103, 2.0
5,104, 4.0
5,105, 3.5
5,106, 4.0
2. Programming implementation:
Purpose: To recommend a product for user 1:
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. 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. 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, giving the similarity between the two users, there are multiple measurement methods: usersimilarity similarity = new pearsoncorrelationsimilarity (model); // user neighbor, which is the most similar to the user userneighborhood neighborhood = new neighbor (2, similarity, model ); // recommender = new genericuserbasedrecommender (model, neighborhood, similarity ); // recommended item 1, 1, and list for user 1 <recommendeditem> recommendeditems = recommender. recommend (1, 1); // output for (recommendeditem item: recommendeditems) {system. out. println (item );}}}
Output result:
14/08/04 08:46:31 INFO file.FileDataModel: Creating FileDataModel for file data\intro.csv14/08/04 08:46:31 INFO file.FileDataModel: Reading file info...14/08/04 08:46:31 INFO file.FileDataModel: Read lines: 2114/08/04 08:46:31 INFO model.GenericDataModel: Processed 5 usersRecommendedItem[item:104, value:4.257081]
Of course, you can also recommend multiple items, that is, recommender. Recommend (1, N.
The recommendation results are good.