ALS 算法

 

一、业务背景

主要是没出评分值的user_item_mat ,计算缺失值;

 

二、算法推导

 

 

三、用python实现算法流程

3.1  生成user_item_mat :

import numpy as np
LATENT_FACTOR = 4
user_set = tuple(['user' + str(i) for i in range(1, 8)])
item_set = tuple(['item' + str(i) for i in range(1, 10)])
rating_dic = {'user1': {'item6': 4, 'item8': 8},
              'user2': {'item3': 6, 'item5': 1, 'item7': 7},
              'user3': {'item2': 4, 'item4': 4, 'item9': 5},
              'user4': {'item3': 5, 'item4': 2, 'item9': 3},
              'user5': {'item5': 7, 'item7': 1},
              'user6': {'item1': 9, 'item6': 5},
              'user7': {'item1': 7, 'item6': 3, 'item7': 5}}
user_item_mat = np.zeros((len(user_set), len(item_set)))
for i, user in enumerate(user_set):
    for j, item in enumerate(item_set):
        if item in rating_dic[user].keys():
            user_item_mat[i][j] = rating_dic[user][item]
print(user_item_mat)

生成结果:

[[0. 0. 0. 0. 0. 4. 0. 8. 0.]
 [0. 0. 6. 0. 1. 0. 7. 0. 0.]
 [0. 4. 0. 4. 0. 0. 0. 0. 5.]
 [0. 0. 5. 2. 0. 0. 0. 0. 3.]
 [0. 0. 0. 0. 7. 0. 1. 0. 0.]
 [9. 0. 0. 0. 0. 5. 0. 0. 0.]
 [7. 0. 0. 0. 0. 3. 5. 0. 0.]]

3.2 随机化生成用户特征矩阵 和物品 特征矩阵

#  随机化生成用户特征矩阵和物品特征矩阵
user_fea_mat = np.random.rand(len(user_set), LATENT_FACTOR)
item_fea_mat = np.random.rand(len(item_set), LATENT_FACTOR)

结果显示(这里设置隐因子为:4)

 

用户特征矩阵
[[0.08713515 0.965344   0.303895   0.49206858]
 [0.61075869 0.833574   0.50883378 0.77677441]
 [0.17092587 0.16123455 0.48475028 0.86985211]
 [0.98051947 0.04303567 0.72002434 0.36179213]
 [0.19622217 0.38446489 0.06609381 0.9874216 ]
 [0.59425107 0.15897335 0.02986513 0.08384685]
 [0.25069786 0.0656715  0.19319849 0.01860785]]
物品特征矩阵

[[0.09508884 0.18402322 0.13596882 0.59230289]
 [0.41094932 0.78011752 0.93166365 0.01186177]
 [0.34813858 0.76486845 0.81440773 0.37559024]
 [0.43278413 0.67868955 0.08026823 0.03558632]
 [0.02527359 0.01883249 0.65428009 0.58995535]
 [0.30562352 0.68324911 0.82264174 0.66525049]
 [0.47565535 0.46621474 0.02619889 0.20191455]
 [0.46422544 0.10559926 0.78237686 0.78689683]
 [0.76574482 0.12160784 0.18476729 0.33489597]]

 

 

 

 

 

你可能感兴趣的:(spark)