[读书笔记]A Programmer's Guide to Data Mining - chapter 2 Get Started with Recommendation Systems

本文介绍了协同过滤推荐系统的原理与实现,包括如何寻找相似用户、多种距离度量方法及其适用场景,以及皮尔逊相关系数和余弦相似度的应用。

这一章的主要内容是讲解 Collaborative filtering.

Contents

  • How a recommendation system works.
  • How social filtering works
  • How to find similar items
  • Manhattan distance
  • Euclidean distance
  • Minkowski distance
  • Pearson Correlation Coefficient
  • Cosine similarity
  • implementing k-nearest neighbors in Python
  • the Book Crossing dataset
首先我们先看看我们手上的数据:


现在,有一位神秘的MR. X, 他给 Snow Crash 4 星,并且给了The Gril with the Dragon Tattoo 2星的评价,我们需要给MR.X 推荐一本书。那么首先我们需要找到和MR.X最为相似的人。


Manhattan Distance

这是最简单的测量距离的方式

具体公式如下:

那么得到如下的结果:


到这里,我们可以确定Amy和MR.X最为相似,那么我们查看Amy的历史数据,将她最喜欢的Paolo Bacigalupi's The Windup Girl,推荐给MR.X


Euclidean Distance

欧几里得距离实际上就是计算两点之间的直线距离,使用勾股定理即可


然后我们得到对应的结果:



N-Dimensional Scenario

现在看一下N-D空间下的情况


我们计算Angelica和Bill的相似度,这里只需要计算两个人共同评分的项目即可。

manhattan distance


Euclidean Distance


A Flaw

我们发现这种方法计算Hailey和Veronica,Hailey和Jordyn,的距离会出现一些问题,前两个人只有2项共同评分的项目,而后者两人有5个共同评分的项目,这样就出现一个问题,两个距离分别是2维空间和5维空间的距离.
Manhattan Distance 和 Euclidean Distance在没有缺失项目评分的时候工作的很好,但是如何处理这种有空缺项目的情况呢,我们后面会提到。


Minkowski Distance

下面的极限情况被称为Chebyshev distance,我们可以发现这个距离的结果是r的单调函数,也就是r越大,那么距离差越大的那个dimension的距离就会越起主导作用。



一个问题

根据上图计算距离的结果中Angelica和Veronica是最为接近的邻居,我们要向Angelica推荐一个电影的话,遍历Veronica的所有项发现,Angelica都已经看过了,怎么办,我们后面会提到。


诡异的用户评分模式


不同的用户有着自己不同的评分标准,很难说两个用户相同的分值表示相同的意愿。那么如何解决这个问题呢?


Pearson Correlation Coefficient

相关系数:考察两个事物(在数据里我们称之为变量)之间的相关程度。

 如果有两个变量:X、Y,最终计算出的相关系数的含义可以有如下理解:

(1)、当相关系数为0时,X和Y两变量无关系。

(2)、当X的值增大(减小),Y值增大(减小),两个变量为正相关,相关系数在0.00与1.00之间。

(3)、当X的值增大(减小),Y值减小(增大),两个变量为负相关,相关系数在-1.00与0.00之间。

 

相关系数的绝对值越大,相关性越强,相关系数越接近于1或-1,相关度越强,相关系数越接近于0,相关度越弱。

通常情况下通过以下取值范围判断变量的相关强度:

相关系数     0.8-1.0     极强相关
                 0.6-0.8     强相关
                 0.4-0.6     中等程度相关
                 0.2-0.4     弱相关
                 0.0-0.2     极弱相关或无相关

由于计算上式需要多次遍历全部数据,这里我们采用他的另外一种形式


下面我们直观的感受一下皮尔逊相关系数表达的含义

下面一张图片是皮尔逊相关系数为1的时候的情况,Clara和Robert的评分匹配成为一条直线


下面这张图片表示的是皮尔逊相关系数为0.91的情况:


很明显皮尔逊相关系数必须要计算两个人共同评分的项目才可以起作用,如果一个人有评分,另一个人没有评分,这种情况讨论他们的相关性没有意义。



Cosine Similarity

我们拿itunes里面的音乐为例子,itunes里面大概有150,000,000首歌曲,但是我们如果比较两个人在音乐上的相似度的话,必然会有很多的音乐是两个人都没有听过的,也就是说两个人会有很多共同播放数为0的曲目,而且会有很多。于是这里我们引入cosine相似度计算,它直接忽略那些0-0的曲目。


[-1,1],-1表示完全负相关,1表示完全正相关


Which similarity measure to use?


好,我们进一步解释为什么数据稀疏的时候,不适合使用以上提到的距离测量法。

Linda 和 Eric 两个人喜欢同种类型的音乐,他们又20首相同的音乐,而且他们对这些音乐的评分的平均差只是0.5.那么这种情况下manhattan 距离 20*0.5 = 10,而欧几里得距离 d = 5^0.5 - 2.236

Jake 和 Linda仅仅共同评价了同一首歌曲,一个人给了5分一个人给了3分,那么两个人之间的距离,manhattan距离是2,欧几里得距离是2.两个数据都表示Jake更加接近Linda。这很明显很不合理。

恩,那么,如果把他没有评分的项目默认设置为0的话呢???


来,我们试一下。这样的话,我们稍微调整一下,给出一个新的场景。


Cooper,Kelsey, and Jake

Jake 评价了25首歌曲,Cooper评价了26首,其中25首和Jake相同,而且平均差值为0.25.

Kelsey评价了150首歌曲,25首和Jake相同,而且平均差值为0.25.

我们的只管感受是COOPER,KELSEY和JAKE的相似度应该是一样的。

但是如果把未评分项默认取值0的话,COOPER会远远的偏离jake。

这种数据稀疏的情况,更适合使用Cosine Similarity。


K-nearest neighbor

以上都是基于单个最相似的人来进行推荐的,但是可能有这样的情况:

Billy和我最相似,但是我可能由于朋友的道义,必须给我的好朋友Bob的歌曲打最高的分数。但这显然不适合推荐给Billy.

那么,我们可以计算出多个相似的人,然后共同推荐。每个人的相似度可以作为他打的分数的权重。

Learn the art of building robust and powerful recommendation engines using R About This Book Learn to exploit various data mining techniques Understand some of the most popular recommendation techniques This is a step-by-step guide full of real-world examples to help you build and optimize recommendation engines Who This Book Is For If you are a competent developer with some knowledge of machine learning and R, and want to further enhance your skills to build recommendation systems, then this book is for you. What You Will Learn Get to grips with the most important branches of recommendation Understand various data processing and data mining techniques Evaluate and optimize the recommendation algorithms Prepare and structure the data before building models Discover different recommender systems along with their implementation in R Explore various evaluation techniques used in recommender systems Get to know about recommenderlab, an R package, and understand how to optimize it to build efficient recommendation systems In Detail A recommendation system performs extensive data analysis in order to generate suggestions to its users about what might interest them. R has recently become one of the most popular programming languages for the data analysis. Its structure allows you to interactively explore the data and its modules contain the most cutting-edge techniques thanks to its wide international community. This distinctive feature of the R language makes it a preferred choice for developers who are looking to build recommendation systems. The book will help you understand how to build recommender systems using R. It starts off by explaining the basics of data mining and machine learning. Next, you will be familiarized with how to build and optimize recommender models using R. Following that, you will be given an overview of the most popular recommendation techniques. Finally, you will learn to implement all the concepts you have learned throughout the book to build a recommender system. Style and approach This is a step-by-step guide that will take you through a series of core tasks. Every task is explained in detail with the help of practical examples. Table of Contents Chapter 1: Getting Started with Recommender Systems Chapter 2: Data Mining Techniques Used in Recommender Systems Chapter 3: Recommender Systems Chapter 4: Evaluating the Recommender Systems Chapter 5: Case Study – Building Your Own Recommendation Engine
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值