ביצוע זיהוי אנומליות באמצעות מודל חיזוי של סדרת זמן רב-משתנית
במדריך הזה מוסבר איך לבצע את הפעולות הבאות:
- יצירת
ARIMA_PLUS_XREGמודל חיזוי של סדרת זמנים. - מריצים את הפונקציה
ML.DETECT_ANOMALIESעל המודל כדי לזהות חריגות בנתונים של סדרות הזמנים.
במדריך הזה נעשה שימוש בטבלאות הבאות מתוך מערך הנתונים הציבורי epa_historical_air_quality, שמכיל נתונים יומיים של חלקיקים נשימים (PM 2.5), טמפרטורה ומהירות רוח שנאספו מכמה ערים בארה"ב:
epa_historical_air_quality.pm25_nonfrm_daily_summaryepa_historical_air_quality.wind_daily_summaryepa_historical_air_quality.temperature_daily_summary
ההרשאות הנדרשות
כדי ליצור את מערך הנתונים, אתם צריכים את ההרשאה
bigquery.datasets.createב-IAM.כדי ליצור את המודל, צריך את ההרשאות הבאות:
bigquery.jobs.createbigquery.models.createbigquery.models.getDatabigquery.models.updateData
כדי להריץ הסקה, אתם צריכים את ההרשאות הבאות:
bigquery.models.getDatabigquery.jobs.create
במאמר מבוא ל-IAM יש מידע נוסף על תפקידים והרשאות ב-IAM ב-BigQuery.
עלויות
במסמך הזה משתמשים ברכיבים הבאים של Google Cloud, והשימוש בהם כרוך בתשלום:
- BigQuery: You incur costs for the data you process in BigQuery.
כדי ליצור הערכת עלויות בהתאם לשימוש החזוי, אתם יכולים להשתמש במחשבון התמחור.
כאן תוכלו לקרוא מידע נוסף על התמחור ב-BigQuery.
לפני שמתחילים
- נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- 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
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). 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
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.
יצירת מערך נתונים
כדי ליצור מערך נתונים ב-BigQuery, בוחרים באחת מהאפשרויות הבאות:
המסוף
במסוף Google Cloud , עוברים לדף BigQuery.
בחלונית הימנית, לוחצים על כלי הניתוחים:

אם החלונית הימנית לא מוצגת, לוחצים על הרחבת החלונית הימנית כדי לפתוח אותה.
בסייר, מרחיבים את הפרויקט ואז לוחצים על מערכי נתונים.
בדף Datasets (מערכי נתונים), לוחצים על Create dataset (יצירת מערך נתונים).
בחלונית Create dataset:
בשדה Dataset ID (מזהה קבוצת נתונים), מזינים
bqml_tutorial.בקטע Data location, בוחרים באפשרות US.
משאירים את שאר הגדרות ברירת המחדל כמו שהן.
לוחצים על יצירת מערך נתונים.
BQ
כדי ליצור מערך נתונים חדש, משתמשים בפקודה bq mk --dataset.
יוצרים מערך נתונים בשם
bqml_tutorialעם מיקום הנתונים שמוגדר ל-US: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: הטמפרטורה הכי גבוהה בכל יום
הטבלה החדשה כוללת נתונים יומיים מ-11 באוגוסט 2009 עד 31 בינואר 2022.
עוברים לדף 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ואפשר לגשת אליו בחלונית Explorer.מכיוון שהשאילתה משתמשת בהצהרת
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 , נכנסים לדף Manage resources.
- ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
- כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.