[scikit-learn] Truncated svd not working for complex matrices

André Melo andre.nascimento.melo at gmail.com
Thu Aug 10 09:55:22 EDT 2017

Hello all,

I'm trying to use the randomized version of scikit-learn's
TruncatedSVD (although I'm actually calling the internal function
randomized_svd to get the actual u, s, v matrices). While it is
working fine for real matrices, for complex matrices I can't get back
the original matrix even though the singular values are exactly

>>> import numpy as np
>>> from sklearn.utils.extmath import randomized_svd
>>> N = 3
>>> a = np.random.rand(N, N)*(1 + 1j)
>>> u1, s1, v1 = np.linalg.svd(a)
>>> u2, s2, v2 = randomized_svd(a, n_components=N, n_iter=7)
>>> np.allclose(s1, s2)
>>> np.allclose(a, u1.dot(np.diag(s1)).dot(v1))
>>> np.allclose(a, u2.dot(np.diag(s2)).dot(v2))

Any idea what could be wrong?

Thank you!

Best regards,
Andre Melo

More information about the scikit-learn mailing list