[dask][doc] Wrap the example in main guard. (#6979)
This commit is contained in:
parent
81bdfb835d
commit
cf06a266a8
@ -44,33 +44,30 @@ on a dask cluster:
|
|||||||
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__":
|
||||||
|
cluster = dask.distributed.LocalCluster()
|
||||||
client = dask.distributed.Client(cluster)
|
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,
|
dtrain,
|
||||||
num_boost_round=4, 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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user