【投稿】Machine Learning With Spark Note 2:构建简单的推荐系统

本文为数盟特约作者投稿,欢迎转载,请注明出处“数盟社区”和作者

博主简介:段石石,1号店精准化推荐算法工程师,主要负责1号店用户画像构建,喜欢钻研点Machine Learning的黑科技,对Deep Learning感兴趣,喜欢玩kaggle、看9神,对数据和Machine Learning有兴趣咱们可以一起聊聊,个人博客: hacker.duanshishi.com

推荐引擎应用场景:

  • 用户有海量选择:随着场景内item越来越多,用户越来越难以选择到合适的产品
  • 个性化场景:在选择产品时,会借鉴那些与推荐用户相似地群体,利用群体智慧对用户进行推荐”千人千面”

在本篇博客中,会涉及到以下几个部分:

  • 介绍不同类型的推荐引擎
  • 使用用户偏好模型来构造推荐模型
  • 使用训练好的模型来为指定user计算给定item的相似度大的items
  • 使用标准的评测函数来构造推荐模型的好坏

 

推荐模型类别:

  • 基于item的过滤:使用item的内容或者属性,选择给定item的相似的item列表,这些属性一般为文本内容,包括题目、名、标签以及一些产品的元信息,通常也包括一些media信息,比如图像、音频等等
  • 协同过滤:协同过滤是一种集体智慧的推荐模型,在基于用户的协同过滤方法中,如果两个用户有相似的偏好(通过用户对物品的评分、用户查看物品的记录、用户对物品的评论),当为给定用户来推荐相关产品时,会使用其他相似偏好的用户的产品列表来对该用户进行推荐。基于item的协同过滤,一般数据组成为用户和用户对某些items的rating,产品被相似偏好的用户rating相同的趋势比较大,因而我们可以用所有用户对物品的偏好,来发现物品与物品之间的相似度,根据用户的历史偏好物品,根据相似信息来推荐给该用户
  • Matrix Factorization

因为在Spark的MLlib模块中只有MF算法,文章之后会讲述如何使用Matrix Factorization来做相关的推荐。

Matrix Factorization

MF在Netflix Prize中得到最好的名词,关于MF的一片overview:http://techblog.netflix.com/2012/04/netflix-recommendations-beyond-5-stars.html

Explicit matrix factorization

user ratings 数据:






以user为行,movie为列构造对应rating matrix:

NewImage

MF就是一种直接建模user-item矩阵的方法,利用两个低维度的小矩阵的乘积来表示,属于一种降维的技术。

如果我们有U个用户,I个items,若不经过MF处理,它看来会使这样的:

NewImage

是一个极其稀疏的矩阵,经过MF处理后,表示为两个维度较小的矩阵相乘:

NewImage

这类模型被称为latent feature models,旨在寻找那些潜在的特征,来间接表示user-item rating的矩阵。这类潜在的features并不直接建模user对item的rating关系,而是通过latent features更趋近于建模用户对某类items的偏好,例如某类影片、风格等等,而这些事通过MF寻找其内在的信息,无需items的详细描述(和基于content的方法不同)。

MF模型如何计算一个user对某个item的偏好,对应向量相乘即可:

NewImage

如何计算两个item的相似度:

NewImage

MF模型的好处是一旦模型创建好后,predict变得十分容易,并且性能也很好,但是在海量的用户和itemset时,存储和生产MF中的如上图的这两个矩阵会变得具有挑战性。

Implicit matrix factorization

前面我们都在讨论显式的一些偏好信息,比如rating,但是在大部分应用中,拿不到这类信息,我们更多滴搜集的是一些隐性的反馈信息,这类反馈信息没有明确地告诉某个用户对某个item的偏好信息,但是却可以从用户对某个item的交互信息中建模出来,例如一些二值特征,包括是否浏览过、是否购买过产品、以及多少次看过某部电影等等。

MLlib中提供了一种处理这类隐性特征的方法,将前面的输入ratings矩阵其实可以看做是两个矩阵:二值偏好矩阵P和信心权重矩阵C;

举个例子:假定我们的网站上面没有设计对movie的rating部分,只能通过log查看到用户是否观看过影片,然后通过后期处理,可以看出他观看到过多少次某部影片,这里P来表示影片是否被某用户看过,C来描述这里的confidence weighting也就是观看的次数:

NewImage

这里我们把P和C的dot product来替代前面的rating矩阵,那么我们最终建模来预估某用户对item的偏好

Alternating least squares

ALS是解决MF问题的一个优化技术,被证明高效、高性能并且能有效地并行化,目前为止,是MLlib中推荐模块的唯一一个算法。Spark官网上有专门地描述

特征提取

特征提取是从已有数据中找到有用的数据来对算法进行建模,本文中使用显式数据也就是用户对movie的rating信息,这个数据来源于网络上的MovieLens标准数据集,以下代码为《Machine Learning with Spark》这本书里面的python的重写版本,会有专门的ipython notebook放到github上。

NewImage

数据分别是userId,itemId,rating和timestamp。

格式化数据,用于后面建模数据,导入Rating,ALS模块,下面是ALS类的使用说明:

NewImage

其中rank就是上面latent feature model中矩阵的k,在下面的实验中,我们设为50:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Big data – that was our motivation to explore the world of machine learning with Spark a couple of years ago. We wanted to build machine learning applications that would leverag models trained on large amounts of data, but the beginning was not easy. Spark was still evolving, it did not contain a powerful machine learning library, and we were still trying to figure out what it means to build a machine learning application. But, step by step, we started to explore different corners of the Spark ecosystem and followed Spark’s evolution. For us, the crucial part was a powerful machine learning library, which would provide features such as R or Python libraries did. This was an easy task for us, since we are actively involved in the development of H2O’s machine learning library and its branch called Sparkling Water, which enables the use of the H2O library from Spark applications. However, model training is just the tip of the machine learning iceberg. We still had to explore how to connect Sparkling Water to Spark RDDs, DataFrames, and DataSets, how to connect Spark to different data sources and read data, or how to export models and reuse them in different applications. During our journey, Spark evolved as well. Originally, being a pure Scala project, it started to expose Python and, later, R interfaces. It also took its Spark API on a long journey from low-level RDDs to a high-level DataSet, exposing a SQL-like interface. Furthermore, Spark also introduced the concept of machine learning pipelines, adopted from the scikit-learn library known from Python. All these improvements made Spark a great tool for data transformation and data processing. Based on this experience, we decided to share our knowledge with the rest of the world via this book. Its intention is simple: to demonstrate different aspects of building Spark machine learning applications on examples, and show how to use not only the latest Spark features, but also low-level Spark interfaces. On our journey

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值