经典协同过滤算法:Slope One 的原理及实现(Java)
  WaYJTbj6RMqU 2023年11月02日 58 0


本文首先介绍了Slope One算法的原理,然后给出了算法的Java版实现。

一. Slope One 算法的原理介绍

用户         对事物A打分对事物B打分X34Y24Z4?

    用户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;
02	 
03	import java.util.HashMap;
04	import java.util.List;
05	import java.util.Map;
06	import java.util.Set;
07	 
08	import org.slf4j.Logger;
09	import org.slf4j.LoggerFactory;
10	 
11	import com.liangtee.slopeone.recommender.DataSetModel;
12	import com.liangtee.slopeone.recommender.Recommender;
13	import com.liangtee.slopeone.vo.RecommendedItem;
14	 
15	/**
16	 * Weighted Slope One Algorithm
17	 *
18	 * Just for Fun
19	 *
20	 * @author LiangTE
21	 *
22	 */
23	 
24	public class SlopeOneRecommender implements Recommender {
25	 
26	    private static final Logger log = LoggerFactory.getLogger(SlopeOneRecommender.class);
27	     
28	    private DataSetModel dataSet = null;
29	     
30	    public SlopeOneRecommender(DataSetModel dataSet) {
31	        this.dataSet = dataSet;
32	    }
33	 
34	    @Override
35	    public float preferenceOnItem(long userID, long ItemID) throws Exception {
36	         
37	        if(!dataSet.containsUser(userID)) {
38	            throw new Exception("not contains this User : " + userID);
39	        }
40	         
41	        Set<Long> UIDs = dataSet.getUserIDs();
42	        Map<Long, Float> targetUserRatings = dataSet.getUserRatings(userID);
43	        Set<Long> ratedItems = targetUserRatings.keySet();
44	         
45	        Map<Long, Float> diffCache = new HashMap<Long, Float>();
46	        Map<Long, Integer> counter = new HashMap<Long, Integer>();
47	         
48	        log.info("Slope One start to work...");
49	         
50	        for(long UID : UIDs) {                                                  //计算每个用户的差值
51	            if(UID != userID) {
52	                Map<Long, Float> ratings = dataSet.getUserRatings(UID);
53	                for(long iid : ratedItems) {                                    // iid = item id
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);
58	                        } else {
59	                            diffCache.put(iid, diff);
60	                        }
61	                        if(counter.containsKey(iid)) {
62	                            counter.put(iid, counter.get(iid)+1);
63	                        } else {
64	                            counter.put(iid, 1);
65	                        }
66	                    }
67	                }
68	            }
69	             
70	        }
71	         
72	        int K = 0;
73	        float total = 0F;
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;
79	                total += p;
80	                K++;
81	            }
82	        }
83	         
84	         
85	        return total/K;
86	    }


 

   开源的Slope one的程序包

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  kG7ef0NqClb6   2023年11月13日   32   0   0 java
  u2N3sQ7nC0dn   2023年11月13日   28   0   0 java
  rCd1NYtlhh0U   2023年11月13日   31   0   0 java
  rCd1NYtlhh0U   2023年11月13日   35   0   0 java
  Ydqp9G8hCwM0   2023年11月13日   35   0   0 java
WaYJTbj6RMqU