Mahout in Action 在第3.3节 Coping without preference values 提到了没有偏好值的数据场景,能够得到1~5分偏好评分的场景太难了,这需要有一个略微复杂点的价值判断过程.相比之下,"顶"和"踩"就来的简单的多.看下面的图,对于布尔型数据模型,已经简化成为:喜欢,不喜欢,不知道

  

  作者给出了一个"样例代码",这段样例代码运行到最后是会抛出异常的,这个作者也明确指出来了(P37).

  "You should find that running this code results in an IllegalArgumentException from the PearsonCorrelationSimilarity constructor. This may be surprising at first: isn’t GenericBooleanPrefDataModel also a DataModel, and nearly the same as GenericDataModel except that it doesn’t store distinct preference values?"

 

  但是,这个异常还是被提到了论坛里面: http://www.manning-sandbox.com/thread.jspa?threadID=41765

INFO: Processed 943 users
Feb 5, 2011 10:54:31 AM org.slf4j.impl.JCLLoggerAdapter info
INFO: Beginning evaluation using 0.9 of GenericBooleanPrefDataModel[users:1,2,3...]
Exception in thread "main" java.lang.IllegalArgumentException: DataModel doesn't have preference values
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:90)
at org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity.<init>(PearsonCorrelationSimilarity.java:74)
at org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity.<init>(PearsonCorrelationSimilarity.java:66)
at mia.recommender.ch02.RecommenderIntro$6.buildRecommender(RecommenderIntro.java:163)
at org.apache.mahout.cf.taste.impl.eval.AbstractDifferenceRecommenderEvaluator.evaluate(AbstractDifferenceRecommenderEvaluator.java:124)
at mia.recommender.ch02.RecommenderIntro.eg6(RecommenderIntro.java:175)
at mia.recommender.ch02.RecommenderIntro.main(RecommenderIntro.java:38) 

 

    这个其实,耐心点往后读一点就能看到作者的解释.不过,话说回来,怎样才能将这段代码运行通过呢?究其原因是选择了PearsonCorrelationSimilarity相似度算法,而这个算法是要求偏好值的,所以抛出了" DataModel doesn't have preference values"的异常,我们只需要选适当的相似度算法(或者说不需要偏好值的算法)就可以解决这个问题.这里可选的方案有: Tanimoto coefficient算法和 log-likelihood算法,对应到具体的类:TanimotoCoefficientSimilarity LogLikelihoodSimilarity

 

 自己动手试一下吧

 

Mahout in Action 书中对应的代码在项目:https://github.com/tdunning/MiA/tree/master/src/main

 

小图一张