This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
docs: add python code sample to multiple timeseries forecasting #531
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5911187
docs: add python code sample to multiple timeseries forecasting
5712534
Merge branch 'main' into Stephanie446
DevStephanie 25e8d63
docs: add python code sample to multiple timeseries forecasting
37341e5
Merge branch 'Stephanie446' of https://github.com/googleapis/python-b…
1d62110
Merge branch 'main' into Stephanie446
DevStephanie b4a9c22
Merge branch 'main' into Stephanie446
DevStephanie cec173a
fix: if setting recurison limit fails, still succeed the import
58d08ae
Merge branch 'main' into Stephanie446
tswast File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
samples/snippets/create_multiple_timeseries_forecasting_model.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| # Copyright 2024 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| def test_multiple_timeseries_forecasting_model(random_model_id): | ||
| your_model_id = random_model_id | ||
|
|
||
| # [START bigquery_dataframes_bqml_arima_multiple_step_2_visualize] | ||
|
|
||
| import bigframes.pandas as bpd | ||
|
|
||
| df = bpd.read_gbq("bigquery-public-data.new_york.citibike_trips") | ||
|
|
||
| features = bpd.DataFrame( | ||
| { | ||
| "num_trips": df.starttime, | ||
| "date": df["starttime"].dt.date, | ||
| } | ||
| ) | ||
| date = df["starttime"].dt.date | ||
| df.groupby([date]) | ||
| num_trips = features.groupby(["date"]).count() | ||
|
|
||
| # Results from running "print(num_trips)" | ||
|
|
||
| # num_trips | ||
| # date | ||
| # 2013-07-01 16650 | ||
| # 2013-07-02 22745 | ||
| # 2013-07-03 21864 | ||
| # 2013-07-04 22326 | ||
| # 2013-07-05 21842 | ||
| # 2013-07-06 20467 | ||
| # 2013-07-07 20477 | ||
| # 2013-07-08 21615 | ||
| # 2013-07-09 26641 | ||
| # 2013-07-10 25732 | ||
| # 2013-07-11 24417 | ||
| # 2013-07-12 19006 | ||
| # 2013-07-13 26119 | ||
| # 2013-07-14 29287 | ||
| # 2013-07-15 28069 | ||
| # 2013-07-16 29842 | ||
| # 2013-07-17 30550 | ||
| # 2013-07-18 28869 | ||
| # 2013-07-19 26591 | ||
| # 2013-07-20 25278 | ||
| # 2013-07-21 30297 | ||
| # 2013-07-22 25979 | ||
| # 2013-07-23 32376 | ||
| # 2013-07-24 35271 | ||
| # 2013-07-25 31084 | ||
|
|
||
| num_trips.plot.line( | ||
| # Rotate the x labels so they are more visible. | ||
| rot=45, | ||
|
DevStephanie marked this conversation as resolved.
|
||
| ) | ||
|
|
||
| # [END bigquery_dataframes_bqml_arima_multiple_step_2_visualize] | ||
|
|
||
| # [START bigquery_dataframes_bqml_arima_multiple_step_3_fit] | ||
| from bigframes.ml import forecasting | ||
| import bigframes.pandas as bpd | ||
|
|
||
| df = bpd.read_gbq("bigquery-public-data.new_york.citibike_trips") | ||
|
|
||
|
tswast marked this conversation as resolved.
|
||
| features = bpd.DataFrame( | ||
| { | ||
| "num_trips": df.starttime, | ||
| "date": df["starttime"].dt.date, | ||
| } | ||
| ) | ||
| num_trips = features.groupby(["date"], as_index=False).count() | ||
| model = forecasting.ARIMAPlus() | ||
|
|
||
| X = num_trips["date"].to_frame() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to_frame() not needed.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, will update that.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like we might need .to_frame() because without it, I see |
||
| y = num_trips["num_trips"].to_frame() | ||
|
|
||
| model.fit(X, y) | ||
| # The model.fit() call above created a temporary model. | ||
| # Use the to_gbq() method to write to a permanent location. | ||
|
|
||
| model.to_gbq( | ||
| your_model_id, # For example: "bqml_tutorial.nyc_citibike_arima_model", | ||
| replace=True, | ||
| ) | ||
| # [END bigquery_dataframes_bqml_arima_multiple_step_3_fit] | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.