[Python] TruncatedSVD and randomized_svd from sklearn yield a different matrix U

Discussão em 'Python' iniciado por Stack, Dezembro 9, 2024.

  1. Stack

    Stack Membro Participativo

    I recently ran into the following dilemma when using 2 sklearn implementations:

    Suppose I have an array A of the shape (p, q) that I want to decompose using truncated SVD for let's say the 10 first components. My goal is to get the matrix U from the UΣV' decomposition.

    1. I use randomized_svd from sklearn.utils.extmath:


    from sklearn.utils.extmath import randomized_svd

    U1, _, _ = randomized_svd(A, n_components=10)

    2. I use TruncatedSVD from sklearn.decomposition, following this post answers: Get U, Sigma, V* matrix from Truncated SVD in scikit-learn . Since svd.fit_transform yields UΣ we would have to divide by the Σ given by svd.singular_values_:


    from sklearn.decomposition import TruncatedSVD

    svd = TruncatedSVD(n_components=10)
    transform = svd.fit_transform(A)

    U2 = transform / svd.singular_values_ # Alternatively: svd.transform(A) / svd.singular_values_


    U1 and U2 will have the same shape, i.e., (p, 10), as expected, but when I analyze the results I encounter that they are not the same. Below I attach some plots to visualize the two results and the direct difference between them [StackOverflow doesn't allow me to attach images directly so please use the link].

    Note: In my implementation I have used an A input array of the shape (200, 841).

    U1, U2, Differences

    So, my question is why the two methods are yielding different results, shouldn't them be the same? If one goes to the implementation of Truncated_SVD, one realizes that the default algorithm it uses is radomized_svd, if I even set the number of iterations equally, I still get different results. Which would be the most correct one?

    Continue reading...

Compartilhe esta Página