SlopeOne推荐算法
|
|
101
|
102
|
103
|
104
|
| UserX | 5 |
3.5
|
|
|
|
UserY
|
2
|
5
|
4
|
2
|
|
UserZ
|
4.5
|
3.5
|
1
|
4
|
求物品两两之间的差值平均分:
|
|
101
|
102
|
103
|
104
|
|
101
|
|
|
|
|
|
102
|
0.17
|
|
|
|
|
103
|
-0.75
|
-1.75
|
|
|
|
104
|
-0.25
|
-1.25
|
0.5
|
|
OK,现在准备工作已经完成了,然后进行推荐,例如对X用户进行推荐,103和104个预测评分根据101、102的评分来的。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import org.apache.commons.cli2.OptionException;
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.FileDataModel;
import org.apache.mahout.cf.taste.impl.recommender.CachingRecommender;
import org.apache.mahout.cf.taste.impl.recommender.slopeone.SlopeOneRecommender;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
public class App
{
static final String inputFile = "/mnt/new/ml-1m/ratings.dat";
static final String outputFile = "/mnt/new/ml-1m/ratings.csv";
public static void main( String[] args ) throws IOException, TasteException, OptionException
{
CreateCsvRatingsFile();
//创建模型数据源文件
File ratingsFile = new File(outputFile);
DataModel model = new FileDataModel(ratingsFile);
// SlopeOne算法
CachingRecommender cachingRecommender = new CachingRecommender(new SlopeOneRecommender(model));
// 对所有用户进行推荐
for (LongPrimitiveIterator it = model.getUserIDs(); it.hasNext();){
long userId = it.nextLong();
// 对单个User进行推荐
List<RecommendedItem> recommendations = cachingRecommender.recommend(userId, 10);
// 该用户无推荐结果
if (recommendations.size() == 0){
System.out.print("User ");
System.out.print(userId);
System.out.println(": no recommendations");
}
// 打印推荐信息
for (RecommendedItem recommendedItem : recommendations) {
System.out.print("User ");
System.out.print(userId);
System.out.print(": ");
System.out.println(recommendedItem);
}
}
}
//读文件前1000行作为模型输入
private static void CreateCsvRatingsFile() throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader(inputFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
String line = null;
String line2write = null;
String[] temp;
int i = 0;
while ((line = br.readLine()) != null && i < 1000)
{
i++;
temp = line.split("::");
line2write = temp[0] + "," + temp[1];
bw.write(line2write);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
参考资料:1,Mahout cookbook;2,http://weibo.com/bicloud 网友写的SlopeOnePPT
SlopeOne推荐算法的更多相关文章
- java和python实现一个加权SlopeOne推荐算法
一.加权SlopeOne算法公式: (1).求得所有item之间的评分偏差 上式中分子部分为项目j与项目i的偏差和,分母部分为所有同时对项目j与项目i评分的用户数 (2).加权预测评分 项目j与项目i ...
- Mahout推荐算法API详解
转载自:http://blog.fens.me/mahout-recommendation-api/ Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, ...
- Mahout推荐算法基础
转载自(http://www.geek521.com/?p=1423) Mahout推荐算法分为以下几大类 GenericUserBasedRecommender 算法: 1.基于用户的相似度 2.相 ...
- 转】Mahout推荐算法API详解
原博文出自于: http://blog.fens.me/mahout-recommendation-api/ 感谢! Posted: Oct 21, 2013 Tags: itemCFknnMahou ...
- Mahout推荐算法之SlopOne
Mahout推荐算法之SlopOne 一. 算法原理 有别于基于用户的协同过滤和基于item的协同过滤,SlopeOne采用简单的线性模型估计用户对item的评分.如下图,估计UserB对 ...
- [转]Mahout推荐算法API详解
Mahout推荐算法API详解 Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, Zookeepe ...
- 推荐算法之 slope one 算法
1.示例引入 多个吃货在某美团的某家饭馆点餐,如下两道菜: 可乐鸡翅: 红烧肉: 顾客吃过后,会有相关的星级评分.假设评分如下: 评分 可乐鸡翅 红烧肉 小明 4 5 小红 4 3 小伟 2 3 小芳 ...
- 从分类,排序,top-k多个方面对推荐算法稳定性的评价
介绍 论文名: "classification, ranking, and top-k stability of recommendation algorithms". 本文讲述比 ...
- Mahout推荐算法API具体解释【一起学Mahout】
阅读导读: 1.mahout单机内存算法实现和分布式算法实现分别存在哪些问题? 2.算法评判标准有哪些? 3.什么会影响算法的评分? 1. Mahout推荐算法介绍 Mahout推荐算法,从数据处理能 ...
随机推荐
- ios大文件存储
I am using Erica Sadun's method of Asynchronous Downloads (link here for the project file: download) ...
- Git之Github使用(一):Push代码到Github
Git之Github使用(一):Push代码到Github 热度 2已有 58 次阅读2016-8-26 17:56 |个人分类:常见问题|系统分类:移动开发| 互联网, commit, status ...
- wpa破解学习
TENDA 159031106A iPhone 192.168.0.11 90:27:E4:53:49:D6 18:58:52 PC-201211262044 192.168.0.12 00:F1: ...
- 课程设计之"网络考试系统"(php、Extjs)
1.TestSystem大概结构框图 2.数据库设计(11张表) 数据库名称:db_testsystem 数据库表: tb_admin 记录题库管理员帐户信息 代码 tb_allcontent 记录随 ...
- linux代理设置
http_proxy:http协议使用代理服务器地址:https_proxy:https协议使用安全代理地址:ftp_proxy:ftp协议使用代理服务器地址:user:代理使用的用户名:passwo ...
- (转)python装饰器二
Python装饰器进阶之二 保存被装饰方法的元数据 什么是方法的元数据 举个栗子 def hello(): print('Hello, World.') print(dir(hello)) 结果如下: ...
- python+tesseract验证码识别的一点小心得
由于公司需要,最近开始学习验证码的识别 我选用的是tesseract-ocr进行识别,据说以前是惠普公司开发的排名前三的,现在开源了.到目前为止已经出到3.0.2了 当然了,前期我们还是需要对验证码进 ...
- An internal error occurred Exception caught during execution of commit command
在工程目录下找到 .git 文件夹 ,找到里面的 index.lock 文件,删掉再commit
- iWatch报错: Missing com.apple.developer.healthkit entitlement
今天开发iWatch项目,报错: Optional (Error "Missing come.apple.developer.healthkit entitlement.") Us ...
- 全国车辆违章查询API文档及demo
简介 聚合数据全国车辆违章API,目前已经支持300个左右的城市违章查询,已连接上万个APP.方便有车一族随时了解自己是否有过交通违章,避免因遗忘或逾期处理违章罚单而造成的不必要损失. API参考文档 ...