Mahout:采用斯皮尔曼相关系数基于相对排名定义相似度

//此处为UserSimilarity实现引入缓存的机制
//采用斯皮尔曼相关系数基于相对排名定义相似度
//其中加入了缓存的机制,将得到的结果进行内部的缓存
//然后,当需要进行提供一个已经计算过的用户间相似度时,
//就可以直接返回,而不需要进行重新的进行计算
//但是这个方法的代价很高,很耗内存
package byuser;
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.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.CachingUserSimilarity;
import org.apache.mahout.cf.taste.impl.similarity.SpearmanCorrelationSimilarity;
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 SpearManCorrelationSimilarityTest {

	public SpearManCorrelationSimilarityTest() throws TasteException, IOException{
		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 {
				UserSimilarity similarity = new CachingUserSimilarity(new SpearmanCorrelationSimilarity(model), model);
				UserNeighborhood neighborhood = new NearestNUserNeighborhood(100, similarity, model);
				return new GenericUserBasedRecommender(model, neighborhood, similarity);
			}
		};
		//这里evaluate()函数trainingPercentage从0.95升到0.99
		//从而让数据规模从5%减为1%
		double score = evaluator.evaluate(recommenderBuilder, null, model, 0.99, 0.05);
		System.out.println("采用斯皮尔曼相关系数的推荐引擎的评测得分是: " + score);
	}
	public static void main(String[] args) throws IOException, TasteException {
		// TODO Auto-generated method stub
		SpearManCorrelationSimilarityTest tt = new SpearManCorrelationSimilarityTest();
	}

}

如图:

Mahout:采用斯皮尔曼相关系数基于相对排名定义相似度_第1张图片

你可能感兴趣的:(Mahout:采用斯皮尔曼相关系数基于相对排名定义相似度)