zoukankan      html  css  js  c++  java
  • 【推荐算法】基于用户协同过滤算法

    """
        ORM外部调用的配置:
    """
    import numpy as np
    import math
    import os
    
    def orm_standby():
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "TravelRecommend.settings")  # manage.py文件中有同样的环境配置
        import django
        django.setup()
    
    def make(data,n):
        # 二值化处理 n为长度
        rea_data = np.zeros(n,dtype=np.int)
        # 将所选号码填充为1
        rea_data[data] =  1
        return rea_data
    
    # 这里传进去的已经是二值化数据
    def calculation(mine,other):
        # 分子
        fenzi = 0
        for x in range(len(mine)):
            fenzi = fenzi +  mine[x] * other[x]
    
        # 分母 第一部分
        fenmu1 = 0
        for x in range(len(mine)):
            fenmu1 = fenmu1 +  mine[x] * mine[x]
        # 给分母 第一部分 开平方
        fenmu1 = math.sqrt(fenmu1)
        # print("分母1开根号", fenmu1)
    
        # 分母 第二部分
        fenmu2 = 0
        for x in range(len(other)):
            fenmu2 = fenmu2 + other[x] * other[x]
        # 给分母 第二部分 开平方
        fenmu2 = math.sqrt(fenmu2)
        # print("分母2开根号", fenmu2)
    
    
        # 计算权重
        cos_th = fenzi / (fenmu1*fenmu2)
        # print("********************************推荐符合度",cos_th)
    
        return cos_th
    
    
    if __name__ == '__main__':
        orm_standby()
        from database import models
    
        username = "123"
        # 推荐用户
        user_id = models.UserInfo.objects.get(username=username).pk
        # 当前用户收藏数组
        user_collect_list = []
        user_collect_obj = models.Collect.objects.filter(user_id=user_id).values("scenic_spot_id")
        for collect_id in user_collect_obj:
            user_collect_list.append(collect_id.get("scenic_spot_id"))
    
        # 创建排序组
        sort_user_pk_list = []
        # 其他用户的收藏数组
        collect_list_all = []
        user_all = models.UserInfo.objects.values("pk").all()
        for user_obj in user_all:
            if user_obj.get("pk") != user_id:
                sort_user_pk_list.append(user_obj.get("pk"))
                collect_list_obj_other =  models.Collect.objects.filter(user_id=user_obj.get("pk")).values("scenic_spot_id")
                other_collect_list = []
                for collect_id in collect_list_obj_other:
                    other_collect_list.append(collect_id.get("scenic_spot_id"))
                collect_list_all.append(other_collect_list)
    
        # 获取最后一个PK     spots_last[0].get("pk")
        spots_last = models.ScenicSpot.objects.all().order_by("-id").values("pk")
        # 计算二值化
        user_binaryzation_list = make(user_collect_list, int(spots_last[0].get("pk")))
        # 计算最推荐值
        list_res = []
        for data in collect_list_all:
            list_res.append(calculation(user_binaryzation_list, make(data, int(spots_last[0].get("pk")))))
    
        max_index  = list_res.index(max(list_res))
        # 筛选出最匹配的用户
        print(sort_user_pk_list[max_index])

  • 相关阅读:
    uva 557 Burger
    uva 1639 Candy (对数处理精度)
    uva 10288 Coupons (分数模板)
    uva 12230 Crossing Rivers
    [USACO5.4] Telecowmunication
    epoll讲解--转自知乎
    多线程ExecutorService中submit和execute区别
    google Guava包的ListenableFuture解析
    git commit 时出现:please enter the commit message for your changes
    Git常用命令
  • 原文地址:https://www.cnblogs.com/wanghong1994/p/14084292.html
Copyright © 2011-2022 走看看