2015-05-25-nmf-svd-recommend
本文以NMF和经典SVD为例,讲一讲矩阵分解在推荐系统中的应用。
数据
item\user | Ben | Tom | John | Fred | ||||
---|---|---|---|---|---|---|---|---|
item 2 | 5 | 0 | 3 | 4 | ||||
item 3 | 3 | 4 | 0 | 3 | ||||
item 4 | 0 | 0 | 5 | 3 | ||||
item 5 | 5 | 4 | 4 | 5 | ||||
item 6 | 5 | 4 | 5 | 5 |
user\item | item 1 | item 2 | item 3 | item 4 |
---|---|---|---|---|
Ben | 5 | 5 | 3 | 0 |
Tom | 5 | 0 | 4 | 0 |
John | 0 | 3 | 0 | 5 |
Fred | 5 | 4 | 3 | 3 |
NMF
关于NMF,在 .... 已经有过介绍。
用户和物品的主题分布
#!/usr/bin/python2.7
# coding: UTF-8
import numpy as np
from sklearn.decomposition import NMF
import matplotlib.pyplot as plt
RATE_MATRIX = np.array(
[[5, 5, 3, 0, 5, 5],
[5, 0, 4, 0, 4, 4],
[0, 3, 0, 5, 4, 5],
[5, 4, 3, 3, 5, 5]]
)
nmf = NMF(n_components=2)
user_distribution = nmf.fit_transform(RATE_MATRIX)
item_distribution = nmf.components_
print '用户的主题分布:'
print user_distribution
print '物品的主题分布:'
print item_distribution
运行后输出:
用户的主题分布:
[[ 2.20884275 0.84137492]
[ 2.08253282 -0. ]
[-0. 3.18154406]
[ 1.84992603 1.60839505]]
物品的主题分布:
[[ 2.4129931 1.02524235 1.62258152 0. 1.80111078 1.69591943]
[ 0.0435741 1.13506094 0. 1.54526337 1.21253494 1.48756118]]
可视化物品的主题分布:
#!/usr/bin/python2.7
# coding: UTF-8
import numpy as np
from sklearn.decomposition import NMF
import matplotlib.pyplot as plt
RATE_MATRIX = np.array(
[[5, 5, 3, 0, 5, 5],
[5, 0, 4, 0, 4, 4],
[0, 3, 0, 5, 4, 5],
[5, 4, 3, 3, 5, 5]]
)
nmf = NMF(n_components=2)
user_distribution = nmf.fit_transform(RATE_MATRIX)
item_distribution = nmf.components_
item_distribution = item_distribution.T
plt.plot(item_distribution[:, 0], item_distribution[:, 1], "b*")
plt.xlim((-1, 3))
plt.ylim((-1, 3))
plt.title(u'the distribution of items (NMF)')
count = 1
for item in item_distribution:
plt.text(item[0], item[1], 'item '+str(count), bbox=dict(facecolor='red', alpha=0.2),)
count += 1
plt.show()
结果:
从距离的角度来看,item 5和item 6比较类似;从余弦相似度角度看,item 2、5、6 比较相似,item 1、3比较相似。
可视化用户的主题分布:
#!/usr/bin/python2.7
# coding: UTF-8
import numpy as np
from sklearn.decomposition import NMF
import matplotlib.pyplot as plt
RATE_MATRIX = np.array(
[[5, 5, 3, 0, 5, 5],
[5, 0, 4, 0, 4, 4],
[0, 3, 0, 5, 4, 5],
[5, 4, 3, 3, 5, 5]]
)
nmf = NMF(n_components=2)
user_distribution = nmf.fit_transform(RATE_MATRIX)
item_distribution = nmf.components_
users = ['Ben', 'Tom', 'John', 'Fred']
zip_data = zip(users, user_distribution)
plt.title(u'the distribution of users (NMF)')
plt.xlim((-1, 3))
plt.ylim((-1, 4))
for item in zip_data:
user_name = item[0]
data = item[1]
plt.plot(data[0], data[1], "b*")
plt.text(data[0], data[1], user_name, bbox=dict(facecolor='red', alpha=0.2),)
plt.show()
结果:
从距离的角度来看,Fred、Ben、Tom的口味差不多;从余弦相似度角度看,Fred、Ben、Tom的口味还是差不多。
如何推荐
现在对于用户A,如何向其推荐物品呢?
方法1:找出与用户A最相似的用户B,将B评分过的、评分较高、A没评分过的的若干物品推荐给A。
方法2:找出用户A评分较高的若干物品,找出与这些物品相似的、且A没评分的若干物品推荐给A。
方法3:找出用户A最感兴趣的k个主题,找出最符合这k个主题的、且A没评分的若干物品推荐给A。
方法4:由NMF的评分结果,重建评分矩阵。例如:
#!/usr/bin/python2.7
# coding: UTF-8
import numpy as np
from sklearn.decomposition import NMF
import matplotlib.pyplot as plt
RATE_MATRIX = np.array(
[[5, 5, 3, 0, 5, 5],
[5, 0, 4, 0, 4, 4],
[0, 3, 0, 5, 4, 5],
[5, 4, 3, 3, 5, 5]]
)
RATE_MATRIX[1, 2] = 0 # 对评分矩阵略做修改
print '新评分矩阵:'
print RATE_MATRIX
nmf = NMF(n_components=2)
user_distribution = nmf.fit_transform(RATE_MATRIX)
item_distribution = nmf.components_
reconstruct_matrix = np.dot(user_distribution, item_distribution)
filter_matrix = RATE_MATRIX < 1e-6 # 小于0
print '重建矩阵,并过滤掉已经评分的物品:'
print reconstruct_matrix*filter_matrix
运行结果:
新评分矩阵:
[[5 5 3 0 5 5]
[5 0 0 0 4 4]
[0 3 0 5 4 5]
[5 4 3 3 5 5]]
重建矩阵,并过滤掉已经评分的物品:
[[ 0. 0. 0. 0.80443133 0. 0. ]
[ 0. 2.19148602 1.73560797 0. 0. 0. ]
[ 0.02543568 0. 0.48692891 0. 0. 0. ]
[ 0. 0. 0. 0. 0. 0. ]]
对于Tom(评分矩阵的第2行),其未评分过的物品是item 2、item 3、item 4。item 2的推荐值是 2.19148602
,item 3的推荐值是 1.73560797
,item 4的推荐值是0
,若要推荐一个物品,推荐item 2。
如何处理有评分记录的新用户
NMF是将非负矩阵V分解为两个非负矩阵W和H:
V = W×H
在本文上面的实现中,V对应评分矩阵,W是用户的主题分布,H是物品的主题分布。
对于有评分记录的新用户,如何得到其主题分布?
方法1:有评分记录的新用户的评分数据放入评分矩阵中,使用NMF处理新的评分矩阵。
方法2:物品的主题分布矩阵H保持不变,将W更换为新用户的评分组成的行向量,求W即可。
下面尝试一下方法2。
设新用户Bob的评分记录为:
[5,5,0,0,0,5]
#!/usr/bin/python2.7
# coding: UTF-8
import numpy as np
from sklearn.decomposition import NMF
import matplotlib.pyplot as plt
RATE_MATRIX = np.array(
[[5, 5, 3, 0, 5, 5],
[5, 0, 4, 0, 4, 4],
[0, 3, 0, 5, 4, 5],
[5, 4, 3, 3, 5, 5]]
)
nmf = NMF(n_components=2)
user_distribution = nmf.fit_transform(RATE_MATRIX)
item_distribution = nmf.components_
bob = [5, 5, 0, 0, 0, 5]
print 'Bob的主题分布:'
print nmf.transform(bob)
运行结果是:
Bob的主题分布:
[[ 1.37800534 0.69236738]]
经典SVD
#!/usr/bin/python2.7
# coding: UTF-8
import numpy as np
from scipy.sparse.linalg import svds
from scipy import sparse
import matplotlib.pyplot as plt
def vector_to_diagonal(vector):
"""
将向量放在对角矩阵的对角线上
:param vector:
:return:
"""
if (isinstance(vector, np.ndarray) and vector.ndim == 1) or \
isinstance(vector, list):
length = len(vector)
diag_matrix = np.zeros((length, length))
np.fill_diagonal(diag_matrix, vector)
return diag_matrix
return None
RATE_MATRIX = np.array(
[[5, 5, 3, 0, 5, 5],
[5, 0, 4, 0, 4, 4],
[0, 3, 0, 5, 4, 5],
[5, 4, 3, 3, 5, 5]]
)
RATE_MATRIX = RATE_MATRIX.astype('float')
U, S, VT = svds(sparse.csr_matrix(RATE_MATRIX), k=2, maxiter=200)
S = vector_to_diagonal(S)
print '用户的主题分布:'
print U
print '奇异值:'
print S
print '物品的主题分布:'
print VT
print '重建评分矩阵,并过滤掉已经评分的物品:'
print np.dot(np.dot(U, S), VT) * (RATE_MATRIX < 1e-6)
运行结果:
用户的主题分布:
[[-0.22279713 0.57098887]
[-0.51723555 0.4274751 ]
[ 0.82462029 0.38459931]
[ 0.05319973 0.58593526]]
奇异值:
[[ 6.39167145 0. ]
[ 0. 17.71392084]]
物品的主题分布:
[[-0.53728743 0.24605053 -0.40329582 0.67004393 0.05969518 0.18870999]
[ 0.44721867 0.35861531 0.29246336 0.20779151 0.50993331 0.53164501]]
重建评分矩阵,并过滤掉已经评分的物品:
[[ 0. 0. 0. 1.14752376 0. 0. ]
[ 0. 1.90208543 0. -0.64171368 0. 0. ]
[ 0.21491237 0. -0.13316888 0. 0. 0. ]
[ 0. 0. 0. 0. 0. 0. ]]
可视化一下:
经典SVD + 协同过滤
这个算法来自下面这篇论文:
Vozalis M G, Margaritis K G. Applying SVD on Generalized Item-based Filtering[J]. IJCSA, 2006, 3(3): 27-51.
参考
SVD Recommendation System in Ruby 这篇文章使用的数据来自该链接,里面处理新用户的方法表示没看懂。
How do I use the SVD in collaborative filtering?
Vozalis M G, Margaritis K G. Applying SVD on Generalized Item-based Filtering[J]. IJCSA, 2006, 3(3): 27-51.