经典算法题每日演练——第六题 协同推荐SlopeOne 算法
原文:经典算法题每日演练——第六题 协同推荐SlopeOne 算法
相信大家对如下的Category都很熟悉,很多网站都有类似如下的功能,“商品推荐”,"猜你喜欢“,在实体店中我们有导购来为我们服务,在网络上
我们需要同样的一种替代物,如果简简单单的在数据库里面去捞,去比较,几乎是完成不了的,这时我们就需要一种协同推荐算法,来高效的推荐浏览者喜
欢的商品。
一:概念
SlopeOne的思想很简单,就是用均值化的思想来掩盖个体的打分差异,举个例子说明一下:
在这个图中,系统该如何计算“王五“对”电冰箱“的打分值呢?刚才我们也说了,slopeone是采用均值化的思想,也就是:R王五 =4-{[(5-10)+(4-5)]/2}=7 。
下面我们看看多于两项的商品,如何计算打分值。
rb = (n * (ra - R(A->B)) + m * (rc - R(C->B)))/(m+n)
注意: a,b,c 代表“商品”。
ra 代表“商品的打分值”。
ra->b 代表“A组到B组的平均差(均值化)”。
m,n 代表人数。
根据公式,我们来算一下。
r王五 = (2 * (4 - R(洗衣机->彩电)) + 2 * (10 - R(电冰箱->彩电))+ 2 * (5 - R(空调->彩电)))/(2+2+2)=6.8
是的,slopeOne就是这么简单,实战效果非常不错。
二:实现
1:定义一个评分类Rating。
/// <summary>
/// 评分实体类
/// </summary>
public class Rating
{
/// <summary>
/// 记录差值
/// </summary>
public float Value { get; set; } /// <summary>
/// 记录评分人数,方便公式中的 m 和 n 的值
/// </summary>
public int Freq { get; set; } /// <summary>
/// 记录打分用户的ID
/// </summary>
public HashSet<int> hash_user = new HashSet<int>(); /// <summary>
/// 平均值
/// </summary>
public float AverageValue
{
get { return Value / Freq; }
}
}
2: 定义一个产品类
/// <summary>
/// 产品类
/// </summary>
public class Product
{
public int ProductID { get; set; } public string ProductName { get; set; } /// <summary>
/// 对产品的打分
/// </summary>
public float Score { get; set; }
}
3:SlopeOne类
参考了网络上的例子,将二维矩阵做成线性表,有效的降低了空间复杂度。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace SupportCenter.Test
{
#region Slope One 算法
/// <summary>
/// Slope One 算法
/// </summary>
public class SlopeOne
{
/// <summary>
/// 评分系统
/// </summary>
public static Dictionary<int, Product> dicRatingSystem = new Dictionary<int, Product>(); public Dictionary<string, Rating> dic_Martix = new Dictionary<string, Rating>(); public HashSet<int> hash_items = new HashSet<int>(); #region 接收一个用户的打分记录
/// <summary>
/// 接收一个用户的打分记录
/// </summary>
/// <param name="userRatings"></param>
public void AddUserRatings(IDictionary<int, List<Product>> userRatings)
{
foreach (var user1 in userRatings)
{
//遍历所有的Item
foreach (var item1 in user1.Value)
{
//该产品的编号(具有唯一性)
int item1Id = item1.ProductID; //该项目的评分
float item1Rating = item1.Score; //将产品编号字存放在hash表中
hash_items.Add(item1.ProductID); foreach (var user2 in userRatings)
{
//再次遍历item,用于计算俩俩 Item 之间的差值
foreach (var item2 in user2.Value)
{
//过滤掉同名的项目
if (item2.ProductID <= item1Id)
continue; //该产品的名字
int item2Id = item2.ProductID; //该项目的评分
float item2Rating = item2.Score; Rating ratingDiff; //用表的形式构建矩阵
var key = Tools.GetKey(item1Id, item2Id); //将俩俩 Item 的差值 存放到 Rating 中
if (dic_Martix.Keys.Contains(key))
ratingDiff = dic_Martix[key];
else
{
ratingDiff = new Rating();
dic_Martix[key] = ratingDiff;
} //方便以后以后userrating的编辑操作,(add)
if (!ratingDiff.hash_user.Contains(user1.Key))
{
//value保存差值
ratingDiff.Value += item1Rating - item2Rating; //说明计算过一次
ratingDiff.Freq += ;
} //记录操作人的ID,方便以后再次添加评分
ratingDiff.hash_user.Add(user1.Key);
}
}
}
}
}
#endregion #region 根据矩阵的值,预测出该Rating中的值
/// <summary>
/// 根据矩阵的值,预测出该Rating中的值
/// </summary>
/// <param name="userRatings"></param>
/// <returns></returns>
public IDictionary<int, float> Predict(List<Product> userRatings)
{
Dictionary<int, float> predictions = new Dictionary<int, float>(); var productIDs = userRatings.Select(i => i.ProductID).ToList(); //循环遍历_Items中所有的Items
foreach (var itemId in this.hash_items)
{
//过滤掉不需要计算的产品编号
if (productIDs.Contains(itemId))
continue; Rating itemRating = new Rating(); // 内层遍历userRatings
foreach (var userRating in userRatings)
{
if (userRating.ProductID == itemId)
continue; int inputItemId = userRating.ProductID; //获取该key对应项目的两组AVG的值
var key = Tools.GetKey(itemId, inputItemId); if (dic_Martix.Keys.Contains(key))
{
Rating diff = dic_Martix[key]; //关键点:运用公式求解(这边为了节省空间,对角线两侧的值呈现奇函数的特性)
itemRating.Value += diff.Freq * (userRating.Score + diff.AverageValue * ((itemId < inputItemId) ? : -)); //关键点:运用公式求解 累计每两组的人数
itemRating.Freq += diff.Freq;
}
} predictions.Add(itemId, itemRating.AverageValue);
} return predictions;
}
#endregion
}
#endregion #region 工具类
/// <summary>
/// 工具类
/// </summary>
public class Tools
{
public static string GetKey(int Item1Id, int Item2Id)
{
return (Item1Id < Item2Id) ? Item1Id + "->" + Item2Id : Item2Id + "->" + Item1Id;
}
}
#endregion
}
4: 测试类Program
这里我们灌入了userid=1000,2000,3000的这三个人,然后我们预测userID=3000这个人对 “彩电” 的打分会是多少?
public class Program
{
static void Main(string[] args)
{
SlopeOne test = new SlopeOne(); Dictionary<int, List<Product>> userRating = new Dictionary<int, List<Product>>(); //第一位用户
List<Product> list = new List<Product>()
{
new Product(){ ProductID=, ProductName="洗衣机",Score=},
new Product(){ ProductID=, ProductName="电冰箱", Score=},
new Product(){ ProductID=, ProductName="彩电", Score=},
new Product(){ ProductID=, ProductName="空调", Score=},
}; userRating.Add(, list); test.AddUserRatings(userRating); userRating.Clear();
userRating.Add(, list); test.AddUserRatings(userRating); //第二位用户
list = new List<Product>()
{
new Product(){ ProductID=, ProductName="洗衣机",Score=},
new Product(){ ProductID=, ProductName="电冰箱", Score=},
new Product(){ ProductID=, ProductName="彩电", Score=},
new Product(){ ProductID=, ProductName="空调", Score=},
}; userRating.Clear();
userRating.Add(, list); test.AddUserRatings(userRating); //第三位用户
list = new List<Product>()
{
new Product(){ ProductID=, ProductName="洗衣机", Score=},
new Product(){ ProductID=, ProductName="电冰箱", Score=},
new Product(){ ProductID=, ProductName="空调", Score=},
}; userRating.Clear();
userRating.Add(, list); test.AddUserRatings(userRating); //那么我们预测userID=3000这个人对 “彩电” 的打分会是多少?
var userID = userRating.Keys.FirstOrDefault();
var result = userRating[userID]; var predictions = test.Predict(result); foreach (var rating in predictions)
Console.WriteLine("ProductID= " + rating.Key + " Rating: " + rating.Value);
}
}
经典算法题每日演练——第六题 协同推荐SlopeOne 算法的更多相关文章
- 经典算法题每日演练——第十七题 Dijkstra算法
原文:经典算法题每日演练--第十七题 Dijkstra算法 或许在生活中,经常会碰到针对某一个问题,在众多的限制条件下,如何去寻找一个最优解?可能大家想到了很多诸如“线性规划”,“动态规划” 这些经典 ...
- 经典算法题每日演练——第十一题 Bitmap算法
原文:经典算法题每日演练--第十一题 Bitmap算法 在所有具有性能优化的数据结构中,我想大家使用最多的就是hash表,是的,在具有定位查找上具有O(1)的常量时间,多么的简洁优美, 但是在特定的场 ...
- 经典算法题每日演练——第八题 AC自动机
原文:经典算法题每日演练--第八题 AC自动机 上一篇我们说了单模式匹配算法KMP,现在我们有需求了,我要检查一篇文章中是否有某些敏感词,这其实就是多模式匹配的问题. 当然你也可以用KMP算法求出,那 ...
- 经典算法题每日演练——第七题 KMP算法
原文:经典算法题每日演练--第七题 KMP算法 在大学的时候,应该在数据结构里面都看过kmp算法吧,不知道有多少老师对该算法是一笔带过的,至少我们以前是的, 确实kmp算法还是有点饶人的,如果说红黑树 ...
- 经典算法题每日演练——第十一题 Bitmap算法 (转)
http://www.cnblogs.com/huangxincheng/archive/2012/12/06/2804756.html 在所有具有性能优化的数据结构中,我想大家使用最多的就是hash ...
- 经典算法题每日演练——第十六题 Kruskal算法
原文:经典算法题每日演练--第十六题 Kruskal算法 这篇我们看看第二种生成树的Kruskal算法,这个算法的魅力在于我们可以打一下算法和数据结构的组合拳,很有意思的. 一:思想 若存在M={0, ...
- 经典算法题每日演练——第十四题 Prim算法
原文:经典算法题每日演练--第十四题 Prim算法 图论在数据结构中是非常有趣而复杂的,作为web码农的我,在实际开发中一直没有找到它的使用场景,不像树那样的频繁使用,不过还是准备 仔细的把图论全部过 ...
- java算法题每日一练01,java入门简单算法题小练
1.给数组做反序 public class Ak01 { public static void main(String[] args) { int[] a = new int[]{22,48,41,2 ...
- codeforces水题100道 第六题 Yandex.Algorithm 2011 Qualification 2 A. Double Cola (math)
题目链接:www.codeforces.com/problemset/problem/82/A题意:五个人排队喝可乐,一个人喝完一杯,就在可乐的最后面放两杯自己喝的可乐,问第n个喝的人是谁.C++代码 ...
随机推荐
- 使用WPF创建无边框窗体
一.无边框窗口添加窗口阴影 实际上在WPF中添加无边框窗口的窗口阴影十分简单. 首先,设置WindowStyle="None"以及AllowsTransparency=" ...
- Web静态和动态项目委托代理基于面向方面编程AOP
本来每天更新,我一般喜欢晚上十二点的时候发文章,结果是不是愚人节?校内网也将是非常有趣,破,把我给打. ..好吧-从今天开始的话题AOP.AOP太重要了,所以把第二篇文章谈论这个话题,AOP它是Spr ...
- [渣译文] SignalR 2.0 系列: 支持的平台
原文:[渣译文] SignalR 2.0 系列: 支持的平台 英文渣水平,大伙凑合着看吧,并不是逐字翻译的…… 这是微软官方SignalR 2.0教程Getting Started with ASP. ...
- Java SE学习之数组——匿名数组和不规则数组
本文是学习网络上的文章时的总结以及自己的一点实践.感谢大家无私的分享. 近期偶然遇到了数组的问题,学习了匿名数组和不规则数组. 匿名数组适用于仅仅使用一次的情况:不规则数组适用是每行数据总数不确定的情 ...
- iphone内容开发技术学习
一.iOS基础 1 开发环境搭建以及IOS组件.框架的概要介绍. 2 mac操作系统与iOS操作系统 3 xcode IDE开发环境的初始 二.C语言基础 1数据类型.表达式与控制流程语句 2数组.函 ...
- Oracle 初始化参数文件pfile和spfile
pfile和spfile差额 pfile :Oracle 9i之前.ORACLE使用我们一直PFILE存储的初始化参数,,能够在操作系统级别改动. 当spfile文件改动出现错误导致oracle无法启 ...
- 玩转Web之JavaScript(三)-----javaScript语法总结(三) 窗口/滚动条/文本的相关语法
JS语法集锦(三) 窗口/滚动条/文本 alert("文本") 警告框:警告框经常用于确保用户可以得到某些信息,当警告框出现后,用户需要点击确定按钮才能继续进行操作. con ...
- Swing开发界面时的一个bug复盘
问题:QA突然发个截图说一个Dialog上展示的东西变形了 分析:不理解,什么也没做,怎么会变形,刚刚我用的时候还正常.看看代码,的确什么也没更改:在本地测一下,也没有问题:baidu,bing,st ...
- 举例说,Linux核心名单(两)
使用列表 我认为最好的方式,成为熟悉的核心列表功能是看一些简单的例子,素材去更好的理解链表. 以下是一个样例.包括创建.加入.删除和遍历链表. <span style="font-si ...
- 使用AppCompat_v7 21.0.0d的几个兼容问题
1.实现新的ActionBarDrawerToggle动画 ActionBarDrawerToggle使用最新的AppCompat_v7 21会出现一个非常帅的动画.使用方式在Androidstudi ...