Live Notebook

You can run this notebook in a live session Binder or view it on Github.

Singular Value Decomposition

This notebook introduces the da.linalg.svd algorithms for the Singular Value Decomposition

Start Dask Client for Dashboard

Starting the Dask Client is optional. It will provide a dashboard which is useful to gain insight on the computation.

The link to the dashboard will become visible when you create the client below. We recommend having it open on one side of your screen while using your notebook on the other side. This can take some effort to arrange your windows, but seeing them both at the same is very useful when learning.

[1]:
from dask.distributed import Client, progress
client = Client(processes=False, threads_per_worker=4,
                n_workers=1, memory_limit='2GB')
client
[1]:

Client

Cluster

  • Workers: 1
  • Cores: 4
  • Memory: 2.00 GB

Compute SVD of Tall-and-Skinny Matrix

For many applications the provided matrix has many more rows than columns. In this case a specialized algorithm can be used.

[2]:
import dask.array as da

X = da.random.random((200000, 100), chunks=(10000, 100)).persist()
[3]:
import dask

u, s, v = da.linalg.svd(X)
dask.visualize(u, s, v)
[3]:
../_images/machine-learning_svd_5_0.png
[4]:
v.compute()
[4]:
array([[ 0.09987272,  0.10016554,  0.0999333 , ...,  0.10021128,
         0.09994514,  0.10016929],
       [ 0.11983104, -0.01626447,  0.00485723, ...,  0.00832221,
        -0.13885888,  0.11365081],
       [-0.02382224, -0.08345846,  0.14126556, ..., -0.09148828,
        -0.06423535,  0.07369607],
       ...,
       [-0.08503548,  0.08654095, -0.08496403, ...,  0.08814855,
         0.00929058,  0.03290219],
       [ 0.04549893,  0.207729  , -0.03839446, ..., -0.00615796,
        -0.05587221, -0.03749254],
       [-0.02027408,  0.03557052,  0.06404219, ..., -0.01248406,
         0.12334598,  0.11129131]])

Compute SVD of General Non-Skinny Matrix with Approximate algorithm

When there are also many chunks in columns then we use an approximate randomized algorithm to collect only a few of the singular values and vectors.

[5]:
import dask.array as da

X = da.random.random((10000, 10000), chunks=(2000, 2000)).persist()
[6]:
import dask

u, s, v = da.linalg.svd_compressed(X, k=5)
dask.visualize(u, s, v)
[6]:
../_images/machine-learning_svd_9_0.png
[7]:
v.compute()
[7]:
array([[ 0.01001002,  0.00992078,  0.01006724, ...,  0.01004748,
         0.00987234,  0.00990875],
       [ 0.00507526, -0.01032374, -0.00776401, ..., -0.01175529,
         0.01053561,  0.00367785],
       [ 0.00644246,  0.00283535, -0.00772032, ...,  0.00050066,
        -0.02865659,  0.00310412],
       [ 0.00248802, -0.00069016, -0.00289833, ...,  0.01177242,
         0.00614382, -0.01641056],
       [-0.01512942,  0.00245313,  0.00496026, ..., -0.0011845 ,
        -0.00474612, -0.0215209 ]])