זיהוי אנומליות באמצעות מודל חיזוי של סדרות עתיות עם כמה משתנים

במדריך הזה מוסבר איך לבצע את הפעולות הבאות:

במדריך הזה משתמשים בטבלאות הבאות ממערך הנתונים הציבורי epa_historical_air_quality, שמכיל מידע יומי על PM 2.5, טמפרטורה ומהירות הרוח שנאסף מכמה ערים בארה"ב:

ההרשאות הנדרשות

  • כדי ליצור את קבוצת הנתונים, אתם צריכים את ההרשאה bigquery.datasets.create ב-IAM.

  • כדי ליצור את המודל, צריך את ההרשאות הבאות:

    • bigquery.jobs.create
    • bigquery.models.create
    • bigquery.models.getData
    • bigquery.models.updateData
  • כדי להריץ הסקה, צריך את ההרשאות הבאות:

    • bigquery.models.getData
    • bigquery.jobs.create

במאמר מבוא ל-IAM תוכלו לקרוא מידע נוסף על התפקידים וההרשאות של IAM ב-BigQuery.

עלויות

במסמך הזה משתמשים ברכיבים הבאים של Google Cloud, והשימוש בהם כרוך בתשלום:

  • BigQuery: You incur costs for the data you process in BigQuery.

בעזרת מחשבון העלויות אתם יכולים להעריך את העלויות בהתאם לשימוש הצפוי.

משתמשים חדשים של Google Cloud ? יכול להיות שאתם זכאים לתקופת ניסיון בחינם.

כאן תוכלו לקרוא מידע נוסף על התמחור ב-BigQuery.

לפני שמתחילים

  1. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Verify that billing is enabled for your Google Cloud project.

  3. Enable the BigQuery API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    Enable the API

יצירת קבוצת נתונים

כדי ליצור מערך נתונים ב-BigQuery, בוחרים אחת מהאפשרויות הבאות:

המסוף

  1. נכנסים לדף BigQuery במסוף Google Cloud .

    כניסה ל-BigQuery

  2. בחלונית הימנית, לוחצים על Explorer:

    כפתור מודגש לחלונית Explorer.

    אם החלונית הימנית לא מוצגת, לוחצים על Expand left pane כדי לפתוח אותה.

  3. ב-Explorer, מרחיבים את הפרויקט ולוחצים על Datasets.

  4. בדף Datasets, לוחצים על Create dataset.

  5. בחלונית Create dataset:

    • בשדה Dataset ID, מזינים bqml_tutorial.

    • בקטע Data location (מיקום הנתונים), בוחרים באפשרות US (ארה"ב).

    משאירים את שאר הגדרות ברירת המחדל כמו שהן.

  6. לוחצים על Create dataset.

BQ

כדי ליצור קבוצת נתונים חדשה, משתמשים בפקודה bq mk --dataset.

  1. יוצרים מערך נתונים בשם bqml_tutorial ומגדירים את מיקום הנתונים ל-US:

    bq mk --dataset \
      --location=US \
      --description "BigQuery ML tutorial dataset." \
      bqml_tutorial
  2. מוודאים שמערך הנתונים נוצר:

    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.

  1. נכנסים לדף BigQuery.

    כניסה ל-BigQuery

  2. בחלונית של עורך ה-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 כנתוני אימון.

  1. נכנסים לדף BigQuery.

    כניסה ל-BigQuery

  2. בחלונית של עורך ה-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 כדי ליצור מודל, אין תוצאות לשאילתה.

זיהוי אנומליות בנתונים היסטוריים

מריצים זיהוי אנומליות על הנתונים ההיסטוריים ששימשו לאימון המודל.

  1. נכנסים לדף BigQuery.

    כניסה ל-BigQuery

  2. בחלונית של עורך ה-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   |
    +-------------------------+-------------+------------+--------------------+--------------------+---------------------+
    

זיהוי אנומליות בנתונים חדשים

מפעילים זיהוי אנומליות על הנתונים החדשים שנוצרו.

  1. נכנסים לדף BigQuery.

    כניסה ל-BigQuery

  2. בחלונית של עורך ה-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  |
    +-------------------------+-------------+------------+--------------------+--------------------+---------------------+-------------------------+
    

הסרת המשאבים

  1. במסוף Google Cloud , נכנסים לדף Manage resources.

    כניסה לדף Manage resources

  2. ברשימת הפרויקטים, בוחרים את הפרויקט שרוצים למחוק ולוחצים על Delete.
  3. כדי למחוק את הפרויקט, כותבים את מזהה הפרויקט בתיבת הדו-שיח ולוחצים על Shut down.