Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7c37c7d

Browse files
rey-esptswast
andauthoredFeb 26, 2025
docs: add snippet for explaining the linear regression model prediction (#1427)
* add snippet for explaining the linear regression model prediction * Update samples/snippets/linear_regression_tutorial_test.py * Update samples/snippets/linear_regression_tutorial_test.py --------- Co-authored-by: Tim Sweña (Swast) <swast@google.com>
1 parent 962b152 commit 7c37c7d

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
 

‎samples/snippets/linear_regression_tutorial_test.py

+15
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,23 @@ def test_linear_regression(random_model_id: str) -> None:
7878
# 332 4740.7907 Gentoo penguin (Pygoscelis papua) Biscoe 46.2 14.4 214.0 4650.0 <NA>
7979
# 160 4731.310452 Gentoo penguin (Pygoscelis papua) Biscoe 44.5 14.3 216.0 4100.0 <NA>
8080
# [END bigquery_dataframes_bqml_linear_predict]
81+
# [START bigquery_dataframes_bqml_linear_predict_explain]
82+
# Use 'predict_explain' function to understand why the model is generating these prediction results.
83+
# 'predict_explain'is an extended version of the 'predict' function that not only outputs prediction results, but also outputs additional columns to explain the prediction results.
84+
# Using the trained model and utilizing data specific to Biscoe Island, explain the predictions of the top 3 features
85+
explained = model.predict_explain(biscoe_data, top_k_features=3)
86+
87+
# Expected results:
88+
# predicted_body_mass_g top_feature_attributions baseline_prediction_value prediction_value approximation_error species island culmen_length_mm culmen_depth_mm flipper_length_mm body_mass_g sex
89+
# 0 5413.510134 [{'feature': 'island', 'attribution': 7348.877... -5320.222128 5413.510134 0.0 Gentoo penguin (Pygoscelis papua) Biscoe 45.2 16.4 223.0 5950.0 MALE
90+
# 1 4768.351092 [{'feature': 'island', 'attribution': 7348.877... -5320.222128 4768.351092 0.0 Gentoo penguin (Pygoscelis papua) Biscoe 46.5 14.5 213.0 4400.0 FEMALE
91+
# 2 3235.896372 [{'feature': 'island', 'attribution': 7348.877... -5320.222128 3235.896372 0.0 Adelie Penguin (Pygoscelis adeliae) Biscoe 37.7 16.0 183.0 3075.0 FEMALE
92+
# 3 5349.603734 [{'feature': 'island', 'attribution': 7348.877... -5320.222128 5349.603734 0.0 Gentoo penguin (Pygoscelis papua) Biscoe 46.4 15.6 221.0 5000.0 MALE
93+
# 4 4637.165037 [{'feature': 'island', 'attribution': 7348.877... -5320.222128 4637.165037 0.0 Gentoo penguin (Pygoscelis papua) Biscoe 46.1 13.2 211.0 4500.0 FEMALE
94+
# [END bigquery_dataframes_bqml_linear_predict_explain]
8195
assert feature_columns is not None
8296
assert label_columns is not None
8397
assert model is not None
8498
assert score is not None
8599
assert result is not None
100+
assert explained is not None

0 commit comments

Comments
 (0)
Failed to load comments.