1. 程式人生 > >mahout入門之對推薦結果進行評估

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);
		
	}

}
最後得出的評估值越小,說明推薦結果越好