/* * 这段程序对于基于余弦相似度的评估 * */ 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.neighborhood.NearestNUserNeighborhood; import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender; import org.apache.mahout.cf.taste.impl.similarity.EuclideanDistanceSimilarity; 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.cf.taste.similarity.precompute.example.GroupLensDataModel; public class GenericRecByGroupLens_Evalu { public GenericRecByGroupLens_Evalu() throws Exception{ DataModel model = new GroupLensDataModel(new File("E:\\mahout项目\\examples\\ratings.dat")); RecommenderEvaluator evaluator = new AverageAbsoluteDifferenceRecommenderEvaluator(); RecommenderBuilder recommenderBuilder = new RecommenderBuilder() { @Override public Recommender buildRecommender(DataModel model) throws TasteException { //PearsonCoreCOnrrelationSimilarity是皮尔逊相关系数的算法使用 UserSimilarity similarity = new PearsonCorrelationSimilarity(model); //这里使用的是基于欧式距离定义相似度的算法 UserSimilarity similarity1 = new EuclideanDistanceSimilarity(model); //这里使用余弦相似性度量,以前的名字是:CosineMeasureSimilarity现在改成了如下的名字: UserSimilarity similarity2 = new PearsonCorrelationSimilarity(model); UserNeighborhood neighborhood = new NearestNUserNeighborhood(100, similarity2, model); return new GenericUserBasedRecommender(model, neighborhood, similarity2); } }; double score = evaluator.evaluate(recommenderBuilder, null, model, 0.95, 0.05); System.out.println("基于余弦相似度的推荐引擎的评测得分是: " + score); } public static void main(String[] args) throws Exception { // TODO Auto-generated method stub GenericRecByGroupLens_Evalu eva = new GenericRecByGroupLens_Evalu(); } }
如图:
这里和上一篇博客的图片进行对比:
优于皮尔逊,略逊与欧氏距离。
时间: 2024-10-04 18:21:00