Data preparation: Test.txt
The first column is UserID, and the second column is Itemid, and the third column preference Value is the score [plain] view plain copy print? 1,101,5 1,102,3 1,103,2.5 2,101,2 2,102,2.5 2,103,5 2,104,2 3,101,2.5 3,104,4 3,105,4.5 3,107,5 4,10 1,5 4,103,3 4,104,4.5 4,106,4 5,101,4 5,102,3 5,103,2 5,104,4 5,105,3.5 5,106,4
1,101,5
1,102,3
1,103,2.5
2,101,2
2,102,2.5
2,103,5 2,104,2 3,101,2.5 3,104,4
3,105,4.5
3,107,5
4,101,5
4,103,3
4,104,4.5
4,106,4 5,101,4 5,102,3
5,103,2
5,104,4
5,105,3.5
5,106,4
Create a new Java project in Eclipse, and then create a new class Recommenderintro in the SRC directory (this is just a test, put it under the default package, no new package), the code is as follows:[Java] View Plain copy print? import org.apache.mahout.cf.taste.impl.model.file.*; import org.apache.mahout.cf.taste.impl.neighborhood.*; import org.apache.mahout.cf.taste.impl.recommender.*; import org.apache.mahout.cf.taste.impl.similarity.*; import org.apache.mahout.cf.taste.model.*; import org.apache.mahout.cf.taste.neighborhood.*; import org.apache.mahout.cf.taste.recommender.*; import org.apache.mahout.cf.taste.similarity.*; import java.io.*; import java.util.*; public class recommenderintro { private recommenderintro () {}; public static void main ( String args[]) throws exception{ // step:1 Building Model 2 Computing similarity 3 lookup K Next 4 construction recommendation engine datamodel model =new filedatamodel ( New file ("/home/test/test-in/test.txt"))//filename must be absolute path usersimilarity similarity =new pearsoncorrelationsimilarity (model); UserNeighborhood neighborhood =new Nearestnuserneighborhood (2,similarity,model); Recommender recommender= new genericuserbasedrecommender (model,neighborhood,similarity); list<recommendeditem> recommendations = Recommender.recommend (1,&NBSP;2)//For user 1 recommend two itemid for (Recommendeditem recommendation :recommendations) { &NBSP;&NBSP;SYSTEM.OUT.PRINTLN (recommendation); } } }
Import org.apache.mahout.cf.taste.impl.model.file.*;
Import org.apache.mahout.cf.taste.impl.neighborhood.*;
Import org.apache.mahout.cf.taste.impl.recommender.*;
Import org.apache.mahout.cf.taste.impl.similarity.*;
Import org.apache.mahout.cf.taste.model.*;
Import org.apache.mahout.cf.taste.neighborhood.*;
Import org.apache.mahout.cf.taste.recommender.*;
Import org.apache.mahout.cf.taste.similarity.*;
Import java.io.*;
Import java.util.*;
public class Recommenderintro {private Recommenderintro () {}; public static void Main (String args[]) throws exception{//step:1 Build Model 2 compute similarity 3 find K close proximity 4 construct recommendation engine Datamod El model =new Filedatamodel (new file ("/home/test/test-in/test.txt");//filename must be absolute path usersimilarity similarity =new Pear
Soncorrelationsimilarity (model);
Userneighborhood Neighborhood =new Nearestnuserneighborhood (2,similarity,model);
Recommender recommender= New Genericuserbasedrecommender (model,neighborhood,similarity); List<recommendeditem> RecommendatIons =recommender.recommend (1, 2);//Two Itemid for (Recommendeditem recommendation:recommendations) {System.out is recommended for user 1.
println (recommendation); }
}
}
At this time will certainly error, because has not imported any related jar package, will Mahout-core-0.4.jar guide to the project, at this time the code will not show the wrong
Then the run as selects Java application, which runs on a single machine (which, of course, can be run on the cluster if it is successfully launched). At this time after running, will be reported that some packets do not exist, according to the prompts, in turn, the package into the project. I was in the process of testing, has imported Slf4j-api-1.6.0.jar,slf4j-jcl-1.6.0.jar,google-collection-1.0-rc2.jar,commons-logging-1.1.1.jar,mahout-math-0.4.jar, Uncommons-maths-1.2.jar, these packages can be found in the Lib directory under the Mahout installation directory. After a step-by-Step packet Guide package (the console report what the package does not exist, I found the package import) after debugging, the last successful operation, the results are as follows:
recommendeditem[item:104, value:4.257081]
recommendeditem[item:106, value:4.0]
Result Analysis: Find user 1 Similar user 2 and user 5, user 2 and user 5 together have 104, so recommended score is high, but I do not know how this score is calculated. There is no other recommendation in User 2, and then there are 105 and 106 in user 5 that can be recommended, while 106 is higher than 105, so the second result is 106, and you don't know if you understand it right. Just guess, have know inside of and know the recommended score how to calculate, please leave a message to share, open source is to promote mutual learning a good way, hehe
Then I looked at how Mahout's taste works, I place the Mahout-taste-webapp-0.4.war in the Mahout installation directory in WebApps under the Tomcat installation directory, then start Tomcat and enter in the browser: http://localhost:8080/ Mahout-taste-webapp-0.4/recommenderservice.jws, you can see the Web service provider (that is, the WebServices service) provided by taste, but it cannot be queried based on the user ID. Estimate should also download the corresponding DataSet and jar package, see based on Apache Mahout build social recommendation engine
Resources:
1.http://www.ibm.com/developerworks/cn/java/j-mahout/
2.http://blog.csdn.net/zhzhl202/article/details/6316570
3.http://www.ibm.com/developerworks/cn/java/j-lo-mahout/