<jupyter_start><jupyter_text>推荐系统实践  UserCF  计算用户的余弦相似度<jupyter_code>import numpy as np
train={
    'A':(1,2,3),
    'B':(1,4),
    'C':(2,3,5,6),
    'D':(2,4,5,8)
      }
train<jupyter_output><empty_output><jupyter_text>计算a,b list中元素相同的个数，实践复杂度 O(len(a)*len(b))<jupyter_code>def cross_num(a,b):
    co = [ i==j for i in a for j in b]
    counts=0
    for c in co: 
        if (c==1): 
            counts+=1
    return counts<jupyter_output><empty_output><jupyter_text>根据user list，得到use对，(u,v)<jupyter_code>def get_two_users(users):
    res=[]
    for i,init_u in enumerate(users):
        for j,u in enumerate(users):
            if i <j : res.append(init_u+u)
    return res
get_two_users( list(train.keys()) )
W={}
users = train.keys()

for i,init_u in enumerate(users):
    for j,u in enumerate(users):
        if i <j : W[init_u+u] = cross_num(train[init_u],train[u]) / np.sqrt( len(train[init_u]) * len(train[u]))
W<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16911764705882354, 'fraction_numerical': 0.01680672268907563, 'mean_word_length': 3.8622448979591835, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '411488', 'license': 'Apache-2.0', 'n_tokens_mistral': 444, 'n_tokens_neox': 446, 'n_words': 74, 'path': 'recommender_system_ex.ipynb', 'repo_name': 'anribras/machine-learning', 'size': '952'}