Skip to content

Commit 4cb62fd

Browse files
rey-esptswast
andauthored
docs: move and edit existing linear-regression tutorial snippet (#991)
* add new alias '__version__' * remove accidental changes * correct assignment * docs: edit existing code snippet for linear regression and add to /snippets * return type * remove filtering * input data updated * remove score and predict Co-authored-by: Tim Sweña (Swast) <[email protected]> * delete blank line Co-authored-by: Tim Sweña (Swast) <[email protected]> * remove test data Co-authored-by: Tim Sweña (Swast) <[email protected]> * remove test checks Co-authored-by: Tim Sweña (Swast) <[email protected]> * remove test check Co-authored-by: Tim Sweña (Swast) <[email protected]> * add variable input to allow random string to be passed and saved as the model Co-authored-by: Tim Sweña (Swast) <[email protected]> * add line that writes the dataframe to big query * update comment with the correct tutorial model reference * update comment with the correct tutorial model reference * remove extra line --------- Co-authored-by: Tim Sweña (Swast) <[email protected]>
1 parent 208a984 commit 4cb62fd

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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

Comments
 (0)