|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (t |
| 4 | +# you may not use this file except in compliance wi |
| 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 |
| 10 | +# distributed under the License is distributed on a |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, eit |
| 12 | +# See the License for the specific language governi |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +def test_limit_single_timeseries(random_model_id: str) -> None: |
| 17 | + your_model_id = random_model_id |
| 18 | + |
| 19 | + # [START bigquery_dataframes_bqml_limit_forecast_visualize] |
| 20 | + import bigframes.pandas as bpd |
| 21 | + |
| 22 | + df = bpd.read_gbq("bigquery-public-data.new_york.citibike_trips") |
| 23 | + |
| 24 | + features = bpd.DataFrame( |
| 25 | + { |
| 26 | + "num_trips": df.starttime, |
| 27 | + "date": df["starttime"].dt.date, |
| 28 | + } |
| 29 | + ) |
| 30 | + num_trips = features.groupby(["date"]).count() |
| 31 | + |
| 32 | + num_trips.plot.line() |
| 33 | + # [END bigquery_dataframes_bqml_limit_forecast_visualize] |
| 34 | + |
| 35 | + # [START bigquery_dataframes_bqml_limit_forecast_create] |
| 36 | + from bigframes.ml import forecasting |
| 37 | + import bigframes.pandas as bpd |
| 38 | + |
| 39 | + df = bpd.read_gbq("bigquery-public-data.new_york.citibike_trips") |
| 40 | + |
| 41 | + features = bpd.DataFrame( |
| 42 | + { |
| 43 | + "start_station_id": df["start_station_id"], |
| 44 | + "num_trips": df.starttime, |
| 45 | + "date": df["starttime"].dt.date, |
| 46 | + } |
| 47 | + ) |
| 48 | + num_trips = features.groupby(["date", "start_station_id"], as_index=False).count() |
| 49 | + model = forecasting.ARIMAPlus() |
| 50 | + |
| 51 | + X = num_trips[["date"]] |
| 52 | + y = num_trips[["num_trips"]] |
| 53 | + id_col = num_trips[["start_station_id"]] |
| 54 | + |
| 55 | + model.fit(X, y, id_col=id_col) |
| 56 | + |
| 57 | + model.to_gbq( |
| 58 | + your_model_id, # For example: "bqml_tutorial.nyc_citibike_arima_model", |
| 59 | + replace=True, |
| 60 | + ) |
| 61 | + # [END bigquery_dataframes_bqml_limit_forecast_create] |
| 62 | + assert df is not None |
| 63 | + assert features is not None |
| 64 | + assert num_trips is not None |
0 commit comments