Skip to content

Commit 108f4a9

Browse files
rey-esptswast
andauthored
docs: add snippet for Linear Regression tutorial Predict Outcomes section (#1101)
* docs: add python translation for predicting outcomes * add '6' to previous snippet comment * fix data to predict * add expected results * replace model with dataframe * update dataframe to drop nulls in body mass column * update df * Update samples/snippets/linear_regression_tutorial_test.py * complete snippet --------- Co-authored-by: Tim Sweña (Swast) <[email protected]>
1 parent 2d3fd4b commit 108f4a9

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

samples/snippets/linear_regression_tutorial_test.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,34 @@ def test_linear_regression(random_model_id: str) -> None:
5252
# Expected output results:
5353
# index mean_absolute_error mean_squared_error mean_squared_log_error median_absolute_error r2_score explained_variance
5454
# 0 227.012237 81838.159892 0.00507 173.080816 0.872377 0.872377
55-
# 1 rows x columns
55+
# 1 rows x 6 columns
5656
# [END bigquery_dataframes_bqml_linear_evaluate]
57+
# [START bigquery_dataframes_bqml_linear_predict]
58+
# Select the model you'll use for predictions. `read_gbq_model` loads
59+
# model data from BigQuery, but you could also use the `model` object
60+
# object from previous steps.
61+
model = bpd.read_gbq_model(
62+
your_model_id,
63+
# For example: "bqml_tutorial.penguins_model",
64+
)
65+
66+
# Load data from BigQuery
67+
bq_df = bpd.read_gbq("bigquery-public-data.ml_datasets.penguins")
68+
69+
# Use 'contains' function to filter by island containing the string
70+
# "Biscoe".
71+
biscoe_data = bq_df.loc[bq_df["island"].str.contains("Biscoe")]
72+
73+
result = model.predict(biscoe_data)
74+
75+
# Expected output results:
76+
# predicted_body_mass_g species island culmen_length_mm culmen_depth_mm body_mass_g flipper_length_mm sex
77+
# 23 4681.782896 Gentoo penguin (Pygoscelis papua) Biscoe <NA> <NA> <NA> <NA> <NA>
78+
# 332 4740.7907 Gentoo penguin (Pygoscelis papua) Biscoe 46.2 14.4 214.0 4650.0 <NA>
79+
# 160 4731.310452 Gentoo penguin (Pygoscelis papua) Biscoe 44.5 14.3 216.0 4100.0 <NA>
80+
# [END bigquery_dataframes_bqml_linear_predict]
5781
assert feature_columns is not None
5882
assert label_columns is not None
5983
assert model is not None
6084
assert score is not None
85+
assert result is not None

0 commit comments

Comments
 (0)