2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Azure Stream Analyticsでビルトインされた異常検知モデルを動かしてみる

Last updated at Posted at 2019-06-19

はじめに

Stream Analyticsの異常検知がなかなかよさそうなので試してみた

Stream Analyticsの異常検知とは

閾値を設けずに今までの傾向からみて急に上がったり下がったり(Spike / Dip)、長期的な傾きが急に変わったり(ChangePoint)を検出してくれる機能
裏側では機械学習モデルが働いている

SpikeAndDip

image.png

ChangePoint

レベルの変化
image.png

傾向の変化
image.png

参照

詳しくはこちら
https://docs.microsoft.com/ja-jp/azure/stream-analytics/stream-analytics-machine-learning-anomaly-detection

関数の説明も
https://docs.microsoft.com/en-us/stream-analytics-query/anomalydetection-spikeanddip-azure-stream-analytics
https://docs.microsoft.com/en-us/stream-analytics-query/anomalydetection-changepoint-azure-stream-analytics

今回動かしてみるもの

Device Simulater
https://github.com/Azure/azure-stream-analytics/tree/master/Samples/DeviceSimulator

動かしてみる

gitからコードをダウンロードしてVisual Studioで開いてみる
DeviceSimulator.csproj を開くよ
image.png

依存関係に問題があるんで「依存関係」を右クリック→「NuGetパッケージの管理」から同名のものを一つずつインストールします
image.png

更新とかすればOK
image.png

3つのパッケージを更新したらDevice Simulatorが使えます
すごいぞ~扱いやすいぞ~
image.png

IoT Hub Configを埋めてあげたら準備OK

Stream Analytics Jobのクエリはこう書いた
公式のドキュメント内のSQLをくっつけてます。
ChangePointは学習期間を短めにしている

WITH AnomalyDetectionStep AS
(
    SELECT
        EVENTENQUEUEDUTCTIME AS time,
        CAST(temperature AS float) AS temp,
        AnomalyDetection_SpikeAndDip(CAST(temperature AS float), 95, 120, 'spikesanddips')
            OVER(LIMIT DURATION(second, 120)) AS SpikeAndDipScores
    FROM anomalyiot
),AnomalyDetectionStep2 AS
(
    SELECT
        EVENTENQUEUEDUTCTIME AS time,
        CAST(temperature AS float) AS temp,
        AnomalyDetection_ChangePoint(CAST(temperature AS float), 80, 120) 
       -- OVER(LIMIT DURATION(minute, 20)) AS ChangePointScores
        OVER(LIMIT DURATION(second, 120)) AS ChangePointScores
    FROM anomalyiot
)
SELECT
    time,
    temp,
    CAST(GetRecordPropertyValue(SpikeAndDipScores, 'Score') AS float) AS
    SpikeAndDipScore,
    CAST(GetRecordPropertyValue(SpikeAndDipScores, 'IsAnomaly') AS bigint) AS
    IsSpikeAndDipAnomaly
INTO anomalyspikedippbi
FROM AnomalyDetectionStep;
SELECT
    time,
    temp,
    CAST(GetRecordPropertyValue(ChangePointScores, 'Score') AS float) AS
    ChangePointScore,
    CAST(GetRecordPropertyValue(ChangePointScores, 'IsAnomaly') AS bigint) AS
    IsChangePointAnomaly
INTO anomalychangepbi
FROM AnomalyDetectionStep2

Power BIで出力を確認してみる
temp=気温
出力値は下記のようになってます。
SpikeAndDipScore=スコア
IsSpikeAndDipAnomaly=検知フラグ

ChangePointScore=スコア
IsChangePointAnomaly=検知フラグ

まずはSpike。感知が早い
Scoreの読み方はわからないけど、0ほど異常?
とりあえず使えそう。
image.png

対してChangePoint。傾向を見ているので、変わった瞬間ではない
公式でもChangePointは長期的な変化傾向の異常を検知すると書いてある。

image.png

それにしてもScoreは一体・・・

イベントがどれだけ異常であるかを示す、計算されたMartingaleスコア(float)。このスコアは、異常値とともに指数関数的に増加します。

とあるけどなんか逆な気がする。これうまく動いているのかな?動画のおじさんはSpikeAndDipしかやってなかったからChangePointは正解がわかりませんがなんとなく検知できてるのでよし!

アルゴリズムはそれぞれ
SpikeAndDip 適応カーネル密度推定アルゴリズム
ChangePoint 交換可能性マルチンゲールアルゴリズム
を使っているらしいけどぶっちゃけわけわかめ。

このあたりは割り切ってツールとして使うべきなんでしょう。
独自のモデル、アルゴリズムを使いたいなら推論環境作って連携すればいいと思うけど、それをやらなくてもこのように異常検知の仕組みができるのは素敵だと思う

おしまい

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?