{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Singular Value Decomposition\n", "\n", "This notebook introduces the `da.linalg.svd` algorithms for the Singular Value Decomposition" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Start Dask Client for Dashboard\n", "\n", "Starting the Dask Client is optional. It will provide a dashboard which \n", "is useful to gain insight on the computation. \n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from dask.distributed import Client, progress\n", "client = Client(processes=False, threads_per_worker=4,\n", " n_workers=1, memory_limit='2GB')\n", "client" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compute SVD of Tall-and-Skinny Matrix\n", "\n", "For many applications the provided matrix has many more rows than columns. In this case a specialized algorithm can be used." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import dask.array as da\n", "\n", "X = da.random.random((200000, 100), chunks=(10000, 100)).persist()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import dask\n", "\n", "u, s, v = da.linalg.svd(X)\n", "dask.visualize(u, s, v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v.compute()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compute SVD of General Non-Skinny Matrix with Approximate algorithm\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import dask.array as da\n", "\n", "X = da.random.random((10000, 10000), chunks=(2000, 2000)).persist()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import dask\n", "\n", "u, s, v = da.linalg.svd_compressed(X, k=5)\n", "dask.visualize(u, s, v)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "v.compute()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" } }, "nbformat": 4, "nbformat_minor": 4 }