推荐系统的类型有很多, 其中基于流行度的推荐最为简单,在日常生活中应用也比较多,比如某一地方餐馆的菜品等推荐。
下面我们应用UCI机器学习数据集,建立基于流行度的餐馆推荐模型。
数据集来源于UCI Restaurant consumer data
首先,我们需要导入我们需要的数据分析包
导入数据分析包
1 | import pandas as pd |
探索数据
将数据读取到pandas
的DataFrame
中,
1 | frame = pd.read_csv("ratings_final.csv") #数据集放在脚本的同目录 |
查看两个数据的基本情况:
1 | frame.head() |
userID | placeID | rating | food_rating | service_rating | |
---|---|---|---|---|---|
0 | U1077 | 135085 | 2 | 2 | 2 |
1 | U1077 | 135038 | 2 | 2 | 1 |
2 | U1077 | 132825 | 2 | 2 | 2 |
3 | U1077 | 135060 | 1 | 2 | 2 |
4 | U1068 | 135104 | 1 | 1 | 2 |
1 | cuisine.head() |
placeID | Rcuisine | |
---|---|---|
0 | 135110 | Spanish |
1 | 135109 | Italian |
2 | 135107 | Latin_American |
3 | 135106 | Mexican |
4 | 135105 | Fast_Food |
基于流行度的推荐模型
将餐馆按照placeID
分组,对评价数排序。
1 | rating_count = pd.DataFrame(frame.groupby('placeID')['rating'].count()) |
rating | |
---|---|
placeID | |
135085 | 36 |
132825 | 32 |
135032 | 28 |
135052 | 25 |
132834 | 25 |
找出评价数前五的餐馆,并且与菜品对应
1 | most_rated_places = pd.DataFrame([135085, 132825, 135032, 135052, 132834], index=np.arange(5), columns=['placeID']) |
placeID | Rcuisine | |
---|---|---|
0 | 135085 | Fast_Food |
1 | 132825 | Mexican |
2 | 135032 | Cafeteria |
3 | 135032 | Contemporary |
4 | 135052 | Bar |
5 | 135052 | Bar_Pub_Brewery |
6 | 132834 | Mexican |
对于包含Rcuisine
的统计
1 | cuisine['Rcuisine'].describe() |
unique 59
top Mexican
freq 239
Name: Rcuisine, dtype: object
最后,我们找到了比较受欢迎的相应参观的对应菜品。
这就是基于流行度的菜品推荐。
感谢您的时间,有问题请联系我。