|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""BigQuery DataFrames code samples for |
| 16 | +https://cloud.google.com/bigquery/docs/logistic-regression-prediction. |
| 17 | +""" |
| 18 | + |
| 19 | + |
| 20 | +def test_logistic_regression_prediction(random_model_id: str) -> None: |
| 21 | + your_model_id = random_model_id |
| 22 | + |
| 23 | + # [START bigquery_dataframes_logistic_regression_prediction_examine] |
| 24 | + import bigframes.pandas as bpd |
| 25 | + |
| 26 | + df = bpd.read_gbq( |
| 27 | + "bigquery-public-data.ml_datasets.census_adult_income", |
| 28 | + columns=( |
| 29 | + "age", |
| 30 | + "workclass", |
| 31 | + "marital_status", |
| 32 | + "education_num", |
| 33 | + "occupation", |
| 34 | + "hours_per_week", |
| 35 | + "income_bracket", |
| 36 | + "functional_weight", |
| 37 | + ), |
| 38 | + max_results=100, |
| 39 | + ) |
| 40 | + df.peek() |
| 41 | + # Output: |
| 42 | + # age workclass marital_status education_num occupation hours_per_week income_bracket functional_weight |
| 43 | + # 47 Local-gov Married-civ-spouse 13 Prof-specialty 40 >50K 198660 |
| 44 | + # 56 Private Never-married 9 Adm-clerical 40 <=50K 85018 |
| 45 | + # 40 Private Married-civ-spouse 12 Tech-support 40 >50K 285787 |
| 46 | + # 34 Self-emp-inc Married-civ-spouse 9 Craft-repair 54 >50K 207668 |
| 47 | + # 23 Private Married-civ-spouse 10 Handlers-cleaners 40 <=50K 40060 |
| 48 | + # [END bigquery_dataframes_logistic_regression_prediction_examine] |
| 49 | + |
| 50 | + # [START bigquery_dataframes_logistic_regression_prediction_prepare] |
| 51 | + import bigframes.pandas as bpd |
| 52 | + |
| 53 | + input_data = bpd.read_gbq( |
| 54 | + "bigquery-public-data.ml_datasets.census_adult_income", |
| 55 | + columns=( |
| 56 | + "age", |
| 57 | + "workclass", |
| 58 | + "marital_status", |
| 59 | + "education_num", |
| 60 | + "occupation", |
| 61 | + "hours_per_week", |
| 62 | + "income_bracket", |
| 63 | + "functional_weight", |
| 64 | + ), |
| 65 | + ) |
| 66 | + input_data["dataframe"] = bpd.Series("training", index=input_data.index,).case_when( |
| 67 | + [ |
| 68 | + (((input_data["functional_weight"] % 10) == 8), "evaluation"), |
| 69 | + (((input_data["functional_weight"] % 10) == 9), "prediction"), |
| 70 | + ] |
| 71 | + ) |
| 72 | + del input_data["functional_weight"] |
| 73 | + # [END bigquery_dataframes_logistic_regression_prediction_prepare] |
| 74 | + |
| 75 | + # [START bigquery_dataframes_logistic_regression_prediction_create_model] |
| 76 | + import bigframes.ml.linear_model |
| 77 | + |
| 78 | + # input_data is defined in an earlier step. |
| 79 | + training_data = input_data[input_data["dataframe"] == "training"] |
| 80 | + X = training_data.drop(columns=["income_bracket", "dataframe"]) |
| 81 | + y = training_data["income_bracket"] |
| 82 | + |
| 83 | + census_model = bigframes.ml.linear_model.LogisticRegression() |
| 84 | + census_model.fit(X, y) |
| 85 | + |
| 86 | + census_model.to_gbq( |
| 87 | + your_model_id, # For example: "your-project.census.census_model" |
| 88 | + replace=True, |
| 89 | + ) |
| 90 | + # [END bigquery_dataframes_logistic_regression_prediction_create_model] |
| 91 | + |
| 92 | + # [START bigquery_dataframes_logistic_regression_prediction_evaluate_model] |
| 93 | + # Select model you'll use for predictions. `read_gbq_model` loads model |
| 94 | + # data from BigQuery, but you could also use the `census_model` object |
| 95 | + # from previous steps. |
| 96 | + census_model = bpd.read_gbq_model( |
| 97 | + your_model_id, # For example: "your-project.census.census_model" |
| 98 | + ) |
| 99 | + |
| 100 | + # input_data is defined in an earlier step. |
| 101 | + evaluation_data = input_data[input_data["dataframe"] == "evaluation"] |
| 102 | + X = evaluation_data.drop(columns=["income_bracket", "dataframe"]) |
| 103 | + y = evaluation_data["income_bracket"] |
| 104 | + |
| 105 | + # The score() method evaluates how the model performs compared to the |
| 106 | + # actual data. Output DataFrame matches that of ML.EVALUATE(). |
| 107 | + score = census_model.score(X, y) |
| 108 | + score.peek() |
| 109 | + # Output: |
| 110 | + # precision recall accuracy f1_score log_loss roc_auc |
| 111 | + # 0 0.685764 0.536685 0.83819 0.602134 0.350417 0.882953 |
| 112 | + # [END bigquery_dataframes_logistic_regression_prediction_evaluate_model] |
| 113 | + |
| 114 | + # [START bigquery_dataframes_logistic_regression_prediction_predict_income_bracket] |
| 115 | + # Select model you'll use for predictions. `read_gbq_model` loads model |
| 116 | + # data from BigQuery, but you could also use the `census_model` object |
| 117 | + # from previous steps. |
| 118 | + census_model = bpd.read_gbq_model( |
| 119 | + your_model_id, # For example: "your-project.census.census_model" |
| 120 | + ) |
| 121 | + |
| 122 | + # input_data is defined in an earlier step. |
| 123 | + prediction_data = input_data[input_data["dataframe"] == "prediction"] |
| 124 | + |
| 125 | + predictions = census_model.predict(prediction_data) |
| 126 | + predictions.peek() |
| 127 | + # Output: |
| 128 | + # predicted_income_bracket predicted_income_bracket_probs age workclass ... occupation hours_per_week income_bracket dataframe |
| 129 | + # 18004 <=50K [{'label': ' >50K', 'prob': 0.0763305999358786... 75 ? ... ? 6 <=50K prediction |
| 130 | + # 18886 <=50K [{'label': ' >50K', 'prob': 0.0448866871906495... 73 ? ... ? 22 >50K prediction |
| 131 | + # 31024 <=50K [{'label': ' >50K', 'prob': 0.0362982319421936... 69 ? ... ? 1 <=50K prediction |
| 132 | + # 31022 <=50K [{'label': ' >50K', 'prob': 0.0787836112058324... 75 ? ... ? 5 <=50K prediction |
| 133 | + # 23295 <=50K [{'label': ' >50K', 'prob': 0.3385373037905673... 78 ? ... ? 32 <=50K prediction |
| 134 | + # [END bigquery_dataframes_logistic_regression_prediction_predict_income_bracket] |
| 135 | + |
| 136 | + # TODO(tswast): Implement ML.EXPLAIN_PREDICT() and corresponding sample. |
| 137 | + # TODO(tswast): Implement ML.GLOBAL_EXPLAIN() and corresponding sample. |
0 commit comments