本文首先介绍了Slope One算法的原理,然后给出了算法的Java版实现。
一. Slope One 算法的原理介绍
用户Z对事物B的打分可能是多少呢?股票上有个说法是平均值可以掩盖一切异常波动,所以股票上的各个技术指标收拾不同时间段的平均值的曲线图或者柱状图等。同样的,Slope One算法也认为:平均值也可以代替某两个未知个体之间的打分差异,事物A对事物B的平均很差是:((3 - 4) + (2 - 4)) / 2 = -1.5,也就是说人们对事物B的打分一般比事物A的打分要高1.5,于是Slope one算法就猜测Z对事物B的打分是4 + 1.5 = 5.5
是不是非常的简单?
加权算法
有n个人对事物A和事物B打分了,R(A->B)表示这n个人对A和对B打分的平均差(A-B),有m个人对事物B和事物C打分 了,R(B->C)表示这m个人对B和对C打分的平均差(B-C),注意都是平均差而不是平方差,现在某个用户对A的打分是ra,对C的打分是 rc,那么A对B的打分可能是:
rb = (n * (ra - R(A->B)) + m * (rc + R(B->C)))/(m+n)
二. Slope One 算法的实现
我自己实现的Slope One:
01 | package com.liangtee.slopeone.recommender.impl; |
03 | import java.util.HashMap; |
04 | import java.util.List; |
08 | import org.slf4j.Logger; |
09 | import org.slf4j.LoggerFactory; |
11 | import com.liangtee.slopeone.recommender.DataSetModel; |
12 | import com.liangtee.slopeone.recommender.Recommender; |
13 | import com.liangtee.slopeone.vo.RecommendedItem; |
16 | * Weighted Slope One Algorithm |
24 | public class SlopeOneRecommender implements Recommender { |
26 | private static final Logger log = LoggerFactory.getLogger(SlopeOneRecommender. class ); |
28 | private DataSetModel dataSet = null ; |
30 | public SlopeOneRecommender(DataSetModel dataSet) { |
31 | this .dataSet = dataSet; |
35 | public float preferenceOnItem( long userID, long ItemID) throws Exception { |
37 | if (!dataSet.containsUser(userID)) { |
38 | throw new Exception( "not contains this User : " + userID); |
41 | Set<Long> UIDs = dataSet.getUserIDs(); |
42 | Map<Long, Float> targetUserRatings = dataSet.getUserRatings(userID); |
43 | Set<Long> ratedItems = targetUserRatings.keySet(); |
45 | Map<Long, Float> diffCache = new HashMap<Long, Float>(); |
46 | Map<Long, Integer> counter = new HashMap<Long, Integer>(); |
48 | log.info( "Slope One start to work..." ); |
50 | for ( long UID : UIDs) { |
52 | Map<Long, Float> ratings = dataSet.getUserRatings(UID); |
53 | for ( long iid : ratedItems) { |
54 | if (ratings.containsKey(ItemID) && ratings.containsKey(iid)) { |
55 | float diff = ratings.get(ItemID) - ratings.get(iid); |
56 | if (diffCache.containsKey(iid)) { |
57 | diffCache.put(iid, diffCache.get(iid)+diff); |
59 | diffCache.put(iid, diff); |
61 | if (counter.containsKey(iid)) { |
62 | counter.put(iid, counter.get(iid)+ 1 ); |
74 | for ( long iid : ratedItems) { |
75 | if (diffCache.containsKey(iid)) { |
76 | float bias = diffCache.get(iid)/counter.get(iid); |
77 | float targetUserRating = targetUserRatings.get(iid); |
78 | float p = targetUserRating + bias; |
开源的Slope one的程序包