다변수 시계열 예측 모델로 이상 감지 수행
이 튜토리얼에서는 다음 작업을 처리하는 방법을 보여줍니다.
ARIMA_PLUS_XREG시계열 예측 모델을 만듭니다.- 모델에 대해
ML.DETECT_ANOMALIES함수를 실행하여 시계열 데이터에서 이상을 감지합니다.
이 튜토리얼에서는 여러 미국 도시에서 수집된 일일 PM 2.5, 기온, 풍속 정보가 포함된 공개 epa_historical_air_quality 데이터 세트에서 다음 테이블을 사용합니다.
epa_historical_air_quality.pm25_nonfrm_daily_summaryepa_historical_air_quality.wind_daily_summaryepa_historical_air_quality.temperature_daily_summary
필수 권한
데이터 세트를 만들려면
bigquery.datasets.createIAM 권한이 필요합니다.모델을 만들려면 다음 권한이 필요합니다.
bigquery.jobs.createbigquery.models.createbigquery.models.getDatabigquery.models.updateData
추론을 실행하려면 다음 권한이 필요합니다.
bigquery.models.getDatabigquery.jobs.create
BigQuery에서 IAM 역할 및 권한에 대한 자세한 내용은 IAM 소개를 참조하세요.
비용
이 문서에서는 비용이 청구될 수 있는 구성요소를 사용합니다 Google Cloud.
- BigQuery: You incur costs for the data you process in BigQuery.
프로젝트 사용량을 기준으로 예상 비용을 산출하려면 가격 계산기를 사용하세요.
자세한 내용은 BigQuery 가격을 참조하세요.
시작하기 전에
- 계정에 로그인합니다. Google Cloud 를 처음 사용하는 경우 계정을 만들고 Google 제품의 실제 성능을 평가해 보세요. Google Cloud신규 고객에게는 워크로드를 실행, 테스트, 배포하는 데 사용할 수 있는 $300의 무료 크레딧이 제공됩니다.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the BigQuery API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the BigQuery API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
데이터 세트 만들기
ML 모델을 저장할 BigQuery 데이터 세트를 만듭니다.
콘솔
Google Cloud 콘솔에서 BigQuery 페이지로 이동합니다.
탐색기 창에서 프로젝트 이름을 클릭합니다.
작업 보기 > 데이터 세트 만들기를 클릭합니다.
데이터 세트 만들기 페이지에서 다음을 수행합니다.
데이터 세트 ID에
bqml_tutorial을 입력합니다.위치 유형에 대해 멀티 리전을 선택한 다음 US를 선택합니다.
나머지 기본 설정은 그대로 두고 데이터 세트 만들기 를 클릭합니다.
bq
새 데이터 세트를 만들려면
bq mk --dataset 명령어를 사용합니다.
데이터 위치가
US로 설정된bqml_tutorial데이터 세트를 만듭니다.bq mk --dataset \ --location=US \ --description "BigQuery ML tutorial dataset." \ bqml_tutorial
데이터 세트가 생성되었는지 확인합니다.
bq ls
API
데이터 세트 리소스가 정의된 datasets.insert 메서드를 호출합니다.
{ "datasetReference": { "datasetId": "bqml_tutorial" } }
학습 데이터 준비
PM2.5, 온도, 풍속 데이터는 별도의 테이블에 있습니다.
이러한 공개 테이블의 데이터를 결합하여 학습 데이터의 bqml_tutorial.seattle_air_quality_daily 테이블을 만듭니다.
bqml_tutorial.seattle_air_quality_daily에는 다음 열이 포함됩니다.
date: 관측 날짜PM2.5: 일일 평균 PM2.5 값wind_speed: 일일 평균 풍속temperature: 일일 최고 기온
새 테이블에는 2009년 8월 11일부터 2022년 1월 31일까지의 일일 데이터가 포함됩니다.
BigQuery 페이지로 이동합니다.
SQL 편집자 창에서 다음 SQL 문을 실행합니다.
CREATE TABLE `bqml_tutorial.seattle_air_quality_daily` AS WITH pm25_daily AS ( SELECT avg(arithmetic_mean) AS pm25, date_local AS date FROM `bigquery-public-data.epa_historical_air_quality.pm25_nonfrm_daily_summary` WHERE city_name = 'Seattle' AND parameter_name = 'Acceptable PM2.5 AQI & Speciation Mass' GROUP BY date_local ), wind_speed_daily AS ( SELECT avg(arithmetic_mean) AS wind_speed, date_local AS date FROM `bigquery-public-data.epa_historical_air_quality.wind_daily_summary` WHERE city_name = 'Seattle' AND parameter_name = 'Wind Speed - Resultant' GROUP BY date_local ), temperature_daily AS ( SELECT avg(first_max_value) AS temperature, date_local AS date FROM `bigquery-public-data.epa_historical_air_quality.temperature_daily_summary` WHERE city_name = 'Seattle' AND parameter_name = 'Outdoor Temperature' GROUP BY date_local ) SELECT pm25_daily.date AS date, pm25, wind_speed, temperature FROM pm25_daily JOIN wind_speed_daily USING (date) JOIN temperature_daily USING (date)
모델 만들기
bqml_tutorial.seattle_air_quality_daily의 데이터를 학습 데이터로 사용하여 다변수 시계열 모델을 만듭니다.
BigQuery 페이지로 이동합니다.
SQL 편집자 창에서 다음 SQL 문을 실행합니다.
CREATE OR REPLACE MODEL `bqml_tutorial.arimax_model` OPTIONS ( model_type = 'ARIMA_PLUS_XREG', auto_arima=TRUE, time_series_data_col = 'temperature', time_series_timestamp_col = 'date' ) AS SELECT * FROM `bqml_tutorial.seattle_air_quality_daily` WHERE date < "2023-02-01";
쿼리가 완료되는 데 몇 초가 걸리며, 완료되면
arimax_model모델이bqml_tutorial데이터 세트에 표시되고 탐색기 창에서 액세스할 수 있습니다.이 쿼리에서는
CREATE MODEL문을 사용하여 모델을 만들므로 쿼리 결과가 없습니다.
이전 데이터에 이상 감지 수행
모델을 학습시키는 데 사용한 이전 데이터에서 이상 감지를 실행합니다.
BigQuery 페이지로 이동합니다.
SQL 편집자 창에서 다음 SQL 문을 실행합니다.
SELECT * FROM ML.DETECT_ANOMALIES ( MODEL `bqml_tutorial.arimax_model`, STRUCT(0.6 AS anomaly_prob_threshold) ) ORDER BY date ASC;
결과는 다음과 유사합니다.
+-------------------------+-------------+------------+--------------------+--------------------+---------------------+ | date | temperature | is_anomaly | lower_bound | upper_bound | anomaly_probability | +--------------------------------------------------------------------------------------------------------------------+ | 2009-08-11 00:00:00 UTC | 70.1 | false | 67.647370742988727 | 72.552629257011262 | 0 | +--------------------------------------------------------------------------------------------------------------------+ | 2009-08-12 00:00:00 UTC | 73.4 | false | 71.7035428351283 | 76.608801349150838 | 0.20478819992561115 | +--------------------------------------------------------------------------------------------------------------------+ | 2009-08-13 00:00:00 UTC | 64.6 | true | 67.740408724826068 | 72.6456672388486 | 0.945588334903206 | +-------------------------+-------------+------------+--------------------+--------------------+---------------------+
새 데이터에 이상 감지 수행
생성되는 새 데이터에서 이상 감지를 실행합니다.
BigQuery 페이지로 이동합니다.
SQL 편집자 창에서 다음 SQL 문을 실행합니다.
SELECT * FROM ML.DETECT_ANOMALIES ( MODEL `bqml_tutorial.arimax_model`, STRUCT(0.6 AS anomaly_prob_threshold), ( SELECT * FROM UNNEST( [ STRUCT<date TIMESTAMP, pm25 FLOAT64, wind_speed FLOAT64, temperature FLOAT64> ('2023-02-01 00:00:00 UTC', 8.8166665, 1.6525, 44.0), ('2023-02-02 00:00:00 UTC', 11.8354165, 1.558333, 40.5), ('2023-02-03 00:00:00 UTC', 10.1395835, 1.6895835, 46.5), ('2023-02-04 00:00:00 UTC', 11.439583500000001, 2.0854165, 45.0), ('2023-02-05 00:00:00 UTC', 9.7208335, 1.7083335, 46.0), ('2023-02-06 00:00:00 UTC', 13.3020835, 2.23125, 43.5), ('2023-02-07 00:00:00 UTC', 5.7229165, 2.377083, 47.5), ('2023-02-08 00:00:00 UTC', 7.6291665, 2.24375, 44.5), ('2023-02-09 00:00:00 UTC', 8.5208335, 2.2541665, 40.5), ('2023-02-10 00:00:00 UTC', 9.9086955, 7.333335, 39.5) ] ) ) );
결과는 다음과 유사합니다.
+-------------------------+-------------+------------+--------------------+--------------------+---------------------+------------+------------+ | date | temperature | is_anomaly | lower_bound | upper_bound | anomaly_probability | pm25 | wind_speed | +----------------------------------------------------------------------------------------------------------------------------------------------+ | 2023-02-01 00:00:00 UTC | 44.0 | true | 36.89918003713138 | 41.8044385511539 | 0.88975675709801583 | 8.8166665 | 1.6525 | +----------------------------------------------------------------------------------------------------------------------------------------------+ | 2023-02-02 00:00:00 UTC | 40.5 | false | 34.439946284051572 | 40.672021330796483 | 0.57358239699845348 | 11.8354165 | 1.558333 | +--------------------------------------------------------------------------------------------------------------------+-------------------------+ | 2023-02-03 00:00:00 UTC | 46.5 | true | 33.615139992931191 | 40.501364463964549 | 0.97902867696346974 | 10.1395835 | 1.6895835 | +-------------------------+-------------+------------+--------------------+--------------------+---------------------+-------------------------+
삭제
- 콘솔에서 리소스 관리 페이지로 이동합니다. Google Cloud
- 프로젝트 목록에서 삭제할 프로젝트를 선택하고 삭제를 클릭합니다.
- 대화상자에서 프로젝트 ID를 입력한 후 종료를 클릭하여 프로젝트를 삭제합니다.