<span style="font-size:18px;">/* * 代码mahout实现训练数据和评分 * 这里输出测试这个推荐程序的评分 * 评分越低意味着估计值与实际实际偏好值得差别越小 * 0.0为完美的估计 * */ package byuser; import java.io.File; import org.apache.mahout.cf.taste.common.TasteException; import org.apache.mahout.cf.taste.eval.RecommenderBuilder; import org.apache.mahout.cf.taste.eval.RecommenderEvaluator; import org.apache.mahout.cf.taste.impl.eval.AverageAbsoluteDifferenceRecommenderEvaluator; 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.Recommender; import org.apache.mahout.cf.taste.similarity.UserSimilarity; import org.apache.mahout.common.RandomUtils; public class RecommenderEvaluatorStudy { //无参构造 public RecommenderEvaluatorStudy(){ } public static void main(String[] args) { try{ //每次生成的随机数都相同 //因此随机生成可以重复的结果 //这里是为了测试,实际代码中请勿使用 RandomUtils.useTestSeed(); //构建推荐的数据模型 DataModel model = new FileDataModel(new File("E:\\mahout项目\\examples\\intro.csv")); RecommenderEvaluator evaluator = new AverageAbsoluteDifferenceRecommenderEvaluator(); RecommenderBuilder builder = new RecommenderBuilder(){ @Override public Recommender buildRecommender(DataModel model) throws TasteException{ UserSimilarity similarity = new PearsonCorrelationSimilarity(model); UserNeighborhood neighborhood = new NearestNUserNeighborhood(2, similarity, model); return new GenericUserBasedRecommender(model, neighborhood, similarity); } }; //这里的数据意思是训练70%,测试30%的数据 //这里的数据如果显示出现了NAN,就表示计算数据出现了问题NAN: not a number //你只需要修改一下参数,我这里改成了0.9 double score = evaluator.evaluate(builder, null, model, 0.9, 1.0); System.out.println("分值为:" + score); }catch(Exception e){ e.printStackTrace(); } } } </span>
原图:
这里为NAN: Not A Number的意思
修改参数后的图片:
时间: 2024-09-30 04:09:40