Skip to content

docs: add snippet for predicting classifications using a boosted tree model #1156

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

Merged
merged 6 commits into from
Nov 20, 2024
Merged
Changes from 2 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
27 changes: 26 additions & 1 deletion samples/snippets/classification_boosted_tree_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


def test_boosted_tree_model(random_model_id: str) -> None:
# your_model_id = random_model_id
your_model_id = random_model_id
# [START bigquery_dataframes_bqml_boosted_tree_prepare]
import bigframes.pandas as bpd

Expand All @@ -39,4 +39,29 @@ def test_boosted_tree_model(random_model_id: str) -> None:
)
del input_data["functional_weight"]
# [END bigquery_dataframes_bqml_boosted_tree_prepare]
# [START bigquery_dataframes_bqml_boosted_tree_predict]
# Select model you'll use for predictions. `read_gbq_model` loads model
# data from BigQuery, but you could also use the `tree_model` object
# from previous steps.
tree_model = bpd.read_gbq_model(
your_model_id, # For example: "your-project.bqml_tutorial.tree_model"
)

# input_data is defined in an earlier step.
prediction_data = input_data[input_data["dataframe"] == "prediction"]

predictions = tree_model.predict(prediction_data)
predictions.peek()
# Output:
# predicted_income_bracket predicted_income_bracket_probs.label predicted_income_bracket_probs.prob
# <=50K >50K 0.05183430016040802 |
# <50K 0.94816571474075317
# <=50K >50K 0.00365859130397439
# <50K 0.99634140729904175
# <=50K >50K 0.037775970995426178
# <50K 0.96222406625747681
# [END bigquery_dataframes_bqml_boosted_tree_predict]
assert input_data is not None
assert tree_model is not None
assert predictions is not None
assert prediction_data is not None