Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Ray Dataset with prediction example #287

Merged
merged 10 commits into from
Aug 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions xgboost_ray/examples/simple_ray_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import numpy as np
import pandas as pd
import ray
from xgboost import DMatrix

from xgboost_ray import RayDMatrix, RayParams, train


def main(cpus_per_actor, num_actors):
np.random.seed(1234)
# Generate dataset
x = np.repeat(range(8), 16).reshape((32, 4))
# Even numbers --> 0, odd numbers --> 1
Expand All @@ -22,16 +24,7 @@ def main(cpus_per_actor, num_actors):
data.columns = [str(c) for c in data.columns]
data["label"] = y

# There was recent API change - the first clause covers the new
# and current Ray master API
if hasattr(ray.data, "from_pandas_refs"):
# Generate Ray dataset from 4 partitions
ray_ds = ray.data.from_pandas(data).repartition(num_actors)
else:
# Split into 4 partitions
partitions = [ray.put(part) for part in np.split(data, num_actors)]
ray_ds = ray.data.from_pandas(partitions)

ray_ds = ray.data.from_pandas(data)
train_set = RayDMatrix(ray_ds, "label")

evals_result = {}
Expand Down Expand Up @@ -62,6 +55,12 @@ def main(cpus_per_actor, num_actors):
bst.save_model(model_path)
print("Final training error: {:.4f}".format(evals_result["train"]["error"][-1]))

# Distributed prediction
scored = ray_ds.drop_columns(["label"]).map_batches(
lambda batch: {"pred": bst.predict(DMatrix(batch))}, batch_format="pandas"
)
print(scored.to_pandas())


if __name__ == "__main__":
parser = argparse.ArgumentParser()
Expand Down
Loading