mahout入门之对推荐结果进行评估

package com.besttone.mahout.demo.recommender;

import java.io.File;
import java.io.IOException;

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.eval.RMSRecommenderEvaluator;
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 TestRecommenderEvaluator {

	/**
	 * @param args
	 * @throws IOException
	 * @throws TasteException
	 */
	public static void main(String[] args) throws IOException, TasteException {
		// TODO Auto-generated method stub
		//这个是产生唯一的种子使得在划分训练和测试数据的时候具有唯一性= 
		RandomUtils.useTestSeed();
		// 装载数据文件,实现存储,并为计算提供所需的用户ID,物品ID,偏好值
		DataModel dataModel = new FileDataModel(new File(
				MyFirstRecommender.class.getResource("intro.txt").getPath()));
		 //推荐评估,使用均方根
		//RecommenderEvaluator evaluator = new RMSRecommenderEvaluator();
		// //推荐评估,使用平均差值
		RecommenderEvaluator evaluator = new AverageAbsoluteDifferenceRecommenderEvaluator();
		RecommenderBuilder builder = new RecommenderBuilder() {

			public Recommender buildRecommender(DataModel dataModel)
					throws TasteException {
				// TODO Auto-generated method stub
				UserSimilarity similarity = new PearsonCorrelationSimilarity(
						dataModel);
				UserNeighborhood neighborhood = new NearestNUserNeighborhood(2,
						similarity, dataModel);

				return new GenericUserBasedRecommender(dataModel, neighborhood,
						similarity);
			}
		};
		//1.0表示待评估的数据集与总数据集的占比,1.0表示100%。 0。9表示训练数据集在评估数据集的占比
		double score = evaluator.evaluate(builder, null, dataModel, 0.9, 1.0);
		System.out.println(score);
		
	}

}
最后得出的评估值越小,说明推荐结果越好

你可能感兴趣的:(mahout入门之对推荐结果进行评估)