Skip to content

Commit 20f3190

Browse files
rey-esptswast
andauthored
docs: add python snippet for "Create the time series model" section of the Forecast a single time series with a univariate model tutorial (#1227)
* merge main * merge main * comments added - draft * fix test --------- Co-authored-by: Tim Sweña (Swast) <[email protected]>
1 parent 4d854fd commit 20f3190

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

samples/snippets/create_single_timeseries_forecasting_model_test.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,48 @@ def test_create_single_timeseries() -> None:
2222
# Read and visualize the time series you want to forecast.
2323
df = bpd.read_gbq("bigquery-public-data.google_analytics_sample.ga_sessions_*")
2424
parsed_date = bpd.to_datetime(df.date, format="%Y%m%d", utc=True)
25+
parsed_date.name = "parsed_date"
2526
visits = df["totals"].struct.field("visits")
27+
visits.name = "total_visits"
2628
total_visits = visits.groupby(parsed_date).sum()
2729

2830
# Expected output: total_visits.head()
29-
# date
31+
# parsed_date
3032
# 2016-08-01 00:00:00+00:00 1711
3133
# 2016-08-02 00:00:00+00:00 2140
3234
# 2016-08-03 00:00:00+00:00 2890
3335
# 2016-08-04 00:00:00+00:00 3161
3436
# 2016-08-05 00:00:00+00:00 2702
35-
# Name: visits, dtype: Int64
37+
# Name: total_visits, dtype: Int64
3638

3739
total_visits.plot.line()
3840

3941
# [END bigquery_dataframes_single_timeseries_forecasting_model_tutorial]
42+
43+
# [START bigquery_dataframes_single_timeseries_forecasting_model_tutorial_create]
44+
from bigframes.ml import forecasting
45+
import bigframes.pandas as bpd
46+
47+
# Create a time series model to forecast total site visits:
48+
# The auto_arima option defaults to True, so the auto.ARIMA algorithm automatically
49+
# tunes the hyperparameters in the model.
50+
# The data_frequency option defaults to 'auto_frequency so the training
51+
# process automatically infers the data frequency of the input time series.
52+
# The decompose_time_series option defaults to True, so that information about
53+
# the time series data is returned when you evaluate the model in the next step.
54+
model = forecasting.ARIMAPlus()
55+
model.auto_arima = True
56+
model.data_frequency = "auto_frequency"
57+
model.decompose_time_series = True
58+
59+
# Use the data loaded in the previous step to fit the model
60+
training_data = total_visits.to_frame().reset_index(drop=False)
61+
62+
X = training_data[["parsed_date"]]
63+
y = training_data[["total_visits"]]
64+
65+
model.fit(X, y)
66+
# [END bigquery_dataframes_single_timeseries_forecasting_model_tutorial_create]
67+
assert model is not None
68+
assert parsed_date is not None
69+
assert total_visits is not None

0 commit comments

Comments
 (0)