政策资讯

Policy Information


ML之RS:基于用户的CF+LFM实现的推荐系统(基于相关度较高的用户实现电影推荐)

来源: 重庆市软件正版化服务中心    |    时间: 2022-09-19    |    浏览量: 64837    |   

ML之RS:基于用户的CF+LFM实现的推荐系统(基于相关度较高的用户实现电影推荐)

目录

输出结果

实现代码


输出结果

 

实现代码

  1. ML之RS:基于CF和LFM实现的推荐系统
  2. import numpy as np
  3. import pandas as pd
  4. import matplotlib.pyplot as plt
  5. import time
  6. import warnings
  7. warnings.filterwarnings('ignore')
  8. np.random.seed(1)
  9. plt.style.use('ggplot')
  10. data = pd.read_csv('ml-20m/ratings_smaller.csv', index_col=0)
  11. movies = pd.read_csv('ml-20m/movies_smaller.csv')
  12. 1、导入数据集
  13. data = pd.read_csv('ml-latest-small/ratings.csv')
  14. movies = pd.read_csv('ml-latest-small/movies.csv')
  15. movies = movies.set_index('movieId')[['title', 'genres']]
  16. 2、观察数据集
  17. How many users?
  18. print (data.userId.nunique(), 'users')
  19. How many movies?
  20. print (data.movieId.nunique(), 'movies')
  21. How possible ratings?
  22. print (data.userId.nunique() * data.movieId.nunique(), 'possible ratings')
  23. How many do we have?
  24. print (len(data), 'ratings')
  25. print (100 * (float(len(data)) / (data.userId.nunique() * data.movieId.nunique())), '% of possible ratings')
  26. Number of ratings per users
  27. fig = plt.figure(figsize=(10, 10))
  28. ax = plt.hist(data.groupby('userId').apply(lambda x: len(x)).values, bins=50)
  29. plt.xlabel("ratings")
  30. plt.ylabel("users")
  31. plt.title("Number of ratings per user")
  32. plt.show()
  33. Number of ratings per movie
  34. fig = plt.figure(figsize=(10, 10))
  35. ax = plt.hist(data.groupby('movieId').apply(lambda x: len(x)).values, bins=50)
  36. plt.xlabel("ratings")
  37. plt.ylabel("movies")
  38. plt.title('Number of ratings per movie')
  39. plt.show()
  40. Ratings distribution评分分布
  41. fig = plt.figure(figsize=(10, 10))
  42. ax = plt.hist(data.rating.values, bins=5)
  43. plt.xlabel("ratings")
  44. plt.ylabel("numbers")
  45. plt.title("Distribution of ratings")
  46. plt.show()
  47. Average rating per user
  48. fig = plt.figure(figsize=(10, 10))
  49. ax = plt.hist(data.groupby('userId').rating.mean().values, bins=10)
  50. plt.xlabel("Average rating")
  51. plt.ylabel("numbers")
  52. plt.title("Average rating per user")
  53. plt.show()
  54. Average rating per movie
  55. fig = plt.figure(figsize=(10, 10))
  56. ax = plt.hist(data.groupby('movieId').rating.mean().values, bins=10)
  57. plt.title('Average rating per movie')
  58. plt.show()
  59. Top Movies,genres电影类型
  60. average_movie_rating = data.groupby('movieId').mean()
  61. top_movies = average_movie_rating.sort_values('rating', ascending=False).head(10)
  62. pd.concat([movies.loc[top_movies.index.values],
  63. average_movie_rating.loc[top_movies.index.values].rating], axis=1)
  64. Robust Top Movies - Lets weight the average rating by the square root of number of ratings让平均评分进行加权数的平方根
  65. top_movies = data.groupby('movieId').apply(lambda x:len(x)**0.5 * x.mean()).sort_values('rating', ascending=False).head(10)
  66. pd.concat([movies.loc[top_movies.index.values],
  67. average_movie_rating.loc[top_movies.index.values].rating], axis=1)
  68. controversial_movies = data.groupby('movieId').apply(lambda x:len(x)**0.25 * x.std()).sort_values('rating', ascending=False).head(10)
  69. pd.concat([movies.loc[controversial_movies.index.values],
  70. average_movie_rating.loc[controversial_movies.index.values].rating], axis=1)

相关文章推荐

GitHub

评论

QQ咨询 扫一扫加入群聊,了解更多平台咨询
微信咨询 扫一扫加入群聊,了解更多平台咨询
意见反馈
立即提交
QQ咨询
微信咨询
意见反馈