所需jar包
数据格式以逗号分隔
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.0 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 6,102,4.0 6,103,2.0 6,105,3.5 6,107,4.0
基于用户推荐
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.impl.similarity.PearsonCorrelationSimilarity; 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; public class UserItemRecommend { public static void main(String[] args) throws Exception{ //创建数据模型 DataModel dm = new FileDataModel(new File("C:/test.txt")); //使用user来推荐,计算相似度 UserSimilarity us=new PearsonCorrelationSimilarity(dm); //查找K(3)近邻 UserNeighborhood unb=new NearestNUserNeighborhood(3, us, dm); //构造推荐引擎 Recommender re =new GenericUserBasedRecommender(dm, unb, us); //显示推荐结果,为1号用户推荐两个商品 List<RecommendedItem> list = re.recommend(1, 2); for(RecommendedItem recommendedItem :list) { System.out.println(recommendedItem); } } }
推荐结果
RecommendedItem[item:104, value:4.257081] RecommendedItem[item:106, value:4.0]
基于商品
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.recommender.GenericItemBasedRecommender; import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; import org.apache.mahout.cf.taste.model.DataModel; import org.apache.mahout.cf.taste.recommender.RecommendedItem; import org.apache.mahout.cf.taste.recommender.Recommender; import org.apache.mahout.cf.taste.similarity.ItemSimilarity; public class ItemUserRecommend { public static void main(String[] args) throws Exception{ //创建数据模型 DataModel dm = new FileDataModel(new File("C:/test.txt")); ItemSimilarity is=new PearsonCorrelationSimilarity(dm); //构造推荐引擎 Recommender re =new GenericItemBasedRecommender(dm,is); //显示推荐结果,为1号用户推荐两个商品 List<RecommendedItem> list = re.recommend(1, 2); for(RecommendedItem recommendedItem :list) { System.out.println(recommendedItem); } } }
slopeone算法,0.9版本已移除,要使用只能用0.8
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.recommender.GenericItemBasedRecommender; import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity; import org.apache.mahout.cf.taste.model.DataModel; import org.apache.mahout.cf.taste.recommender.RecommendedItem; import org.apache.mahout.cf.taste.recommender.Recommender; import org.apache.mahout.cf.taste.similarity.ItemSimilarity; public class SlopeOneRecommend { public static void main(String[] args) throws Exception{ //创建数据模型 DataModel dm = new FileDataModel(new File("C:/test.txt")); //构造推荐引擎 Recommender re =new SlopeOneRecommender(dm);; //显示推荐结果,为1号用户推荐两个商品 List<RecommendedItem> list = re.recommend(1, 2); for(RecommendedItem recommendedItem :list) { System.out.println(recommendedItem); } } }
本文出自 “点滴积累” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1713455
时间: 2024-09-17 03:05:17