item-based CF(基于物品的协同过滤算法)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
item_based CF
"""


import numpy as np
import pandas as pd


class CFItem(object):
    def __init__(self, k):
        self.top_k = k

    @staticmethod
    def simu_cal_item(data_beha, i, j):
        data_beha = np.array(data_beha)
        data_beha = data_beha[:, [i, j]]
        index = np.where(data_beha == 0)[0]
        data_beha_a = np.delete(data_beha, obj=index, axis=0)
        if data_beha_a.size == 0:
            return 0
        else:
            data_beha_i = data_beha_a[:, 0]
            data_beha_j = data_beha_a[:, 1]
            return np.corrcoef(x=data_beha_i, y=data_beha_j)[0, 1]

    def get_simu_mat(self, data_beha):
        data_beha = np.array(data_beha)
        mat = np.zeros([data_beha.shape[1], data_beha.shape[1]])
        for i in range(mat.shape[0]):
            for j in range(mat.shape[1]):
                mat[i][j] = self.simu_cal_item(data_beha, i, j)
                mat[j][i] = mat[i][j]
        return mat

    def get_att_item_to_user(self, data_beha, user_index):
        data_beha = np.array(data_beha)
        data_i = data_beha[user_index, :]
        id0 = np.where(data_i > 7)[0]
        att = []
        simu_mat = self.get_simu_mat(data_beha=data_beha)
        for i in range(data_beha.shape[1]):
            att.append(np.dot(data_i[id0], simu_mat[i, id0]) / np.linalg.norm(simu_mat[i, id0], 1))
        return att

    def get_topk_recommendation(self, data_beha, user_index):
        data_beha = np.array(data_beha)
        data_i = data_beha[user_index, :]
        id0 = np.where(data_i <= 7)[0]
        att = pd.Series(self.get_att_item_to_user(data_beha=data_beha, user_index=user_index))
        att0 = att[id0]
        att0 = att0.sort_values(ascending=False)
        if len(att0) >= self.top_k:
            return {'item_index:': list(att0.index[:self.top_k]), 'attraction:': att0[att0.index[:self.top_k]]}
        elif len(att0) > 0:
            return {'item_index:': att0.index, 'attraction:': att0}
        else:
            return 'No item.'


if __name__ == '__main__':
    data = [[6, 3, 7, 2, 12, 19], [5, 6, 11, 8, 20, 1], [32, 2, 9, 10, 7, 12], [9, 4, 38, 3, 6, 21]]
    a = CFItem.simu_cal_item(data_beha=data, i=1, j=2)
    print(a)
    b = CFItem(2)
    c = b.get_simu_mat(data_beha=data)
    print(c)
    d = b.get_att_item_to_user(data_beha=data, user_index=2)
    print(d)
    e = b.get_topk_recommendation(data_beha=data, user_index=2)
    print(e)

欢迎交流探讨,QQ:944907482

你可能感兴趣的:(recommendation)