def test_one_user_by_matrix():
    '''
    give one fixed user , recommend result,by matrix
    :return:
    '''
    user = "******"
    alpha = 0.8
    graph = read.get_graph_from_data('ratings.csv')
    pr_recom_resuit = personal_rank_matrix(graph, user, alpha, 100)
    return pr_recom_resuit
def get_one_recom():
    '''
    give one fixed user , recommend result
    :return:
    '''
    user = "******"
    alpha = 0.8
    graph = read.get_graph_from_data('ratings.csv')
    iter_num = 100
    pr_recom_result = personal_rank(graph, user, alpha, iter_num, 100)
    return pr_recom_result
    '''
Exemplo n.º 3
0
def get_one_user_recom():
    user = '******'
    alpha = 0.8  # 0.7
    graph = read.get_graph_from_data('../data/ratings.txt')
    iter_num = 100 # 15
    recom_result = personal_rank(graph,user,alpha,iter_num)
    [print(i) for i in recom_result]

    print("========================================")
    # 解析,便于理解
    item_info = read.get_item_info('../data/movies.txt')
    # 将用户现在感兴趣的item打印出来
    for itemid in graph[user]:
        pure_itemid = itemid.split("_")[1]
        print(item_info[pure_itemid])
    print("=================推荐结果=================")
    # 将用户可能感兴趣的结果打印出来
    for itemid in recom_result:
        pure_itemid = itemid.split("_")[1]
        print(item_info[pure_itemid])
        print(recom_result[itemid])                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
Exemplo n.º 4
0

def mat_all_point(m_mat, vertex, alpha):
    """
    get E-alpha*m_mat.T
    Args:
        m_mat:
        vertex: total item and user point
        alpha: the prob for random walking
    Return:
        a sparse
    """
    total_len = len(vertex)
    row = []
    col = []
    data = []
    for index in range(total_len):
        row.append(index)
        col.append(index)
        data.append(1)
    row = np.array(row)
    col = np.array(col)
    data = np.array(data)
    eye_t = coo_matrix((data, (row, col)), shape=(total_len, total_len))
    return eye_t.tocsr() - alpha * m_mat.tocsr().transpose()


if __name__ == "__main__":
    graph = read.get_graph_from_data("../data/log.txt")
    m, vertex, address_dict = graph_to_m(graph)
    mat_all_point(m, vertex, 0.8)
Exemplo n.º 5
0
def get_one_user_by_mat(user):
    alpha = 0.6
    graph, graph_count = read.get_graph_from_data(r"..\data\企业交易数据all.txt")
    recom_result = personal_rank_mat(graph, graph_count, user, alpha)
    return recom_result
Exemplo n.º 6
0
        alpha:the prob for random walking随机游走概率
    return:
        sparse matrix
    """
    # np.eys()初始化单位矩阵易超内容
    total_len = len(vertex)
    row = []
    col = []
    data = []
    for index in range(total_len):
        row.append(index)
        col.append(index)
        data.append(1)
    row = np.array(row)
    col = np.array(col)
    data = np.array(data)
    eye_t = coo_matrix((data, (row, col)), shape=(total_len, total_len))
    print(eye_t.shape)
    return eye_t.tocsr() - alpha * m_mat.tocsr().transpose()


if __name__ == "__main__":
    graph = read.get_graph_from_data('../data/ratings.txt')
    m, vertex, address_dict = graph_to_m(graph)
    print(len(address_dict))
    print(m.todense())
    print(m.todense().shape)

    res_m = mat_all_point(m, vertex, 0.8)
    print(res_m.shape)
    print(res_m)
Exemplo n.º 7
0
def get_one_user_by_mat():
    user = '******'
    alpha = 0.8
    graph = read.get_graph_from_data('../data/ratings.txt')
    recom_result = personal_rank_mat(graph,user,alpha,10)
    return recom_result

def mat_all_point(m_matrix, vertex, alpha):
    '''
    矩阵算法personal_rank的公式
    :param m_matrix:
    :param vertex: 所有(item+user)顶点
    :param alpha: 随机游走的概率
    :return:  矩阵
    '''

    # 初始化单位矩阵(如果使用numpy创建,容易超内存)
    total_len = len(vertex)
    row = []
    col = []
    data = []
    for index in range(total_len):
        row.append(index)
        col.append(index)
        data.append(1)
    row = np.array(row)
    col = np.array(col)
    data = np.array(data)
    eye_t = coo_matrix((data, (row, col)), shape=(total_len, total_len))
    return eye_t.tocsr() - alpha * m_matrix.tocsr().transpose()


if __name__ == '__main__':
    graph = read.get_graph_from_data('log')
    m, vertex, address_dict = graph_to_m(graph)
    print(address_dict)
Exemplo n.º 9
0
def mat_all_point(m_mat, vertex, alpha):
    """
    get E-alpha*m_mat.T
        m_mat: m matrix
        vertex: all point
        alpha: the prob of walk
    return:
        a sparse matrix
    """
    total_len = len(vertex)
    row = []
    col = []
    data = []
    for index in range(total_len):
        row.append(index)
        col.append(index)
        data.append(1)
    row = np.array(row)
    col = np.array(col)
    data = np.array(data)
    eye_t = coo_matrix((data, (row, col)), shape=(total_len, total_len))
    return eye_t.tocsr() - alpha * m_mat.tocsr().transpose()


if __name__ == "__main__":
    graph = read.get_graph_from_data(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) + "\\recommender\\data\\log.txt")
    m, vertex, address_dict = graph_to_m(graph)
    # print(address_dict)
    # print(m.toarray())
    print(mat_all_point(m, vertex, 0.8))