[dask][doc] Wrap the example in main guard. (#6979)

This commit is contained in:
Jiaming Yuan 2021-05-25 08:24:47 +08:00 committed by GitHub
parent 81bdfb835d
commit cf06a266a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,37 +40,34 @@ on a dask cluster:
.. code-block:: python .. code-block:: python
import xgboost as xgb import xgboost as xgb
import dask.array as da import dask.array as da
import dask.distributed import dask.distributed
cluster = dask.distributed.LocalCluster(n_workers=4, threads_per_worker=1) if __name__ == "__main__":
client = dask.distributed.Client(cluster) cluster = dask.distributed.LocalCluster()
client = dask.distributed.Client(cluster)
# X and y must be Dask dataframes or arrays # X and y must be Dask dataframes or arrays
num_obs = 1e5 num_obs = 1e5
num_features = 20 num_features = 20
X = da.random.random( X = da.random.random(size=(num_obs, num_features), chunks=(1000, num_features))
size=(num_obs, num_features), y = da.random.random(size=(num_obs, 1), chunks=(1000, 1))
chunks=(1000, num_features)
)
y = da.random.random(
size=(num_obs, 1),
chunks=(1000, 1)
)
dtrain = xgb.dask.DaskDMatrix(client, X, y) dtrain = xgb.dask.DaskDMatrix(client, X, y)
output = xgb.dask.train(client, output = xgb.dask.train(
{'verbosity': 2, client,
'tree_method': 'hist', {"verbosity": 2, "tree_method": "hist", "objective": "reg:squarederror"},
'objective': 'reg:squarederror' dtrain,
}, num_boost_round=4,
dtrain, evals=[(dtrain, "train")],
num_boost_round=4, evals=[(dtrain, 'train')]) )
Here we first create a cluster in single-node mode with ``dask.distributed.LocalCluster``, then Here we first create a cluster in single-node mode with ``dask.distributed.LocalCluster``, then
connect a ``dask.distributed.Client`` to this cluster, setting up an environment for later computation. connect a ``dask.distributed.Client`` to this cluster, setting up an environment for later
computation. Notice that the cluster construction is guared by ``__name__ == "__main__"``, which is
necessary otherwise there might be obscure errors.
We then create a ``DaskDMatrix`` object and pass it to ``train``, along with some other parameters, We then create a ``DaskDMatrix`` object and pass it to ``train``, along with some other parameters,
much like XGBoost's normal, non-dask interface. Unlike that interface, ``data`` and ``label`` must much like XGBoost's normal, non-dask interface. Unlike that interface, ``data`` and ``label`` must