资源简介
该资源是在Eclipse平台里,使用Mahout库的API,实现基于用户的协同过滤算法,从而进行商品推荐。
软件环境是:win7 64位 +Eclipse4.4 + jdk1.6,
用到了7个.jar包,
分别为:commons-logging-1.2.jar,
commons-math3-3.5.jar,guava-18.0.jar,mahout-core-0.9.jar,
mahout-math-0.9.jar,slf4j-api-1.6.1.jar,slf4j-jcl-1.6.1.jar。
代码片段和文件信息
package com.xie;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
import org.apache.mahout.cf.taste.impl.model.file.*;
import org.apache.mahout.cf.taste.impl.neighborhood.*;
import org.apache.mahout.cf.taste.impl.recommender.*;
import org.apache.mahout.cf.taste.impl.similarity.*;
import org.apache.mahout.cf.taste.model.*;
import org.apache.mahout.cf.taste.recommender.*;
import org.apache.mahout.cf.taste.similarity.*;
import java.io.*;
import java.util.*;
public class Userbased {
final static int NEIGHBORHOOD_NUM = 2;
final static int RECOMMENDER_NUM = 3;
public static void main(String[] args) throws IOException TasteException {
String file = “src/data/testCF.csv“;
DataModel model = new FileDataModel(new File(file));
UserSimilarity user = new EuclideanDistanceSimilarity(model);
NearestNUserNeighborhood neighbor = new NearestNUserNeighborhood(NEIGHBORHOOD_NUM user model);
Recommender r = new GenericUserbasedRecommender(model neighbor user);
LongPrimitiveIterator iter = model.getUserIDs();
while (iter.hasNext()) {
long uid = iter.nextLong();
List list = r.recommend(uid RECOMMENDER_NUM);
System.out.printf(“uid:%s“ uid);
for (RecommendedItem ritem : list) {
System.out.printf(“(%s%f)“ ritem.getItemID() ritem.getValue());
}
System.out.println();
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 736 2015-12-23 21:28 ChartTwo\.classpath
文件 384 2015-12-23 21:13 ChartTwo\.project
文件 598 2015-12-23 21:13 ChartTwo\.settings\org.eclipse.jdt.core.prefs
文件 3396 2015-12-23 21:29 ChartTwo\bin\com\xie\Userba
文件 230 2015-12-23 19:27 ChartTwo\bin\data\testCF.csv
文件 61829 2014-07-05 20:11 ChartTwo\Lib\commons-logging-1.2.jar
文件 2038489 2015-04-14 14:35 ChartTwo\Lib\commons-math3-3.5.jar
文件 2256213 2015-12-23 20:45 ChartTwo\Lib\guava-18.0.jar
文件 1470402 2014-04-09 12:28 ChartTwo\Lib\mahout-core-0.9.jar
文件 1612934 2014-04-09 12:26 ChartTwo\Lib\mahout-math-0.9.jar
文件 25496 2010-07-05 22:25 ChartTwo\Lib\slf4j-api-1.6.1.jar
文件 7728 2010-07-05 22:25 ChartTwo\Lib\slf4j-jcl-1.6.1.jar
文件 1602 2015-12-23 21:29 ChartTwo\src\com\xie\Userba
文件 230 2015-12-23 19:27 ChartTwo\src\data\testCF.csv
目录 0 2015-12-23 21:29 ChartTwo\bin\com\xie
目录 0 2015-12-23 21:29 ChartTwo\src\com\xie
目录 0 2015-12-23 21:28 ChartTwo\bin\com
目录 0 2015-12-23 21:28 ChartTwo\bin\data
目录 0 2015-12-23 21:14 ChartTwo\src\com
目录 0 2015-12-23 21:27 ChartTwo\src\data
目录 0 2015-12-23 21:13 ChartTwo\.settings
目录 0 2015-12-23 21:28 ChartTwo\bin
目录 0 2015-12-23 21:21 ChartTwo\Lib
目录 0 2015-12-23 21:27 ChartTwo\src
目录 0 2015-12-23 21:14 ChartTwo
----------- --------- ---------- ----- ----
7480267 25
评论
共有 条评论