@@ -22,18 +22,48 @@ def test_create_single_timeseries() -> None:
22
22
# Read and visualize the time series you want to forecast.
23
23
df = bpd .read_gbq ("bigquery-public-data.google_analytics_sample.ga_sessions_*" )
24
24
parsed_date = bpd .to_datetime (df .date , format = "%Y%m%d" , utc = True )
25
+ parsed_date .name = "parsed_date"
25
26
visits = df ["totals" ].struct .field ("visits" )
27
+ visits .name = "total_visits"
26
28
total_visits = visits .groupby (parsed_date ).sum ()
27
29
28
30
# Expected output: total_visits.head()
29
- # date
31
+ # parsed_date
30
32
# 2016-08-01 00:00:00+00:00 1711
31
33
# 2016-08-02 00:00:00+00:00 2140
32
34
# 2016-08-03 00:00:00+00:00 2890
33
35
# 2016-08-04 00:00:00+00:00 3161
34
36
# 2016-08-05 00:00:00+00:00 2702
35
- # Name: visits , dtype: Int64
37
+ # Name: total_visits , dtype: Int64
36
38
37
39
total_visits .plot .line ()
38
40
39
41
# [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