Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SciPy SVD vs. Numpy SVD

Both SciPy and Numpy have built in functions for singular value decomposition (SVD). The commands are basically scipy.linalg.svd and numpy.linalg.svd. What is the difference between these two? Is any of them better than the other one?

like image 642
A.M. Avatar asked Sep 14 '15 16:09

A.M.


People also ask

What is SVD in Numpy?

Singular Value Decomposition means when arr is a 2D array, it is factorized as u and vh, where u and vh are 2D unitary arrays and s is a 1D array of a's singular values. numpy. linalg. svd() function is used to compute the factor of an array by Singular Value Decomposition.

What does Numpy SVD return?

The numpy. linalg. svd() function returns a Singular Value Decomposition.

How do I get SVD in Python?

The SVD can be calculated by calling the svd() function. The function takes a matrix and returns the U, Sigma and V^T elements. The Sigma diagonal matrix is returned as a vector of singular values. The V matrix is returned in a transposed form, e.g. V.T.

What is the method used for singular value decomposition in Scipy Linalg?

SciPy contains two methods to compute the singular value decomposition (SVD) of a matrix: scipy. linalg. svd and scipy. sparse.


2 Answers

From the FAQ page, it says scipy.linalg submodule provides a more complete wrapper for the Fortran LAPACK library whereas numpy.linalg tries to be able to build independent of LAPACK.

I did some benchmarks for the different implementation of the svd functions and found scipy.linalg.svd is faster than the numpy counterpart:

However, jax wrapped numpy, aka jax.numpy.linalg.svd is even faster:

Full notebook for the benchmarks are available here.

like image 184
Zichen Wang Avatar answered Sep 17 '22 13:09

Zichen Wang


Apart from the error checking, the actual work seems to be done within lapack both with numpy and scipy.

Without having done any benchmarking, I guess the performance should be identical.

like image 25
Fred Schoen Avatar answered Sep 21 '22 13:09

Fred Schoen