|
| 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 | + |
| 16 | +def test_linear_regression(random_model_id: str) -> None: |
| 17 | + your_model_id = random_model_id |
| 18 | + # [START bigquery_dataframes_bqml_linear_regression] |
| 19 | + from bigframes.ml.linear_model import LinearRegression |
| 20 | + import bigframes.pandas as bpd |
| 21 | + |
| 22 | + # Load data from BigQuery |
| 23 | + bq_df = bpd.read_gbq("bigquery-public-data.ml_datasets.penguins") |
| 24 | + |
| 25 | + # Drop rows with nulls to get training data |
| 26 | + training_data = bq_df.dropna(subset=["body_mass_g"]) |
| 27 | + |
| 28 | + # Specify your feature (or input) columns and the label (or output) column: |
| 29 | + feature_columns = training_data.drop(columns=["body_mass_g"]) |
| 30 | + label_columns = training_data[["body_mass_g"]] |
| 31 | + |
| 32 | + # Create the linear model |
| 33 | + model = LinearRegression() |
| 34 | + model.fit(feature_columns, label_columns) |
| 35 | + model.to_gbq( |
| 36 | + your_model_id, # For example: "bqml_tutorial.penguins_model" |
| 37 | + replace=True, |
| 38 | + ) |
| 39 | + # [END bigquery_dataframes_bqml_linear_regression] |
| 40 | + assert feature_columns is not None |
| 41 | + assert label_columns is not None |
| 42 | + assert model is not None |
0 commit comments