下面简单通过在测试集上验证错误值 (JAVA)
package xyz.pl8.evaluatorintro;
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 java.io.File;
public class EvaluatorIntro {
public static void main(String[] args){
try{
DataModel model = new FileDataModel(new File("/home/hadoop/ua.base"));
System.out.println(model);
Recommender recommender = null;
RecommenderBuilder recommenderBuilder = new RecommenderBuilder() {
public Recommender buildRecommender(DataModel model) throws TasteException {
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood = new NearestNUserNeighborhood(100, similarity, model );
return new GenericUserBasedRecommender(model, neighborhood, similarity);
}
};
RecommenderEvaluator avgEvaluator = new AverageAbsoluteDifferenceRecommenderEvaluator();
RecommenderEvaluator rmsEvaluator = new RMSRecommenderEvaluator();
// 平均绝对值误差
double avgScore = avgEvaluator.evaluate(recommenderBuilder, null, model, 0.7, 1.0);
// 根方差错误
double rmsScore = rmsEvaluator.evaluate(recommenderBuilder, null, model, 0.7, 1.0);
System.out.println(avgScore);
System.out.println(rmsScore);
}catch (Exception e){
e.printStackTrace();
}
}
}
以下是通过信息检索, 进行多维度的评价模型的优劣度(java)
package xyz.pl8.irevaluatorintro;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.eval.IRStatistics;
import org.apache.mahout.cf.taste.eval.RecommenderBuilder;
import org.apache.mahout.cf.taste.eval.RecommenderEvaluator;
import org.apache.mahout.cf.taste.eval.RecommenderIRStatsEvaluator;
import org.apache.mahout.cf.taste.impl.eval.AverageAbsoluteDifferenceRecommenderEvaluator;
import org.apache.mahout.cf.taste.impl.eval.GenericRecommenderIRStatsEvaluator;
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 java.io.File;
public class IREvaluatorIntro {
public static void main(String[] args){
try{
DataModel model = new FileDataModel(new File("/home/hadoop/ua.base"));
Recommender recommender = null;
RecommenderBuilder recommenderBuilder = new RecommenderBuilder() {
public Recommender buildRecommender(DataModel model) throws TasteException {
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood = new NearestNUserNeighborhood(100, similarity, model );
return new GenericUserBasedRecommender(model, neighborhood, similarity);
}
};
// 构造信息检索评估器
RecommenderIRStatsEvaluator evaluator = new GenericRecommenderIRStatsEvaluator();
// 进行评估
IRStatistics stats = evaluator.evaluate(recommenderBuilder, null, model, null, 5, 0.7, 1.0);
// 输出精确度,召回率, F1测度
System.out.println(stats.getPrecision());
System.out.println(stats.getRecall());
System.out.println(stats.getF1Measure());
}catch (Exception e){
e.printStackTrace();
}
}
}