Lambda architecture recommendation system algorithm (B): K nearest neighbor based on collaborative filtering algorithm and its realization

Collaborative filtering recommendation based on K nearest neighbors

K-nearest neighbor collaborative filtering in fact essentially MemoryBased CF, but when selecting neighbors, plus limit K Nearest Neighbor.

Here we are directly implemented in accordance with the code of MemoryBased CF

Modify the following places

class CollaborativeFiltering(object):

    based = None

    def __init__(self, k=40, rules=None, use_cache=False, standard=None):
        '''
        :param k: 取K个最近邻来进行预测
        :param rules: 过滤规则,四选一,否则将抛异常:"unhot", "rated", ["unhot","rated"], None
        :param use_cache: 相似度计算结果是否开启缓存
        :param standard: 评分标准化方法,None表示不使用、mean表示均值中心化、zscore表示Z-Score标准化
        '''
        self.k = 40
        self.rules = rules
        self.use_cache = use_cache
        self.standard = standard

Modify all selected local neighborhood code, according to the similarity to select K nearest neighbor

similar_users = self.similar[uid].drop([uid]).dropna().sort_values(ascending=False)[:self.k]

similar_items = self.similar[iid].drop([iid]).dropna().sort_values(ascending=False)[:self.k]

But because fewer of our original data, where the effect of our KNN method will be worse than pure MemoryBasedCF

Published 651 original articles · won praise 866 · views 110 000 +

Guess you like

Origin blog.csdn.net/qq_35456045/article/details/104732457
Recommended