Reproduce Matlab's SVD in python

十年热恋 提交于 2020-07-21 03:13:10

问题


I'm trying to reproduce some large project that was written in Matlab, using python. I managed to reproduce most of the results, but I have a problem specifically with SVD decomposition. (I'm looking only on the last, V, part.)

In Matlab:

[~, ~, V] = svd([4.719, -17.257, -11.5392; -17.2575, 63.9545, 40.5581; -11.5392, 40.5581, 31.3256]);

This gets me the following V:

-0.2216    0.0241   -0.9748
0.8081   -0.5549   -0.1974
0.5457    0.8316   -0.1035

in numpy:

 np.linalg.svd(np.array([[4.71993, -17.2575, -11.539], [-17.257, 63.954, 40.558], [-11.539, 40.558, 31.325]]))[2]

Gets me:

array([[-0.22159139,  0.80814521,  0.54570924],
       [ 0.02407525, -0.55491709,  0.83155722],
       [ 0.97484237,  0.19740401,  0.10350855]])

Which is transposed (as I think is expected between numpy and matlab) but also different on some minus signs.

even using opencv (cv2) or scipy (even with lapack_driver="gesvd") still gets the same result

scipy.linalg.svd(np.array([[4.71993, -17.2575, -11.539], [-17.257, 63.954, 40.558], [-11.539, 40.558, 31.325]]), lapack_driver="gesvd")[2]

I also tried to transpose the input matrix, which didn't change much.

I understand that both answers are correct. But I really need to get the exact same result to be able to reproduce the project.


回答1:


you're not representing the matrix the same in both environments (different numerical precision). If you use the same matrix it should be equivalent (transpose).

>> a=[[4.71993, -17.2575, -11.539]; [-17.257, ...
a =

    4.7199  -17.2575  -11.5390
  -17.2570   63.9540   40.5580
  -11.5390   40.5580   31.3250

>> [~,~,v]=svd(a);
>> v'
ans =

  -0.221591   0.808145   0.545709
   0.024075  -0.554917   0.831557
   0.974842   0.197404   0.103509

with Python

import numpy as np

np.set_printoptions(precision=6)
a=[[4.71993, -17.2575, -11.539], [-17.257, ...
np.linalg.svd(np.array(a))[2]

array([[-0.221591,  0.808145,  0.545709],
       [ 0.024075, -0.554917,  0.831557],
       [ 0.974842,  0.197404,  0.103509]])


来源:https://stackoverflow.com/questions/52105389/reproduce-matlabs-svd-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!