1. 问题提出
这一节将讨论协同过滤算法的向量化实现,并且讨论这个算法可以实现的一些功能,例如,给定一个产品可以找到与之相关的其他商品,一个用户最近一直在寻找一个商品有没有相关的其他商品。即我们希望通过另一种方法,写出协同过滤算法
Movie | Alice (1) | Bob (2) | Carol (3) | Dave (4) |
---|---|---|---|---|
Love at last | 5 | 5 | 0 | 0 |
Romance forever | 5 | ? | ? | 0 |
Cute Puppies of love | ? | 4 | 0 | ? |
Nonstop car chases | 0 | 0 | 5 | 4 |
Swords vs. karate | 0 | 0 | 5 | ? |
2. 协同过滤
以上表格可以表示为
Y
Y
Y ,5行4列的矩阵,
n
m
=
5
,
n
u
=
4
n_m=5, n_u=4
nm=5,nu=4
Y
=
[
5
5
0
0
5
?
?
0
?
4
0
?
0
0
5
4
0
0
5
0
]
Y=\left[\begin{array}{llll} 5 & 5 & 0 & 0 \\ 5 & ? & ? & 0 \\ ? & 4 & 0 & ? \\ 0 & 0 & 5 & 4 \\ 0 & 0 & 5 & 0 \end{array}\right]
Y=⎣⎢⎢⎢⎢⎡55?005?4000?05500?40⎦⎥⎥⎥⎥⎤
Y
Y
Y 还有另一种表达方式,为预测的评分:
[
(
θ
(
1
)
)
T
(
x
(
1
)
)
(
θ
(
2
)
)
T
(
x
(
1
)
)
⋯
(
θ
(
n
u
)
)
T
(
x
(
1
)
)
(
θ
(
1
)
)
T
(
x
(
2
)
)
(
θ
(
2
)
)
T
(
x
(
2
)
)
⋯
(
θ
(
n
u
)
)
T
(
x
(
2
)
)
⋮
⋮
⋮
⋮
(
θ
(
1
)
)
T
(
x
(
n
m
)
)
(
θ
(
2
)
)
T
(
x
(
n
m
)
)
⋯
(
θ
(
n
u
)
)
T
(
x
(
n
m
)
)
]
\left[\begin{array}{cccc} \left(\theta^{(1)}\right)^{T}\left(x^{(1)}\right) & \left(\theta^{(2)}\right)^{T}\left(x^{(1)}\right) & \cdots & \left(\theta^{\left(n_{u}\right)}\right)^{T}\left(x^{(1)}\right) \\ \left(\theta^{(1)}\right)^{T}\left(x^{(2)}\right) & \left(\theta^{(2)}\right)^{T}\left(x^{(2)}\right) & \cdots & \left(\theta^{\left(n_{u}\right)}\right)^{T}\left(x^{(2)}\right) \\ \vdots & \vdots & \vdots & \vdots \\ \left(\theta^{(1)}\right)^{T}\left(x^{\left(n_{m}\right)}\right) & \left(\theta^{(2)}\right)^{T}\left(x^{\left(n_{m}\right)}\right) & \cdots & \left(\theta^{\left(n_{u}\right)}\right)^{T}\left(x^{\left(n_{m}\right)}\right) \end{array}\right]
⎣⎢⎢⎢⎢⎡(θ(1))T(x(1))(θ(1))T(x(2))⋮(θ(1))T(x(nm))(θ(2))T(x(1))(θ(2))T(x(2))⋮(θ(2))T(x(nm))⋯⋯⋮⋯(θ(nu))T(x(1))(θ(nu))T(x(2))⋮(θ(nu))T(x(nm))⎦⎥⎥⎥⎥⎤
3. 低秩矩阵分解
有一个比较简单的或者向量化的方法来写出
x
x
x 和
θ
\theta
θ,
X
X
X 表示为:
X
=
[
(
x
(
1
)
)
T
(
x
(
2
)
)
T
⋮
(
x
(
n
)
)
T
]
X= \left[\begin{array}{cccc} (x^{(1)})^T \\ (x^{(2)})^T \\ \vdots\\ (x^{(n)})^T \\ \end{array}\right]
X=⎣⎢⎢⎢⎡(x(1))T(x(2))T⋮(x(n))T⎦⎥⎥⎥⎤
Θ
\Theta
Θ 表示为:
Θ
=
[
(
θ
(
1
)
)
T
(
θ
(
2
)
)
T
⋮
(
θ
(
n
)
)
T
]
\Theta= \left[\begin{array}{cccc} (\theta^{(1)})^T \\ (\theta^{(2)})^T \\ \vdots\\ (\theta^{(n)})^T \\ \end{array}\right]
Θ=⎣⎢⎢⎢⎡(θ(1))T(θ(2))T⋮(θ(n))T⎦⎥⎥⎥⎤
那么
Y
Y
Y 可以表示为:
X
⋅
Θ
X\cdot\Theta
X⋅Θ
这种方式称为低秩矩阵分解。
4. 找到相关的电影
对于每一个电影
i
i
i ,学习一个属性向量
x
(
i
)
∈
R
n
x^{(i)}\in\mathbb{R}^n
x(i)∈Rn .例如:
x
1
=
r
o
m
a
n
c
e
,
x
2
=
a
c
t
i
o
n
,
x
3
=
c
o
m
e
d
y
,
x
4
=
…
x_1=romance, x_2=action, x_3=comedy,x_4=\ldots
x1=romance,x2=action,x3=comedy,x4=…
如何找到和电影
i
i
i 相关的电影
j
j
j ?
如果电影 i i i 和电影 j j j 的属性向量之间的距离很小,那么它们两个就很相关。属性向量之间的距离可以表示为: ∣ ∣ x ( i ) − x ( j ) ∣ ∣ ||x^{(i)}-x^{(j)}|| ∣∣x(i)−x(j)∣∣ 。
如果想要找到与电影 i i i 最相似的前5部电影,那么就找到这5个电影 j j j 与 i i i 有最小的5个 ∣ ∣ x ( i ) − x ( j ) ∣ ∣ ||x^{(i)}-x^{(j)}|| ∣∣x(i)−x(j)∣∣ 值。