Mahout实战---运行第一个推荐引擎
创建输入
创建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
创建推荐程序
由于项目在eclipse下,所以先获取项目额根目录String projectDir = System.getProperty("user.dir");
package com.xxx; import java.io.File; import java.io.IOException; import java.util.List; import org.apache.mahout.cf.taste.common.TasteException; 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; /** * 简单的使用皮尔逊相关系数进行推荐 * @author * */ public class RecommenderIntro { public static void main(String[] args) throws IOException, TasteException { String projectDir = System.getProperty("user.dir"); DataModel model = new FileDataModel(new File(projectDir + "/src/main/intro.csv")); UserSimilarity similarity = new PearsonCorrelationSimilarity(model); UserNeighborhood neighborhood = new NearestNUserNeighborhood(2, similarity, model); Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity); List<RecommendedItem> recommendedItems = recommender.recommend(1, 1); for (RecommendedItem recommendedItem : recommendedItems) { System.out.println(recommendedItem); } } }
推荐程序的步骤是:1,输入user-item矩阵数据 2,选择合适的相似度计算方法(程序中使用的是皮尔逊相关系数)3,构造N最近邻 4,根据邻居产生推荐结果
对应到mahout程序就是上述代码中写的。这个很简单,没毛病,下面是运行结果
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步