You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.6 KiB

6 years ago
# 10.5 实战案例
6 years ago
## 电影评分数据
本次使用电影评分数据为`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
6 years ago
import pandas as pd
# 读取电影数据
ratings_df = pd.read_csv('data.csv')
# 获取用户数
6 years ago
userNo = max(ratings_df['userId'])+1
6 years ago
# 获取电影数
6 years ago
movieNo = max(ratings_df['movieRow'])+1
6 years ago
# 创建电影评分表
6 years ago
rating = np.zeros((userNo,movieNo))
for index,row in ratings_df.iterrows():
rating[int(row['userId']),int(row['movieRow'])]=row['rating']
```
构造出表格后,我们就能使用上一关实现的方法来对用户进行电影推荐了:
```python
6 years ago
# 使用上一节中实现的推荐算法进行推荐
6 years ago
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)。
6 years ago
# 使用上一节中实现的推荐算法进行推荐
6 years ago
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)。
6 years ago
# 使用上一节中实现的推荐算法进行推荐
6 years ago
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)。
6 years ago
# 使用上一节中实现的推荐算法进行推荐
6 years ago
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)
```