Dask arrays coordinate many Numpy arrays, arranged into chunks within a grid. They support a large subset of the Numpy API.
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.
from dask.distributed import Client, progress client = Client(processes=False, threads_per_worker=4, n_workers=1, memory_limit='2GB') client
Create Random array¶
This creates a 10000x10000 array of random numbers, represented as many numpy arrays of size 1000x1000 (or smaller if the array cannot be divided evenly). In this case there are 100 (10x10) numpy arrays of size 1000x1000.
import dask.array as da x = da.random.random((10000, 10000), chunks=(1000, 1000)) x
Use NumPy syntax as usual
y = x + x.T z = y[::2, 5000:].mean(axis=1) z
.compute() when you want your result as a NumPy array.
If you started
Client() above then you may want to watch the status page during computation.
array([0.98824614, 1.00167011, 1.0025533 , ..., 1.00388071, 1.00892841, 0.98767643])
Persist data in memory¶
If you have the available RAM for your dataset then you can persist data in memory.
This allows future computations to be much faster.
y = y.persist()
%time y[0, 0].compute()
CPU times: user 2.22 s, sys: 254 ms, total: 2.47 s Wall time: 1.29 s
CPU times: user 354 ms, sys: 16.9 ms, total: 371 ms Wall time: 263 ms