การตรวจจับความผิดปกติหลายตัวแปรด้วยป่าแยกตัว

บทความนี้แสดงวิธีใช้ SynapseML บน Apache Spark สําหรับการตรวจจับความผิดปกติหลายตัวแปร การตรวจจับความผิดปกติแบบหลายตัวแปรตรวจจับความผิดปกติในหลายตัวแปรหรืออนุกรมเวลา โดยคํานึงถึงความสัมพันธ์และการพึ่งพากันระหว่างตัวแปรต่าง ๆ ในสถานการณ์นี้ คุณใช้ SynapseML เพื่อฝึกโมเดลป่าแยกสําหรับการตรวจจับความผิดปกติหลายตัวแปร จากนั้นใช้แบบจําลองที่ฝึกมาเพื่ออนุมานความผิดปกติหลายตัวแปรภายในชุดข้อมูลที่มีการวัดสังเคราะห์จากเซ็นเซอร์ IoT สามตัว

หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับแบบจําลองป่าแยกตัว โปรดดูบทความต้นฉบับโดย Liu และคณะ

ข้อกำหนดเบื้องต้น

  1. รับการสมัครใช้งาน Microsoft Fabric หรือลงทะเบียนเพื่อทดลองใช้ Microsoft Fabric ฟรี
  2. แนบสมุดบันทึกของคุณเข้ากับเลคเฮ้าส์ ทางด้านซ้าย เลือก เพิ่ม เพื่อเพิ่มเลคเฮ้าส์ที่มีอยู่แล้ว หรือสร้างเลคเฮ้าส์
  3. SynapseML ถูกติดตั้งล่วงหน้าใน Fabric PySpark runtimes (แนะนําให้ใช้ Runtime 1.3 หรือใหม่กว่า) หากต้องการใช้เวอร์ชันเฉพาะ ดูที่ ติดตั้งเวอร์ชันอื่นของ SynapseML บน Fabric

การนําเข้าไลบรารี

from pyspark.sql import functions as F
from pyspark.ml.feature import VectorAssembler
from pyspark.sql.types import DoubleType
from pyspark.ml import Pipeline

from synapse.ml.isolationforest import IsolationForest
from pyspark.sql import SparkSession

# Bootstrap Spark Session
spark = SparkSession.builder.getOrCreate()

ข้อมูลอินพุต

# Table inputs
timestampColumn = "timestamp"  # str: the name of the timestamp column in the table
inputCols = [
    "sensor_1",
    "sensor_2",
    "sensor_3",
]  # list(str): the names of the input variables

# Training Start time, and number of days to use for training:
trainingStartTime = (
    "2022-02-24T06:00:00Z"  # datetime: datetime for when to start the training
)
trainingEndTime = (
    "2022-03-08T23:55:00Z"  # datetime: datetime for when to end the training
)
inferenceStartTime = (
    "2022-03-09T09:30:00Z"  # datetime: datetime for when to start the inference
)
inferenceEndTime = (
    "2022-03-20T23:55:00Z"  # datetime: datetime for when to end the inference
)

# Isolation Forest parameters
contamination = 0.021
num_estimators = 100
max_samples = 256
max_features = 1.0

อ่านข้อมูล

df = (
    spark.read.format("csv")
    .option("header", "true")
    .load(
        "wasbs://publicwasb@mmlspark.blob.core.windows.net/generated_sample_mvad_data.csv"
    )
)

แปลงคอลัมน์ไปยังชนิดข้อมูลที่เหมาะสม

df = (
    df.orderBy(timestampColumn)
    .withColumn("timestamp", F.date_format(timestampColumn, "yyyy-MM-dd'T'HH:mm:ss'Z'"))
    .withColumn("sensor_1", F.col("sensor_1").cast(DoubleType()))
    .withColumn("sensor_2", F.col("sensor_2").cast(DoubleType()))
    .withColumn("sensor_3", F.col("sensor_3").cast(DoubleType()))
    .drop("_c5")  # drop the extra unlabeled column present in the source CSV
)

display(df)

การเตรียมข้อมูลการฝึกอบรม

# filter to data with timestamps within the training window
df_train = df.filter(
    (F.col(timestampColumn) >= trainingStartTime)
    & (F.col(timestampColumn) <= trainingEndTime)
)
display(df_train)

การเตรียมข้อมูลการทดสอบ

# filter to data with timestamps within the inference window
df_test = df.filter(
    (F.col(timestampColumn) >= inferenceStartTime)
    & (F.col(timestampColumn) <= inferenceEndTime)
)
display(df_test)

โมเดล Train Isolation Forest

isolationForest = (
    IsolationForest()
    .setNumEstimators(num_estimators)
    .setBootstrap(False)
    .setMaxSamples(max_samples)
    .setMaxFeatures(max_features)
    .setFeaturesCol("features")
    .setPredictionCol("predictedLabel")
    .setScoreCol("outlierScore")
    .setContamination(contamination)
    .setContaminationError(0.01 * contamination)
    .setRandomSeed(1)
)

ต่อไป สร้าง ML pipeline เพื่อฝึกโมเดล Isolation Forest

สําหรับการฝึกโมเดลและทําการอนุมานในสมุดบันทึกเดียวกัน วัตถุโมเดลก็เพียงพอแล้ว หากต้องการเก็บรักษาและนําโมเดลกลับมาใช้ใหม่ข้ามเซสชัน ให้ลงทะเบียนกับ MLflow ใน Microsoft Fabric

va = VectorAssembler(inputCols=inputCols, outputCol="features")
pipeline = Pipeline(stages=[va, isolationForest])
model = pipeline.fit(df_train)

ทําการอนุมาน

นําโมเดลที่ฝึกมาใช้กับข้อมูลทดสอบ:

df_test_pred = model.transform(df_test)
display(df_test_pred)

เครื่องตรวจจับความผิดปกติที่สร้างไว้ล่วงหน้า

สำคัญ

Microsoft กําลังจะยกเลิกบริการ Azure AI Anomaly Detector ในวันที่ 1 ตุลาคม 2026 ตั้งแต่วันที่ 20 กันยายน 2023 คุณไม่สามารถสร้างทรัพยากรใหม่ได้ สําหรับทางเลือกที่รองรับ ดูที่ การตรวจจับความผิดปกติใน Microsoft Fabric Real-Time Intelligence

Azure AI Anomaly Detector

  • สถานะความผิดปกติของจุดล่าสุด: สร้างโมเดลโดยใช้จุดก่อนหน้าและตรวจสอบว่าจุดล่าสุดนั้นผิดปกติหรือไม่ ดูที่ที่เก็บ GitHub ของ SynapseML สําหรับข้อมูลอ้างอิง API ปัจจุบัน
  • ค้นหาความผิดปกติ: สร้างโมเดลโดยใช้ชุดข้อมูลทั้งหมดและค้นหาความผิดปกติในชุดข้อมูลนั้น ดูที่ที่เก็บ GitHub ของ SynapseML สําหรับข้อมูลอ้างอิง API ปัจจุบัน