TimesFM 단변량 모델로 여러 시계열 예측


이 튜토리얼에서는 BigQuery ML의 내장 TimesFM 단변량 모델과 함께 AI.FORECAST 함수를 사용하여 특정 열의 과거 값을 기반으로 해당 열의 미래 값을 예측하는 방법을 설명합니다.

이 튜토리얼에서는 공개 bigquery-public-data.san_francisco_bikeshare.bikeshare_trips 테이블의 데이터를 사용합니다.

목표

이 튜토리얼에서는 내장된 TimesFM 모델과 함께 AI.FORECAST 함수를 사용하여 자전거 공유 이동을 예측하는 방법을 안내합니다. 처음 두 섹션에서는 단일 시계열의 결과를 예측하고 시각화하는 방법을 설명합니다. 세 번째 섹션에서는 여러 시계열을 예측하는 방법을 설명합니다.

비용

이 튜토리얼에서는 비용이 청구될 수 있는 다음과 같은 Google Cloud구성요소를 사용합니다.

  • BigQuery
  • BigQuery ML

BigQuery 비용에 대한 자세한 내용은 BigQuery 가격 책정 페이지를 참조하세요.

BigQuery ML 비용에 대한 자세한 내용은 BigQuery ML 가격 책정을 참조하세요.

시작하기 전에

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Google Cloud project.

  6. BigQuery는 새 프로젝트에서 자동으로 사용 설정됩니다. 기존 프로젝트에서 BigQuery를 활성화하려면 다음을 수행합니다.

    Enable the BigQuery API.

    Enable the API

단일 자전거 공유 이동 시계열 예측

AI.FORECAST 함수를 사용하여 미래 시계열 값을 예측합니다.

다음 쿼리는 이전 4개월의 과거 데이터를 기반으로 다음 달 (약 720시간)의 시간당 구독자 자전거 공유 이동 수를 예측합니다. confidence_level 인수는 쿼리가 95% 신뢰 수준의 예측 구간을 생성함을 나타냅니다.

TimesFM 모델로 데이터를 예측하려면 다음 단계를 따르세요.

  1. Google Cloud 콘솔에서 BigQuery 페이지로 이동합니다.

    BigQuery로 이동

  2. 쿼리 편집기에 다음 쿼리를 붙여넣고 실행을 클릭합니다.

    SELECT *
    FROM
      AI.FORECAST(
        (
          SELECT TIMESTAMP_TRUNC(start_date, HOUR) as trip_hour, COUNT(*) as num_trips
    FROM `bigquery-public-data.san_francisco_bikeshare.bikeshare_trips`
    WHERE subscriber_type = 'Subscriber' AND start_date >= TIMESTAMP('2018-01-01')
    GROUP BY TIMESTAMP_TRUNC(start_date, HOUR)
        ),
        horizon => 720,
        confidence_level => 0.95,
        timestamp_col => 'trip_hour',
        data_col => 'num_trips');

    결과는 다음과 유사합니다.

    +-------------------------+-------------------+------------------+---------------------------------+---------------------------------+--------------------+
    | forecast_timestamp      | forecast_value    | confidence_level | prediction_interval_lower_bound | prediction_interval_upper_bound | ai_forecast_status |
    +-------------------------+-------------------+------------------+---------------------------------+---------------------------------+--------------------+
    | 2018-05-01 00:00:00 UTC | 26.3045959...     |            0.95  | 21.7088378...                   | 30.9003540...                   |                    |
    +-------------------------+-------------------+------------------+---------------------------------+---------------------------------+--------------------+
    | 2018-05-01 01:00:00 UTC | 34.0890502...     |            0.95  | 2.47682913...                   | 65.7012714...                   |                    |
    +-------------------------+-------------------+------------------+---------------------------------+---------------------------------+--------------------+
    | 2018-05-01 02:00:00 UTC | 24.2154693...     |            0.95  | 2.87621605...                   | 45.5547226...                   |                    |
    +-------------------------+-------------------+------------------+---------------------------------+---------------------------------+--------------------+
    | ...                     | ...               |  ...             | ...                             |  ...                            |                    |
    +-------------------------+-------------------+------------------+---------------------------------+---------------------------------+--------------------+
    

예측 데이터를 입력 데이터와 비교

AI.FORECAST 함수 출력을 함수 입력 데이터의 하위 집합과 함께 차트에 표시하여 비교합니다.

함수 출력을 차트로 표시하려면 다음 단계를 따르세요.

  1. Google Cloud 콘솔에서 BigQuery 페이지로 이동합니다.

    BigQuery로 이동

  2. 쿼리 편집기에 다음 쿼리를 붙여넣고 실행을 클릭합니다.

    WITH historical AS (
    SELECT TIMESTAMP_TRUNC(start_date, HOUR) as trip_hour, COUNT(*) as num_trips
    FROM `bigquery-public-data.san_francisco_bikeshare.bikeshare_trips`
    WHERE subscriber_type = 'Subscriber' AND start_date >= TIMESTAMP('2018-01-01')
    GROUP BY TIMESTAMP_TRUNC(start_date, HOUR)
    ORDER BY TIMESTAMP_TRUNC(start_date, HOUR)
    )
    SELECT * 
    FROM 
    (
    (SELECT
        trip_hour as date,
        num_trips AS historical_value,
        NULL as forecast_value,
        'historical' as type,
        NULL as prediction_interval_low,
        NULL as prediction_interval_upper_bound
    FROM
        historical
    ORDER BY historical.trip_hour DESC
    LIMIT 400)
    UNION ALL
    (SELECT forecast_timestamp AS date,
            NULL as historical_value,
            forecast_value as forecast_value, 
            'forecast' as type, 
            prediction_interval_lower_bound, 
            prediction_interval_upper_bound
    FROM
        AI.FORECAST(
        (
        SELECT * FROM historical
        ),
        horizon => 720,
        confidence_level => 0.99,
        timestamp_col => 'trip_hour',
        data_col => 'num_trips')))
    ORDER BY date asc;
  3. 쿼리 실행이 완료되면 쿼리 결과 창에서 차트 탭을 클릭합니다. 결과 차트는 다음과 유사합니다.

    입력 데이터의 100개 시점과 AI.FORECAST 함수 출력 데이터를 그래프로 표시하여 유사성을 평가합니다.

    입력 데이터와 예측 데이터에서 자전거 공유 사용량이 비슷한 것을 확인할 수 있습니다. 또한 예측 시점이 미래로 갈수록 예측 간격 하한 및 상한이 증가하는 것을 볼 수 있습니다.

여러 자전거 공유 이동 시계열 예측

다음 쿼리는 이전 4개월의 과거 데이터를 기반으로 다음 달 (약 720시간)의 구독자 유형별 및 시간당 자전거 공유 이동 수를 예측합니다. confidence_level 인수는 쿼리가 95% 신뢰 수준의 예측 구간을 생성함을 나타냅니다.

TimesFM 모델로 데이터를 예측하려면 다음 단계를 따르세요.

  1. Google Cloud 콘솔에서 BigQuery 페이지로 이동합니다.

    BigQuery로 이동

  2. 쿼리 편집기에 다음 쿼리를 붙여넣고 실행을 클릭합니다.

    SELECT *
    FROM
      AI.FORECAST(
        (
          SELECT TIMESTAMP_TRUNC(start_date, HOUR) as trip_hour, subscriber_type, COUNT(*) as num_trips
          FROM `bigquery-public-data.san_francisco_bikeshare.bikeshare_trips`
          WHERE start_date >= TIMESTAMP('2018-01-01')
          GROUP BY TIMESTAMP_TRUNC(start_date, HOUR), subscriber_type
        ),
        horizon => 720,
        confidence_level => 0.95,
        timestamp_col => 'trip_hour',
        data_col => 'num_trips',
        id_cols => ['subscriber_type']);

    결과는 다음과 유사합니다.

    +---------------------+--------------------------+------------------+------------------+---------------------------------+---------------------------------+--------------------+
    | subscriber_type     | forecast_timestamp       | forecast_value   | confidence_level | prediction_interval_lower_bound | prediction_interval_upper_bound | ai_forecast_status |
    +---------------------+--------------------------+------------------+------------------+---------------------------------+---------------------------------+--------------------+
    | Subscriber          | 2018-05-01 00:00:00 UTC  | 26.3045959...    |            0.95  | 21.7088378...                   | 30.9003540...                   |                    |
    +---------------------+--------------------------+------------------+------------------+---------------------------------+---------------------------------+--------------------+
    | Subscriber          |  2018-05-01 01:00:00 UTC | 34.0890502...    |            0.95  | 2.47682913...                   | 65.7012714...                   |                    |
    +---------------------+-------------------+------------------+-------------------------+---------------------------------+---------------------------------+--------------------+
    | Subscriber          |  2018-05-01 02:00:00 UTC | 24.2154693...    |            0.95  | 2.87621605...                   | 45.5547226...                   |                    |
    +---------------------+--------------------------+------------------+------------------+---------------------------------+---------------------------------+--------------------+
    | ...                 | ...                      |  ...             | ...              | ...                             |  ...                            |                    |
    +---------------------+--------------------------+------------------+------------------+---------------------------------+---------------------------------+--------------------+
    

삭제

이 튜토리얼에서 사용된 리소스 비용이 Google Cloud 계정에 청구되지 않도록 하려면 리소스가 포함된 프로젝트를 삭제하거나 프로젝트를 유지하고 개별 리소스를 삭제하세요.

프로젝트 삭제

프로젝트를 삭제하는 방법은 다음과 같습니다.

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

다음 단계