当前位置:   article > 正文

基于django的协同过滤食谱推荐系统、python_基于协同过滤算法的食谱推荐

基于协同过滤算法的食谱推荐

1.内容简介

所用技术:scrapy、django、协同过滤

2.过程

2.1爬虫

使用scrapy爬取https://www.douguo.com网站的食谱数据

主要代码:

  1. # 在 parse 方法中获取下一页链接并发送请求
  2. def parse(self, response):
  3. # 获取当前页面的菜谱详情链接并发送请求
  4. for url in response.xpath('//ul[@class="cook-list"]//a[@class="cook-img"]/@href').getall():
  5. pipei_url = re.sub("/0.*", "", response.url)
  6. tag_id = self.url_tag[unquote(pipei_url)][0]
  7. tag_name = self.url_tag[unquote(pipei_url)][1]
  8. m = {"tag_id": tag_id, "tag_name": tag_name}
  9. yield scrapy.Request(url=self.url_root + url, callback=self.parse_detail, meta=m)
  10. # 解析出下一页链接并发送请求
  11. next_page = response.xpath('//a[@class="anext"]/@href')
  12. if next_page:
  13. yield scrapy.Request(url=next_page[0].get().replace("http", "https"), callback=self.parse)
  14. def md5_encrypt(self, s):
  15. md5 = hashlib.md5()
  16. md5.update(s.encode("utf-8"))
  17. return md5.hexdigest()
  18. def parse_detail(self, response, **kwargs):
  19. tag_id = response.meta["tag_id"]
  20. tag_name = response.meta["tag_name"]
  21. divs = response.xpath('//div[@class="step"]/div')
  22. title = response.xpath("//h1/text()").get()
  23. # 步骤
  24. step_list = []
  25. for div in divs:
  26. step_img = div.xpath("a/img//@src").get()
  27. step_index = div.xpath('div[@class="stepinfo"]/p/text()').get()
  28. step_text = "\n".join(div.xpath('div[@class="stepinfo"]/text()').getall()).strip()
  29. step_list.append([step_img, step_index, step_text])
  30. # 配料
  31. mix_list = []
  32. for td in response.xpath("//table/tr/td"):
  33. mix_name = td.xpath('span[@class="scname"]//text()').get()
  34. mix_cot = td.xpath('span[@class="right scnum"]//text()').get()
  35. mix_list.append([mix_name, mix_cot])
  36. info_item = FoodInfoItem(
  37. tag_id=tag_id,
  38. tag_name=tag_name,
  39. food_id=self.md5_encrypt(response.url),
  40. food_url=response.url,
  41. title=title,
  42. step_list=str(step_list),
  43. img=response.xpath('//*[@id="banner"]/a/img/@src').get(),
  44. desc1="\n".join(response.xpath('//p[@class="intro"]/text()').getall()).strip(),
  45. mix_list=str(mix_list),
  46. all_key=tag_name + title + str(mix_list),
  47. )
  48. yield info_item

2.2django展示

2.2.1主页展示(协同过滤推荐)

 2.2.2搜索功能

 2.2.3 饮食营养构成

 2.3协同过滤推荐

基于用户的协同过滤推荐算法

  1. def recommend_by_user_cf(user_id, similarity_matrix, N=10):
  2. """
  3. 基于用户的协同过滤推荐算法
  4. Args:
  5. user_id: 目标用户ID
  6. similarity_matrix: 用户之间的相似度矩阵
  7. records: 所有用户的点菜记录,格式为 [(user_id, food_id, eat_date), ...]
  8. N: 推荐菜品数量
  9. Returns:
  10. recommended_foods: 推荐的菜品列表,格式为 [(food_id_1, score_1), (food_id_2, score_2), ...]
  11. """
  12. # 找到和指定用户吃过相同菜品的其他用户
  13. user_eating_records = EatingRecord.objects.filter(user_id=user_id).values("food_id", "eat_date")
  14. user_food_ids = [record["food_id"] for record in user_eating_records]
  15. similar_users = []
  16. for i in range(similarity_matrix.shape[0]):
  17. if i != user_id - 1:
  18. similarity = similarity_matrix[user_id - 1, i]
  19. if similarity > 0:
  20. # 找到该相似用户在最近M天内吃过的所有菜品
  21. similar_user_eating_records = (
  22. EatingRecord.objects.filter(user_id=i + 1).exclude(food_id__in=user_food_ids).values("food_id", "eat_date").order_by("-eat_date")[:10]
  23. )
  24. similar_user_food_ids = [record["food_id"] for record in similar_user_eating_records]
  25. # 计算该相似用户与指定用户之间的相似度,并加入相似用户列表中
  26. similar_users.append((i + 1, similarity, similar_user_food_ids))
  27. # 统计所有相似用户对每个菜品的兴趣度得分
  28. scores = {}
  29. for similar_user_id, similarity, similar_user_food_ids in similar_users:
  30. for food_id in similar_user_food_ids:
  31. if food_id not in user_food_ids:
  32. scores[food_id] = scores.get(food_id, 0) + similarity
  33. # 按照得分从高到低排序,选取前N个菜品作为推荐结果
  34. sorted_scores = sorted(scores.items(), key=lambda x: x[1], reverse=True)
  35. recommended_foods = sorted_scores[:N]
  36. return recommended_foods

python 毕设帮助,指导,源码分享,调试部署:worthy_life_

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/251417
推荐阅读
相关标签
  

闽ICP备14008679号