Posted at

Python、R間でSVDの特異値が同じか確認

More than 1 year has passed since last update.


PythonのSVD

import numpy as np

X = np.array(
[[0.23619964, 0.93715782, 0.50648748, 0.36245609, 0.06628567],
[0.95440595, 0.02250788, 0.47989255, 0.59502410, 0.58785659],
[0.73162857, 0.04590415, 0.04265483, 0.16959635, 0.64327691],
[0.6970129, 0.9410706, 0.1723309, 0.2452992, 0.8771952]])
U, S, V = np.linalg.svd(X, full_matrices=False)
S

array([ 2.21555332, 1.03585253, 0.62783174, 0.06628274])


RのSVD

X <- rbind(

c(0.23619964, 0.93715782, 0.50648748, 0.36245609, 0.06628567),
c(0.95440595, 0.02250788, 0.47989255, 0.59502410, 0.58785659),
c(0.73162857, 0.04590415, 0.04265483, 0.16959635, 0.64327691),
c(0.6970129, 0.9410706, 0.1723309, 0.2452992, 0.8771952)
)
out <- svd(X)
out$d

[1] 2.21555332 1.03585253 0.62783174 0.06628274

全く同じです。前者はscikit-learnのPCAの中で、後者はprcompの中で使われていて、共にLAPACKのラッパーらしいです。