[英]Matrix Multiplication in python vs R not returning the same results for SVD whitening
I'm trying this simple whitening function in python in R我正在尝试这个简单的美白 function 在 python 在 R
Python Python
def svd_whiten(X):
U, s, Vt = np.linalg.svd(X, full_matrices=False)
#print(U)
#print(Vt)
# U and Vt are the singular matrices, and s contains the singular values.
# Since the rows of both U and Vt are orthonormal vectors, then U * Vt
# will be white
X_white = np.dot(U, Vt)
return X_white
Read Python Data读取 Python 数据
df = pd.read_csv("https://raw.githubusercontent.com/thistleknot/Python-Stock/master/data/raw/states.csv")
pd.DataFrame(svd_whiten(df.iloc[:,2:]))
R R
ZCA_svd <- function(x)
{
internal <- svd(x)
U = internal$u
#print(U)
Vt = internal$v
#print(Vt)
s = internal$d
#U, s, Vt = np.linalg.svd(X, full_matrices=False)
# U and Vt are the singular matrices, and s contains the singular values.
# Since the rows of both U and Vt are orthonormal vectors, then U * Vt
# will be white
#dot(U,Vt)
X_white = U%*%Vt
#np$dot(U,Vt)
#
return(X_white)
}
R Data R 数据
x_ = read.csv(file="https://raw.githubusercontent.com/thistleknot/Python-Stock/master/data/raw/states.csv",header =TRUE,row.names = 1)
x = x_[,2:ncol(x_)]
ZCA_svd(x)
If I print the values of U and Vt in either R or Python, they are the same, but when multiplied, the results are different between R and Python. If I print the values of U and Vt in either R or Python, they are the same, but when multiplied, the results are different between R and Python.
To add to the fun, if I use reticulate and import numpy via np$dot(U, Vt).为了增加乐趣,如果我使用 reticulate 并通过 np$dot(U, Vt) 导入 numpy。 The results are the same as U%*%Vt.
结果与 U%*%Vt 相同。 As a result.
因此。 I'm not sure which is the " correct " version to use.
我不确定要使用哪个“ 正确”版本。
It is usually written wiki , where V* is the transpose of V:通常写成wiki ,其中 V* 是 V 的转置:
And this is what you get back in scipy.linalg.svd :这就是你在scipy.linalg.svd中得到的:
Factorizes the matrix a into two unitary matrices U and Vh, and a 1-D array s of singular values (real, non-negative) such that a == U @ S @ Vh, where S is a suitably shaped matrix of zeros with main diagonal s.
将矩阵 a 分解为两个酉矩阵 U 和 Vh,以及奇异值(实数,非负)的一维数组 s,使得 a == U @ S @ Vh,其中 S 是一个适当形状的零矩阵,其中主对角线 s.
Whereas for svd in R they return you V. Therefore should be:而对于 R 中的svd ,他们会返回 V。因此应该是:
Vt = t(internal$v)
Then in R:然后在 R 中:
ZCA_svd(x)
head(ZCA_svd(x))
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.26067006 0.02112997 0.09365719 0.01843731 0.05470893 0.01750415
[2,] -0.17174605 -0.23530453 0.15122167 -0.27738192 0.03830312 -0.21142466
[3,] -0.10659408 0.07042392 0.06732517 -0.12081178 0.09487670 -0.01726953
[4,] 0.10659431 0.13668984 0.18523379 0.03799714 0.06525643 -0.09888497
[5,] -0.12998931 -0.05254591 -0.14654516 -0.15600721 0.13455552 -0.09930468
[6,] -0.07010493 0.01084335 -0.05152612 -0.07803706 -0.03505320 0.43416503
[,7] [,8] [,9]
[1,] -0.02021101 0.08766270 0.073049749
[2,] 0.15877490 0.24157032 0.009806777
[3,] 0.03148085 0.09361557 0.100372380
[4,] -0.03620529 0.09898168 0.044607751
[5,] 0.02847737 -0.30396604 0.574410291
[6,] 0.03105272 0.13842155 0.076071540
In python:在 python 中:
pd.DataFrame(svd_whiten(df.iloc[:,2:])).head(6)
0 1 2 3 4 5 6 7 8
0 0.260670 0.021130 0.093657 0.018437 0.054709 0.017504 -0.020211 0.087663 0.073050
1 -0.171746 -0.235305 0.151222 -0.277382 0.038303 -0.211425 0.158775 0.241570 0.009807
2 -0.106594 0.070424 0.067325 -0.120812 0.094877 -0.017270 0.031481 0.093616 0.100372
3 0.106594 0.136690 0.185234 0.037997 0.065256 -0.098885 -0.036205 0.098982 0.044608
4 -0.129989 -0.052546 -0.146545 -0.156007 0.134556 -0.099305 0.028477 -0.303966 0.574410
5 -0.070105 0.010843 -0.051526 -0.078037 -0.035053 0.434165 0.031053 0.138422 0.076072
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.