Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

59 righe
1.8 KiB

3 anni fa
  1. #import warnings
  2. #warnings.filterwarnings("ignore")
  3. import pandas as pd
  4. import numpy as np
  5. #获取数据
  6. ratings_df = pd.read_csv('real_ratings.csv')
  7. movies_df = pd.read_csv('movies.csv')
  8. userNo = max(ratings_df['userId'])+1
  9. movieNo = max(ratings_df['movieRow'])+1
  10. print(userNo,movieNo)
  11. #创建电影评分表
  12. rating = np.zeros((userNo,movieNo))
  13. for index,row in ratings_df.iterrows():
  14. rating[int(row['userId']),int(row['movieRow'])]=row['rating']
  15. def recommend(userID,lr,alpha,d,n_iter,data):
  16. '''
  17. userID(int):ID
  18. lr(float):
  19. alpha(float):
  20. d(int):()
  21. n_iter(int):
  22. data(ndarray):-
  23. '''
  24. #获取用户数与电影数
  25. m,n = data.shape
  26. #初始化参数
  27. x = np.random.uniform(0,1,(m,d))
  28. w = np.random.uniform(0,1,(d,n))
  29. #创建评分记录表,无评分记为0,有评分记为1
  30. record = np.array(data>0,dtype=int)
  31. #梯度下降,更新参数
  32. for i in range(n_iter):
  33. x_grads = np.dot(np.multiply(record,np.dot(x,w)-data),w.T)
  34. w_grads = np.dot(x.T,np.multiply(record,np.dot(x,w)-data))
  35. x = alpha*x - lr*x_grads
  36. w = alpha*w - lr*w_grads
  37. #预测
  38. predict = np.dot(x,w)
  39. #将用户未看过的电影分值从低到高进行排列
  40. for i in range(n):
  41. if record[userID-1][i] == 1 :
  42. predict[userID-1][i] = 0
  43. recommend = np.argsort(predict[userID-1])
  44. a = recommend[-1]
  45. b = recommend[-2]
  46. c = recommend[-3]
  47. d = recommend[-4]
  48. e = recommend[-5]
  49. print('为用户%d推荐的电影为:\n1:%s\n2:%s\n3:%s\n4:%s\n5:%s'\
  50. %(userID,movies_df['title'][a],movies_df['title'][b],movies_df['title'][c],movies_df['title'][d],movies_df['title'][e]))
  51. recommend(123,1e-4,0.999,20,100,rating)