资源简介
基于用户最近邻模型的协同过滤算法的JAVA代码实现,用户相似度用Pearson相关系数进行计算。
代码片段和文件信息
/*
* To change this template choose Tools | Templates
* and open the template in the editor.
*/
package recommendation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class testRecommend {
public static void main(String[] args) {
Map> userPerfMap = new HashMap>();
Map pref1 = new HashMap();
pref1.put(“A“ 3);
pref1.put(“B“ 4);
pref1.put(“C“ 3);
pref1.put(“D“ 5);
pref1.put(“E“ 1);
pref1.put(“F“ 4);
userPerfMap.put(“p1“ pref1);
Map pref2 = new HashMap();
pref2.put(“A“ 2);
pref2.put(“B“ 4);
pref2.put(“C“ 4);
pref2.put(“D“ 5);
pref2.put(“E“ 3);
pref2.put(“F“ 2);
userPerfMap.put(“p2“ pref2);
Map pref3 = new HashMap();
pref3.put(“A“ 3);
pref3.put(“B“ 5);
pref3.put(“C“ 4);
pref3.put(“D“ 5);
pref3.put(“E“ 2);
pref3.put(“F“ 1);
userPerfMap.put(“p3“ pref3);
Map pref4 = new HashMap();
pref4.put(“A“ 2);
pref4.put(“B“ 2);
pref4.put(“C“ 3);
pref4.put(“D“ 4);
pref4.put(“E“ 3);
pref4.put(“F“ 2);
userPerfMap.put(“p4“ pref4);
Map pref5 = new HashMap();
pref5.put(“A“ 4);
pref5.put(“B“ 4);
pref5.put(“C“ 4);
pref5.put(“D“ 5);
pref5.put(“E“ 1);
pref5.put(“F“ 0);
userPerfMap.put(“p5“ pref5);
Map simUserSimMap = new HashMap();
String output1 = “皮尔逊相关系数:“ output2 = “欧几里得距离:“;
for (Entry> userPerfEn : userPerfMap.entrySet()) {
String userName = userPerfEn.getKey();
if (!“p5“.equals(userName)) {
double sim = getUserSimilar(pref5 userPerfEn.getValue());
double distance = getEuclidDistance(pref5 userPerfEn.getValue());
output1 += “p5与“ + userName + “之间的相关系数:“ + sim + ““;
output2 += “p5与“ + userName + “之间的距离:“ + distance + ““;
simUserSimMap.put(userName sim);
}
}
System.out.println(output1);
System.out.println(output2);
Map> simUserObjMap = new HashMap>();
Map pobjMap1 = new HashMap();
pobjMap1.put(“一夜惊喜“ 3);
pobjMap1.put(“环太平洋“ 4);
pobjMap1.put(“变形金刚“ 3);
simUserObjMap.put(“p1“ pobjMap1);
M
评论
共有 条评论