当手动输入矩阵时,numpy.dot不同的输出



我想知道为什么np.dot(u,sv)!= np.dot(a,b)当我相信a = u和b = sv时,即使我手动关键在A和B的条目使用SVD恢复矩阵U和SV。下面的代码重现了奇数。

import numpy
from numpy.linalg import svd
    In [121]: fullSet = np.array([[100,50,50],[50,100,100],[20,130,130],[50,100,100]])
In [122]: print fullSet
[[100  50  50]
 [ 50 100 100]
 [ 20 130 130]
 [ 50 100 100]]
In [123]: 
In [123]: U,s,V = svd(fullSet,full_matrices=True)
In [124]: print 'U'
U
In [125]: print U.round()
[[ 0.  1. -0. -0.]
 [ 1.  0.  1. -0.]
 [ 1. -0. -1. -0.]
 [ 1.  0.  0.  1.]]
In [126]: 
In [126]: S = np.zeros((U.shape[1],V.shape[0]))
In [127]: S[:s.shape[0],:s.shape[0]] = np.diag(s)
In [128]: print 'S'
S
In [129]: print S.round()
[[ 296.    0.    0.]
 [   0.   82.    0.]
 [   0.    0.    0.]
 [   0.    0.    0.]]
In [130]: 
In [130]: print 'V'
V
In [131]: print V.round()
[[ 0.  1.  1.]
 [ 1. -0. -0.]
 [-0.  1. -1.]]
In [132]: 
In [132]: print 'SV'
SV
In [133]: print np.dot(S,V).round()
[[  97.  198.  198.]
 [  78.  -19.  -19.]
 [   0.    0.   -0.]
 [   0.    0.    0.]]
In [134]: print 'SV'
SV
In [135]: print np.dot(S,V).round()
[[  97.  198.  198.]
 [  78.  -19.  -19.]
 [   0.    0.   -0.]
 [   0.    0.    0.]]
In [136]: print 'USV'
USV
In [137]: SV=np.dot(S,V)
In [138]: print np.dot(U,SV)
[[ 100.   50.   50.]
 [  50.  100.  100.]
 [  20.  130.  130.]
 [  50.  100.  100.]]
In [139]: 
In [139]: 
In [139]: A = np.array([[0,1,0,0],[1,0,-1,0],[1,0,1,0],[1,0,0,1]])
In [140]: B = np.array([[97,198,198],[78,-19,-19],[0,0,0],[0,0,0]])
In [141]: print "A"
A
In [142]: print A
[[ 0  1  0  0]
 [ 1  0 -1  0]
 [ 1  0  1  0]
 [ 1  0  0  1]]
In [143]: print "B"
B
In [144]: print B
[[ 97 198 198]
 [ 78 -19 -19]
 [  0   0   0]
 [  0   0   0]]
In [145]: print "AdotB"
AdotB
In [146]: print np.dot(A,B)
[[ 78 -19 -19]
 [ 97 198 198]
 [ 97 198 198]
 [ 97 198 198]]
In [147]: print np.allclose(A,U.round())
False
In [148]: print np.allclose(B,SV.round())
True
In [149]: print A[0,0]
0
In [150]: print U[0,0]
0.33656051104

重新创建您的USV

In [627]: U
Out[627]: 
array([[ -3.36560511e-01,   8.66235179e-01,  -3.69274473e-01,
         -4.61618492e-16],
       [ -5.07358551e-01,   1.27694290e-02,   4.92365964e-01,
         -7.07106781e-01],
       [ -6.09837375e-01,  -4.99310021e-01,  -6.15457455e-01,
         -1.06278764e-15],
       [ -5.07358551e-01,   1.27694290e-02,   4.92365964e-01,
          7.07106781e-01]])
In [628]: SV
Out[628]: 
array([[ -9.65886537e+01,  -1.97578594e+02,  -1.97578594e+02],
       [  7.79142604e+01,  -1.90446580e+01,  -1.90446580e+01],
       [  0.00000000e+00,   4.63542263e-15,  -4.63542263e-15],
       [  0.00000000e+00,   0.00000000e+00,   0.00000000e+00]])
In [629]: np.dot(U,SV)
Out[629]: 
array([[ 100.,   50.,   50.],
       [  50.,  100.,  100.],
       [  20.,  130.,  130.],
       [  50.,  100.,  100.]])

具有圆形值的dot不会产生相同的结果:

In [630]: np.dot(U.round(),SV.round())
Out[630]: 
array([[  78.,  -19.,  -19.],
       [  97.,  198.,  198.],
       [  97.,  198.,  198.],
       [  97.,  198.,  198.]])

USV的完整浮点值具有显着差异。请记住,dot是产品的总和 - 这些是

相关内容

  • 没有找到相关文章

最新更新