当前位置:网站首页>矩阵分解
矩阵分解
2022-06-22 08:18:00 【Alex_81D】
1.先了解一下 矩阵乘法(内积 点乘)
矩阵的内积参照向量的内积的定义的,先说说向量:两个向量对应乘积之和,
比如: α=(1,2,3), β=(4,5,6),则 α, β的内积等于 1*4 +2*5 + 3*6 = 32 ,α与α 的内积 = 1*1+2*2+3*3 = 14
内积:矩阵内积也就是矩阵的乘积, AB 和 BA 结果不一定相等,且前面可乘不代表后面可乘。
要求 A 的列等于 B 的行的两个矩阵才可以做外积,内积乘法规则是: A 的行乘以 B 的列,结果仍为矩阵。

再比如C=A*B,则A的列数要与B的行数一致,例如C为 [m,n] , 则A为[m,k], B 为[k,n]
两个向量点积结果是一个实数(即标量);两个矩阵相乘结果是一个矩阵
两个相同维数的向量x 和y 的点积(dot product)可看作是矩阵乘积x⊤y。
这个推荐一篇文章觉得说的不错:
花书笔记1——向量乘法、矩阵乘积(相乘)、内积、点积都是什么:
https://blog.csdn.net/yl_best/article/details/86702200
2.dot()的使用
dot()返回的是两个数组的点积(dot product)
a.如果处理的是一维数组,则得到的是两数组的內积

b.如果是二维数组(矩阵)之间的运算,则得到的是矩阵乘积(mastrix product)。所得到的数组中的每个元素为,第一个矩阵中与该元素行号相同的元素与第二个矩阵与该元素列号相同的元素,两两相乘后再求和.

d = np.arange(0,9)
d
e = d[::-1]
e
np.dot(d,e)
a = np.arange(1,5).reshape(2,2)
a
b = np.arange(5,9).reshape(2,2)
b
np.dot(a,b)c.dot()函数可以通过numpy库调用,也可以由数组实例对象进行调用。a.dot(b) 与 np.dot(a,b)效果相同
矩阵积计算不遵循交换律,np.dot(a,b) 和 np.dot(b,a) 得到的结果是不一样的
3.矩阵分解
这个以推荐系统的稀疏矩阵为例:
有如下R(5,4)的打分矩阵:(“-”表示用户没有打分) ,其中打分矩阵R(m,n)是n行和m列,m表示user个数,n行表示item个数

那么,如何根据目前的矩阵R(5,4)如何对未打分的商品进行评分的预测(如何得到分值为0的用户的打分值)?
——矩阵分解的思想可以解决这个问题,其实这种思想可以看作是有监督的机器学习问题(回归问题)。
矩阵R可以近似表示为P与Q的乘积:R(m,n)≈ P(m,K)*Q(K,n)
矩阵分解的过程中,将原始的评分矩阵
分解成两个矩阵
和
的乘积: 
矩阵P(m,K)表示m个user和K个特征之间的关系矩阵,这K个特征是一个中间变量,矩阵Q(K,n)的转置是矩阵Q(n,K),矩阵Q(n,K)表示n个item和K个特征之间的关系矩阵,这里的K值是自己控制的,可以使用交叉验证的方法获得最佳的K值。为了得到近似的R(m,n),必须求出矩阵P和Q,如何求它们呢?
1. 首先令![]()
2. 损失函数:使用原始的评分矩阵
与重新构建的评分矩阵
之间的误差的平方作为损失函数,即:
如果R(i,j)已知,则R(i,j)的误差平方和为:
最终,需要求解所有的非“-”项的损失之和的最小值:
3. 使用梯度下降法获得修正的p和q分量:
- 求解损失函数的负梯度:

- 根据负梯度的方向更新变量:

4. 不停迭代直到算法最终收敛(直到sum(e^2) <=阈值)
为了防止过拟合,增加正则化项
【加入正则项的损失函数求解】
1. 首先令![]()
2. 通常在求解的过程中,为了能够有较好的泛化能力,会在损失函数中加入正则项,以对参数进行约束,加入
正则的损失函数为:

也即:

3. 使用梯度下降法获得修正的p和q分量:
- 求解损失函数的负梯度:

- 根据负梯度的方向更新变量:

4. 不停迭代直到算法最终收敛(直到sum(e^2) <=阈值)
【预测】利用上述的过程,我们可以得到矩阵
和
,这样便可以为用户 i 对商品 j 进行打分:

# !/usr/bin/env python
# encoding: utf-8
__author__ = 'Scarlett'
#矩阵分解在打分预估系统中得到了成熟的发展和应用
# from pylab import *
import matplotlib.pyplot as plt
from math import pow
import numpy
def matrix_factorization(R,P,Q,K,steps=5000,alpha=0.0002,beta=0.02):
Q=Q.T # .T操作表示矩阵的转置
result=[]
for step in range(steps):
for i in range(len(R)):
for j in range(len(R[i])):
if R[i][j]>0:
eij=R[i][j]-numpy.dot(P[i,:],Q[:,j]) # .dot(P,Q) 表示矩阵乘积
for k in range(K):
P[i][k]=P[i][k]+alpha*(2*eij*Q[k][j]-beta*P[i][k])
Q[k][j]=Q[k][j]+alpha*(2*eij*P[i][k]-beta*Q[k][j])
eR=numpy.dot(P,Q)
e=0
for i in range(len(R)):
for j in range(len(R[i])):
if R[i][j]>0:
e=e+pow(R[i][j]-numpy.dot(P[i,:],Q[:,j]),2)
for k in range(K):
e=e+(beta/2)*(pow(P[i][k],2)+pow(Q[k][j],2))
result.append(e)
if e<0.001:
break
return P,Q.T,result
if __name__ == '__main__':
R=[
[5,3,0,1],
[4,0,0,1],
[1,1,0,5],
[1,0,0,4],
[0,1,5,4]
]
R=numpy.array(R)
N=len(R)
M=len(R[0])
K=2
P=numpy.random.rand(N,K) #随机生成一个 N行 K列的矩阵
Q=numpy.random.rand(M,K) #随机生成一个 M行 K列的矩阵
nP,nQ,result=matrix_factorization(R,P,Q,K)
print("原始的评分矩阵R为:\n",R)
R_MF=numpy.dot(nP,nQ.T)
print("经过MF算法填充0处评分值后的评分矩阵R_MF为:\n",R_MF)
#-------------损失函数的收敛曲线图---------------
n=len(result)
x=range(n)
plt.plot(x,result,color='r',linewidth=3)
plt.title("Convergence curve")
plt.xlabel("generation")
plt.ylabel("loss")
plt.show()运行结果如下:

【代码的GitHub地址】
https://github.com/shenxiaolinZERO/CoolRSer/
借鉴博主的文章:地址为:https://www.cnblogs.com/shenxiaolin/p/8637794.html
边栏推荐
- Object to string pit
- How to select the appropriate partition key, routing rule and partition number
- Analyzing the role of cognitive theory in maker teacher training
- DTD约束
- Some mathematical operation functions in LabVIEW
- Example of using listagg in Oracle 11g (splicing the values of a field into a line, separated by commas)
- Mt4/mql4 getting started to mastering EA tutorial lesson 4 - common functions of MQL language (IV) - common functions of K-line value
- Seven challenges faced by CIO in 2022 and Solutions
- 邮件巨头曝严重漏洞,用户数据被窃取
- swagger中的枚举、自定义类型和swaggerignore
猜你喜欢

Carry out effective maker education courses and activities

Mt4/mql4 getting started to proficient in foreign exchange EA automatic trading tutorial - common functions of MQL language

复杂科学在创客教学研究中的应用

成为一名卓越云架构师要具备的五项技能

FastCorrect:语音识别快速纠错模型丨RTC Dev Meetup

QT error prompt 1:invalid use of incomplete type '***‘

EURUSD,H1: invalid lots amount for OrderSend function

Nisp online simulation question bank

并发三大特性1-可见性

Concatenate the specified character at the end of a number in a string
随机推荐
第八章 Web项目测试(此章完结)
Three concurrent features 1- visibility
QT 自定义组合控件(类提升功能)
Concatenate the specified character at the end of a number in a string
JSON usage example
EURUSD,H1: invalid lots amount for OrderSend function
Using KDJ metrics on MT4
DTD约束
Mt4/mql4 getting started to mastering EA tutorial lesson 4 - common functions of MQL language (IV) - common functions of K-line value
每周推荐短视频:什么是“计算世界”?
Five skills to be an outstanding cloud architect
先锋期货安全么?期货开户都是哪些流程?期货手续费怎么降低?
Questions 101 to 200 of the national information security grade examination nisp level 1 question bank (1)
并发三大特性1-可见性
JSON使用示例
Bee framework, an ORM framework
Alibaba cloud ~ simply send SMS
0基础自学stm32(野火)——什么是寄存器?
2022年CIO面临的七大挑战及应对方法
Master data management system