|
| 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_multiple_timeseries_forecasting_model(random_model_id): |
| 17 | + your_model_id = random_model_id |
| 18 | + |
| 19 | + # [START bigquery_dataframes_bqml_arima_multiple_step_2_visualize] |
| 20 | + |
| 21 | + import bigframes.pandas as bpd |
| 22 | + |
| 23 | + df = bpd.read_gbq("bigquery-public-data.new_york.citibike_trips") |
| 24 | + |
| 25 | + features = bpd.DataFrame( |
| 26 | + { |
| 27 | + "num_trips": df.starttime, |
| 28 | + "date": df["starttime"].dt.date, |
| 29 | + } |
| 30 | + ) |
| 31 | + date = df["starttime"].dt.date |
| 32 | + df.groupby([date]) |
| 33 | + num_trips = features.groupby(["date"]).count() |
| 34 | + |
| 35 | + # Results from running "print(num_trips)" |
| 36 | + |
| 37 | + # num_trips |
| 38 | + # date |
| 39 | + # 2013-07-01 16650 |
| 40 | + # 2013-07-02 22745 |
| 41 | + # 2013-07-03 21864 |
| 42 | + # 2013-07-04 22326 |
| 43 | + # 2013-07-05 21842 |
| 44 | + # 2013-07-06 20467 |
| 45 | + # 2013-07-07 20477 |
| 46 | + # 2013-07-08 21615 |
| 47 | + # 2013-07-09 26641 |
| 48 | + # 2013-07-10 25732 |
| 49 | + # 2013-07-11 24417 |
| 50 | + # 2013-07-12 19006 |
| 51 | + # 2013-07-13 26119 |
| 52 | + # 2013-07-14 29287 |
| 53 | + # 2013-07-15 28069 |
| 54 | + # 2013-07-16 29842 |
| 55 | + # 2013-07-17 30550 |
| 56 | + # 2013-07-18 28869 |
| 57 | + # 2013-07-19 26591 |
| 58 | + # 2013-07-20 25278 |
| 59 | + # 2013-07-21 30297 |
| 60 | + # 2013-07-22 25979 |
| 61 | + # 2013-07-23 32376 |
| 62 | + # 2013-07-24 35271 |
| 63 | + # 2013-07-25 31084 |
| 64 | + |
| 65 | + num_trips.plot.line( |
| 66 | + # Rotate the x labels so they are more visible. |
| 67 | + rot=45, |
| 68 | + ) |
| 69 | + |
| 70 | + # [END bigquery_dataframes_bqml_arima_multiple_step_2_visualize] |
| 71 | + |
| 72 | + # [START bigquery_dataframes_bqml_arima_multiple_step_3_fit] |
| 73 | + from bigframes.ml import forecasting |
| 74 | + import bigframes.pandas as bpd |
| 75 | + |
| 76 | + df = bpd.read_gbq("bigquery-public-data.new_york.citibike_trips") |
| 77 | + |
| 78 | + features = bpd.DataFrame( |
| 79 | + { |
| 80 | + "num_trips": df.starttime, |
| 81 | + "date": df["starttime"].dt.date, |
| 82 | + } |
| 83 | + ) |
| 84 | + num_trips = features.groupby(["date"], as_index=False).count() |
| 85 | + model = forecasting.ARIMAPlus() |
| 86 | + |
| 87 | + X = num_trips["date"].to_frame() |
| 88 | + y = num_trips["num_trips"].to_frame() |
| 89 | + |
| 90 | + model.fit(X, y) |
| 91 | + # The model.fit() call above created a temporary model. |
| 92 | + # Use the to_gbq() method to write to a permanent location. |
| 93 | + |
| 94 | + model.to_gbq( |
| 95 | + your_model_id, # For example: "bqml_tutorial.nyc_citibike_arima_model", |
| 96 | + replace=True, |
| 97 | + ) |
| 98 | + # [END bigquery_dataframes_bqml_arima_multiple_step_3_fit] |
0 commit comments