一、软硬件环境
操作系统:Windows 7
IDE:Intellij IDEA社区版
Java版本:JDK1.8
Mahout版本:0.12.2
二、搭建步骤
- 安装Java JDK,建议1.6以上;
- 安装IDE,这里我选择Intellij IDEA社区版,免费而且集成Maven。注意设置JDK路径。
- 下载Mahout,我在官网下载的最新版apache-mahout-distribution-0.12.2.zip,解压到某个目录即可。(当然你也可以下载源码自己用maven编译)
三、单机测试
这里实现一个推荐程序,使用基于用户的协同过滤算法(User-based CF),数据集[用户ID, 物品ID,偏好值]
如下:
1,101,5.0
1,102,3.0
1,103,2.5
2,101,2.0
2,102,2.5
2,103,5.0
2,104,2.0
3,101,2.5
3,104,4.0
3,105,4.5
3,107,5.0
4,101,5.0
4,103,3.0
4,104,4.5
4,106,4.0
5,101,4.0
5,102,3.0
5,103,2.0
5,104,4.0
5,105,3.5
5,106,4.0
在Intellij IDEA中创建一个Java项目,并在File->Project Structure中导入jar依赖库:
mahout-mr-0.12.2.jar
mahout-math-0.12.2.jar
$MAHOUT_HOME/lib/*
当然你也可以导入全部jar包。
然后实现基于用户的协同过滤推荐算法:
import java.io.File;
import java.util.List;
import java.io.IOException;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
/**
* Created by SongLee on 2016/11/10.
*/
public class UserCF {
final static int NEIGHBORHOOD_NUM = 2;
final static int RECOMMENDER_NUM = 1;
public static void main(String[] args) throws IOException, TasteException {
String name = "D:/item.csv";
// 创建数据模型
DataModel model = new FileDataModel(new File(name));
// 计算相似度
UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
//
UserNeighborhood neighborhood = new NearestNUserNeighborhood(NEIGHBORHOOD_NUM, similarity, model);
//
Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity);
// 生成推荐结果
List<RecommendedItem> recommendations = recommender.recommend(1, RECOMMENDER_NUM);
for(RecommendedItem recommendation : recommendations) {
System.out.println(recommendation);
}
}
}
运行上面的程序输出:
RecommendedItem[item:104, value:4.257081]
可以知道,推荐程序把物品104推荐给了用户1,因为它评估出用户1对物品104的偏好值约为4.26。