|
|
|
|
# 10.5 实战案例
|
|
|
|
|
|
|
|
|
|
## 电影评分数据
|
|
|
|
|
|
|
|
|
|
本次使用电影评分数据为`672`个用户对`9123`部电影的评分记录,部分数据如下:
|
|
|
|
|
|
|
|
|
|
|userId| movieRow |rating|
|
|
|
|
|
|--|--|--|
|
|
|
|
|
| 1 | 30 |2.5|
|
|
|
|
|
|7| 30 |3|
|
|
|
|
|
| 31 | 30 |4|
|
|
|
|
|
| 32 |30| 4|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
其中:
|
|
|
|
|
|
|
|
|
|
userId:用户编号
|
|
|
|
|
movieRow:电影编号
|
|
|
|
|
rating:评分值
|
|
|
|
|
|
|
|
|
|
如:
|
|
|
|
|
|
|
|
|
|
- 第一行数据表示用户`1`对电影`30`评分为`2.5`分。
|
|
|
|
|
- 第二行数据表示用户`7`对电影`30`评分为`3`分。
|
|
|
|
|
|
|
|
|
|
然后,我们还有电影编号与电影名字对应的数据如下:
|
|
|
|
|
|
|
|
|
|
|movieRow|title|
|
|
|
|
|
|--|--|
|
|
|
|
|
| 0 | Toy Story (1995) |
|
|
|
|
|
|1| Jumanji (1995) |
|
|
|
|
|
| 2| Grumpier Old Men (1995) |
|
|
|
|
|
| 3|Waiting to Exhale (1995)|
|
|
|
|
|
|
|
|
|
|
其中:
|
|
|
|
|
|
|
|
|
|
movieRow:电影编号
|
|
|
|
|
title:电影名称
|
|
|
|
|
|
|
|
|
|
[数据下载连接 提取码:ve3v](https://pan.baidu.com/s/1kPLXbkGxMXllXBiNg1yCMQ)
|
|
|
|
|
|
|
|
|
|
## 构造用户-电影评分矩阵
|
|
|
|
|
|
|
|
|
|
大家已经知道,要使用基于矩阵分解的协同过滤算法,首先得有用户与电影评分的矩阵,而我们实际中的数据并不是以这样的形式保存,所以在使用算法前要先构造出用户-电影评分矩阵,`python`实现代码如下:
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
import numpy as np
|
|
|
|
|
import pandas as pd
|
|
|
|
|
|
|
|
|
|
# 读取电影数据
|
|
|
|
|
ratings_df = pd.read_csv('data.csv')
|
|
|
|
|
|
|
|
|
|
# 获取用户数
|
|
|
|
|
userNo = max(ratings_df['userId'])+1
|
|
|
|
|
# 获取电影数
|
|
|
|
|
movieNo = max(ratings_df['movieRow'])+1
|
|
|
|
|
|
|
|
|
|
# 创建电影评分表
|
|
|
|
|
rating = np.zeros((userNo,movieNo))
|
|
|
|
|
for index,row in ratings_df.iterrows():
|
|
|
|
|
rating[int(row['userId']),int(row['movieRow'])]=row['rating']
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
构造出表格后,我们就能使用上一关实现的方法来对用户进行电影推荐了:
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
# 使用上一节中实现的推荐算法进行推荐
|
|
|
|
|
recommend(1,1e-4,0.999,20,100,rating)
|
|
|
|
|
>>>
|
|
|
|
|
为用户1推荐的电影为:
|
|
|
|
|
1:Rumble Fish (1983)
|
|
|
|
|
2:Aquamarine (2006)
|
|
|
|
|
3:Stay Alive (2006)
|
|
|
|
|
4:Betrayal, The (Nerakhoon) (2008)
|
|
|
|
|
5:Midnight Express (1978)。
|
|
|
|
|
|
|
|
|
|
# 使用上一节中实现的推荐算法进行推荐
|
|
|
|
|
recommend(666,1e-4,0.999,20,100,rating)
|
|
|
|
|
>>>
|
|
|
|
|
为用户666推荐的电影为:
|
|
|
|
|
1:Aquamarine (2006)
|
|
|
|
|
2:It's a Boy Girl Thing (2006)
|
|
|
|
|
3:Kill the Messenger (2014)
|
|
|
|
|
4:Onion Field, The (1979)
|
|
|
|
|
5:Wind Rises, The (Kaze tachinu) (2013)。
|
|
|
|
|
|
|
|
|
|
# 使用上一节中实现的推荐算法进行推荐
|
|
|
|
|
recommend(555,1e-4,0.999,20,100,rating)
|
|
|
|
|
>>>
|
|
|
|
|
为用户555推荐的电影为:
|
|
|
|
|
1:Return from Witch Mountain (1978)
|
|
|
|
|
2:Hitcher, The (2007)
|
|
|
|
|
3:Betrayal, The (Nerakhoon) (2008)
|
|
|
|
|
4:Listen to Me Marlon (2015)
|
|
|
|
|
5:World of Tomorrow (2015)。
|
|
|
|
|
|
|
|
|
|
# 使用上一节中实现的推荐算法进行推荐
|
|
|
|
|
recommend(88,1e-4,0.999,20,100,rating)
|
|
|
|
|
>>>
|
|
|
|
|
为用户88推荐的电影为:
|
|
|
|
|
1:Now, Voyager (1942)
|
|
|
|
|
2:Betrayal, The (Nerakhoon) (2008)
|
|
|
|
|
3:Aquamarine (2006)
|
|
|
|
|
4:Post Grad (2009)
|
|
|
|
|
5:Hitcher, The (2007)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|