Mahout Recommendation 1

Source: Internet
Author: User
Tags file info

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.