首页 > 代码库 > mahout推荐2
mahout推荐2
设计好了一个推荐程序,如何来完成评估呢?
一般是使用一个真实数据的样例作为测试数据来仿真,来看估计值和实际值的差别,0.0意味着完美的估计,就是没有差别。
一是使用平均差值(直观,易于理解),一是使用均方根。
针对mahout推荐1的推荐程序进行评估:
package mahout;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.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 { public static void main(String[] args) throws Exception { //强制每次生成相同的随机值,生成可重复的结果 RandomUtils.useTestSeed(); //数据装填 DataModel model = new FileDataModel(new File("data/intro.csv")); //推荐评估,使用平均值 RecommenderEvaluator evaluator = new AverageAbsoluteDifferenceRecommenderEvaluator(); //推荐评估,使用均方差 //RecommenderEvaluator evaluator = new RMSRecommenderEvaluator(); //用于生成推荐引擎的构建器,与上一例子实现相同 RecommenderBuilder builder = new RecommenderBuilder() { public Recommender buildRecommender(DataModel model) throws TasteException { // TODO Auto-generated method stub //用户相似度,多种方法 UserSimilarity similarity = new PearsonCorrelationSimilarity(model); //用户邻居 UserNeighborhood neighborhood = new NearestNUserNeighborhood(2, similarity, model); //一个推荐器 return new GenericUserBasedRecommender(model, neighborhood, similarity); } }; //推荐程序评估值(平均差值)训练90%的数据,测试数据10%,《mahout in Action》使用的是0.7,但是出现结果为NaN double score = evaluator.evaluate(builder, null, model, 0.9, 1.0); System.out.println(score); }}
结果:
14/08/04 09:16:21 INFO file.FileDataModel: Creating FileDataModel for file data\intro.csv14/08/04 09:16:21 INFO file.FileDataModel: Reading file info...14/08/04 09:16:21 INFO file.FileDataModel: Read lines: 2114/08/04 09:16:21 INFO model.GenericDataModel: Processed 5 users14/08/04 09:16:21 INFO eval.AbstractDifferenceRecommenderEvaluator: Beginning evaluation using 0.9 of FileDataModel[dataFile:D:\workspace\zoodemo\data\intro.csv]14/08/04 09:16:21 INFO model.GenericDataModel: Processed 5 users14/08/04 09:16:21 INFO eval.AbstractDifferenceRecommenderEvaluator: Beginning evaluation of 3 users14/08/04 09:16:21 INFO eval.AbstractDifferenceRecommenderEvaluator: Starting timing of 3 tasks in 4 threads14/08/04 09:16:21 INFO eval.StatsCallable: Average time per recommendation: 0ms14/08/04 09:16:21 INFO eval.StatsCallable: Approximate memory used: 11MB / 62MB14/08/04 09:16:21 INFO eval.StatsCallable: Unable to recommend in 2 cases14/08/04 09:16:21 INFO eval.AbstractDifferenceRecommenderEvaluator: Evaluation result: 1.01.0
上面有一个:Unable to recommend in 2 cases,就是说有两个测试数据无法完成推荐。
2、如果将训练数据改为0.7或者0.8结果会是一个NaN
14/08/04 09:20:22 INFO file.FileDataModel: Creating FileDataModel for file data\intro.csv14/08/04 09:20:22 INFO file.FileDataModel: Reading file info...14/08/04 09:20:22 INFO file.FileDataModel: Read lines: 2114/08/04 09:20:22 INFO model.GenericDataModel: Processed 5 users14/08/04 09:20:22 INFO eval.AbstractDifferenceRecommenderEvaluator: Beginning evaluation using 0.7 of FileDataModel[dataFile:D:\workspace\zoodemo\data\intro.csv]14/08/04 09:20:22 INFO model.GenericDataModel: Processed 5 users14/08/04 09:20:22 INFO eval.AbstractDifferenceRecommenderEvaluator: Beginning evaluation of 4 users14/08/04 09:20:22 INFO eval.AbstractDifferenceRecommenderEvaluator: Starting timing of 4 tasks in 4 threads14/08/04 09:20:22 INFO eval.StatsCallable: Average time per recommendation: 0ms14/08/04 09:20:22 INFO eval.StatsCallable: Approximate memory used: 11MB / 62MB14/08/04 09:20:22 INFO eval.StatsCallable: Unable to recommend in 9 cases14/08/04 09:20:22 INFO eval.AbstractDifferenceRecommenderEvaluator: Evaluation result: NaNNaN
Unable to recommend in 9 cases ,9个不能完成,太多了。
不过《Mahout in Action》使用的是0.7,出现了结果为1.0的情况,不知是怎么弄的。
3、当然我们也可以换成使用均方根的测试:结果如下(打开注释就可以了)
14/08/04 09:22:53 INFO file.FileDataModel: Creating FileDataModel for file data\intro.csv14/08/04 09:22:53 INFO file.FileDataModel: Reading file info...14/08/04 09:22:53 INFO file.FileDataModel: Read lines: 2114/08/04 09:22:53 INFO model.GenericDataModel: Processed 5 users14/08/04 09:22:53 INFO eval.AbstractDifferenceRecommenderEvaluator: Beginning evaluation using 0.9 of FileDataModel[dataFile:D:\workspace\zoodemo\data\intro.csv]14/08/04 09:22:53 INFO model.GenericDataModel: Processed 5 users14/08/04 09:22:53 INFO eval.AbstractDifferenceRecommenderEvaluator: Beginning evaluation of 3 users14/08/04 09:22:53 INFO eval.AbstractDifferenceRecommenderEvaluator: Starting timing of 3 tasks in 4 threads14/08/04 09:22:53 INFO eval.StatsCallable: Average time per recommendation: 0ms14/08/04 09:22:53 INFO eval.StatsCallable: Approximate memory used: 11MB / 62MB14/08/04 09:22:53 INFO eval.StatsCallable: Unable to recommend in 2 cases14/08/04 09:22:53 INFO eval.AbstractDifferenceRecommenderEvaluator: Evaluation result: 1.01.0
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。