机器学习相关——SVD分解:
http://www.cnblogs.com/luchen927/archive/2012/01/19/2321934.html
SVD python实现:
import numpy as np
from numpy import dot def loadData():
return [[1,1,1,0,0],
[2,2,2,0,0],
[3,3,3,0,0],
[5,5,3,2,2],
[0,0,0,3,3],
[0,0,0,6,6]] data=loadData() u,sigma,v=linalg.svd(data) print sigma
print u
print v def restore(sigma, u, v, k): # 取k个特征,对矩阵重新进行计算
m = len(u)
n = len(v)
b = np.zeros((m, n))
for j in range(k-1):
print "j=" ,j
for i in range(m):
print "i=",i
print "sigma[j]",sigma[j]
print "u[i][j]",u[i][j]
print "v[j]",v[j]
b[i] += sigma[j] * u[i][j] * v[j]
print b
return b result=restore(sigma, u, v, 3)
print result def restore2(sigma, u, v, k):
b = np.zeros((k, k))
for i in range(k):
b[i][i] = sigma[i]
out = dot(dot(u[:,:k],b),v[:k])
return out result2=restore2(sigma,u,v,2)
print result2