# 个人技术总结-协同过滤

131802110-黄宏鹏 学生 2022-06-28 18:20:45

# 目录

• 基于用户的协同过滤算法
• 技术概述
• 技术实现
• 皮尔森系数LIst的计算
• 通过皮尔森系数LIst确定最近邻LIst
• 通过最近邻LIst进行推荐
• 技术总结

# 基于用户的协同过滤算法

## 技术实现

#### 皮尔森系数LIst的计算

`````` public double pearson_dis(List<likelist> rating1, List<likelist> rating2) {
double sum_xy = 0.0D;
double sum_x = 0.0D;
double sum_y = 0.0D;
double sum_x2 = 0.0D;
double sum_y2 = 0.0D;
double n = 0.0D;
double avg_x = 0.0D;
double avg_y = 0.0D;

double sup_xy;
double sup_x22;
for(int i = 0; i < rating1.size(); ++i) {
likelist userName = (likelist)rating1.get(i);

for(int j = 0; j < rating2.size(); ++j) {
likelist recommendUser = (likelist)rating2.get(j);
++n;
sup_xy = (double)(userName.getCount() * 10);
sup_x22 = (double)(recommendUser.getCount() * 10);
sum_xy += sup_xy * sup_x22;
sum_x += sup_xy;
sum_y += sup_x22;
sum_x2 += Math.pow(sup_xy, 2.0D);
sum_y2 += Math.pow(sup_x22, 2.0D);
}
}
}

avg_x = sum_x / n;
avg_y = sum_y / n;
System.out.println("N=" + n + " avg" + avg_x + "avg_x" + avg_y);
double sup_x2 = 0.0D;
double sup_y2 = 0.0D;
sup_xy = 0.0D;
sup_x22 = 0.0D;
double sup_y22 = 0.0D;

for(int i = 0; i < rating1.size(); ++i) {
likelist userName2 = (likelist)rating1.get(i);

for(int j = 0; j < rating2.size(); ++j) {
likelist recommendUser2 = (likelist)rating2.get(j);
double x = (double)(userName2.getCount() * 10);
double y = (double)(recommendUser2.getCount() * 10);
sup_x2 += x - avg_x;
sup_y2 += y - avg_y;
sup_x22 += Math.pow(sup_x2, 2.0D);
sup_y22 += Math.pow(sup_y2, 2.0D);
sup_xy += sup_y2 * sup_x2;
}
}
}

double denominator2 = Math.sqrt(sup_x22 * sup_y22);
System.out.println("denominator2   " + sup_xy + " " + sup_xy + " sup_x2 " + sup_xy + " " + sup_xy + " supy2 " + sup_xy / denominator2);
double denominator = Math.sqrt(sum_x2 - Math.pow(sum_x, 2.0D) / n) * Math.sqrt(sum_y2 - Math.pow(sum_y, 2.0D) / n);
if (denominator2 == 0.0D) {
return 0.0D;
} else {
return sup_xy / denominator2;
}
}
``````

#### 通过皮尔森系数LIst确定最近邻LIst

``````    public Map<String, Double> computeNearestNeighbor(likelist user, Map<String, List<likelist>> likelistMap) {
Map<String, Double> distances = new TreeMap();
Iterator var4 = likelistMap.entrySet().iterator();

while(var4.hasNext()) {
Entry<String, List<likelist>> entry = (Entry)var4.next();
String mapKey = (String)entry.getKey();
List<likelist> mapValue = (List)entry.getValue();
if (!mapKey.equals(user.getUserid().toString())) {
double distance = this.pearson_dis((List)likelistMap.get(user.getUserid().toString()), mapValue);
PrintStream var10000 = System.out;
String var10001 = (String)entry.getKey();
var10000.println("key" + var10001 + " distance" + distance);
distances.put(mapKey, distance);
}
}

System.out.println("distance" + distances);
return distances;
}

``````

#### 通过最近邻LIst进行推荐

`````` public List<Integer> likeListRecommend(likelist user) {
List<Integer> recommendations = new LinkedList();
List<likelist> AllUser = this.likelistMapper.findByUser();
List<String> AllUserId = new ArrayList();
Iterator var5 = AllUser.iterator();

while(var5.hasNext()) {
likelist like = (likelist)var5.next();
}

List<String> listNew = new ArrayList(new TreeSet(AllUserId));
Map<String, List<likelist>> likelistMap = new HashMap();
Iterator var7 = listNew.iterator();

while(var7.hasNext()) {
String userN = (String)var7.next();
likelistMap.put(userN, this.likelistMapper.findById(userN));
}

Map<String, Double> distances = this.computeNearestNeighbor(user, likelistMap);
double maxKey = 0.0D;
List<String> maxValue = new LinkedList();
Iterator var11 = distances.entrySet().iterator();

String neighber;
while(var11.hasNext()) {
Entry<String, Double> entry = (Entry)var11.next();
neighber = (String)entry.getKey();
Double mapValue = (Double)entry.getValue();
if (mapValue >= 0.8D && !neighber.equals(user.getUserid())) {
}
}

List<Integer> RecommendList = new ArrayList();
Iterator var23 = maxValue.iterator();

Iterator var16;
while(var23.hasNext()) {
neighber = (String)var23.next();
List<likelist> flag = (List)likelistMap.get(neighber);
List<Integer> likelistFlagList = new LinkedList();
var16 = flag.iterator();

while(var16.hasNext()) {
likelist likelist = (likelist)var16.next();
}

}

List<Integer> newRecommendList = new ArrayList(new TreeSet(RecommendList));
List<likelist> userList = this.likelistMapper.findById(user.getUserid().toString());
System.out.println("maxvalue" + maxValue);
System.out.println("Recommendations" + RecommendList);
System.out.println("userlist" + userList);
System.out.println("newRecommendations" + newRecommendList);
Iterator var27 = userList.iterator();

while(var27.hasNext()) {
likelist likelist = (likelist)var27.next();
var16 = newRecommendList.iterator();

while(var16.hasNext()) {
Integer i = (Integer)var16.next();
if (likelist.getPostid() == i) {
}
}
}

newRecommendList.removeAll(recommendations);
if (recommendations.isEmpty()) {
}

System.out.println(newRecommendList);
return newRecommendList;
}

``````

...全文
110 回复 打赏 收藏 举报

103

2022-06-28 18:29

2022-06-28 18:20