series_uv_change_points_fl()

この関数series_uv_change_points_fl()は、Azure Cognitive Services の一部である Univariate Anomaly Detection API を呼び出すことによって、時系列の変更ポイントを検索するユーザー定義関数 (UDF) です。 この関数は、数値動的配列、変化点検出しきい値、および安定傾向ウィンドウの最小サイズとして、限られた時系列セットを受け入れます。 各時系列が必要な JSON 形式に変換され、Anomaly Detector サービス エンドポイントに投稿されます。 サービス応答には、変化点の動的配列、それぞれの信頼度、検出された季節性が含まれます。

注意

ネイティブ関数series_decompose_anomalies()の使用を検討してください。これはよりスケーラブルで、より高速に実行できます。

前提条件

構文

T | invoke series_uv_change_points_fl(y_series [,score_threshold [,trend_window [,tsid]]])

構文規則について詳しく知る。

パラメーター

名前 必須 説明
y_series string ✔️ 異常検出される系列の値を含む入力テーブル列の名前。
score_threshold real 変更ポイントを宣言する最小信頼度を指定する値。 信頼度がしきい値を超える各ポイントは、変更ポイントとして定義されます。 既定値: 0.9
trend_window 整数 (integer) 傾向の変化を堅牢に計算するための最小ウィンドウ サイズを指定する値。 既定値: 5
tsid string 時系列 ID を含む入力テーブル列の名前。 1 つの時系列を分析する場合は省略できます。

関数の定義

関数を定義するには、次のようにコードをクエリ定義関数として埋め込むか、データベースに格納された関数として作成します。

次の let ステートメントを使用して関数を定義します。 権限は必要ありません。 次の関数定義で、uri と YOUR-KEY ヘッダーの の をOcp-Apim-Subscription-Key、Anomaly Detectorリソース名とキーに置き換えますYOUR-AD-RESOURCE-NAME

重要

let ステートメントを単独で実行することはできません。 その後に 表形式の式ステートメントを指定する必要があります。 の動作例 series_uv_change_points_fl()を実行するには、「 」を参照してください。

let series_uv_change_points_fl=(tbl:(*), y_series:string, score_threshold:real=0.9, trend_window:int=5, tsid:string='_tsid')
{
    let uri = 'https://YOUR-AD-RESOURCE-NAME.cognitiveservices.azure.com/anomalydetector/v1.0/timeseries/changepoint/detect';
    let headers=dynamic({'Ocp-Apim-Subscription-Key': h'YOUR-KEY'});
    let kwargs = bag_pack('y_series', y_series, 'score_threshold', score_threshold, 'trend_window', trend_window);
    let code = ```if 1:
        import json
        y_series = kargs["y_series"]
        score_threshold = kargs["score_threshold"]
        trend_window = kargs["trend_window"]
        json_str = []
        for i in range(len(df)):
            row = df.iloc[i, :]
            ts = [{'value':row[y_series][j]} for j in range(len(row[y_series]))]
            json_data = {'series': ts, "threshold":score_threshold, "stableTrendWindow": trend_window}     # auto-detect period, or we can force 'period': 84
            json_str = json_str + [json.dumps(json_data)]
        result = df
        result['json_str'] = json_str
    ```;
    tbl
    | evaluate python(typeof(*, json_str:string), code, kwargs)
    | extend _tsid = column_ifexists(tsid, 1)
    | partition by _tsid (
       project json_str
       | evaluate http_request_post(uri, headers, dynamic(null))
        | project period=ResponseBody.period, change_point=series_add(0, ResponseBody.isChangePoint), confidence=ResponseBody.confidenceScores
        | extend _tsid=toscalar(_tsid)
       )
};
// Write your query to use the function here.

次の例では、 invoke 演算子 を使用して関数を実行します。

クエリ定義関数を使用するには、埋め込み関数定義の後で呼び出します。

let series_uv_change_points_fl=(tbl:(*), y_series:string, score_threshold:real=0.9, trend_window:int=5, tsid:string='_tsid')
{
    let uri = 'https://YOUR-AD-RESOURCE-NAME.cognitiveservices.azure.com/anomalydetector/v1.0/timeseries/changepoint/detect';
    let headers=dynamic({'Ocp-Apim-Subscription-Key': h'YOUR-KEY'});
    let kwargs = bag_pack('y_series', y_series, 'score_threshold', score_threshold, 'trend_window', trend_window);
    let code = ```if 1:
        import json
        y_series = kargs["y_series"]
        score_threshold = kargs["score_threshold"]
        trend_window = kargs["trend_window"]
        json_str = []
        for i in range(len(df)):
            row = df.iloc[i, :]
            ts = [{'value':row[y_series][j]} for j in range(len(row[y_series]))]
            json_data = {'series': ts, "threshold":score_threshold, "stableTrendWindow": trend_window}     # auto-detect period, or we can force 'period': 84
            json_str = json_str + [json.dumps(json_data)]
        result = df
        result['json_str'] = json_str
    ```;
    tbl
    | evaluate python(typeof(*, json_str:string), code, kwargs)
    | extend _tsid = column_ifexists(tsid, 1)
    | partition by _tsid (
       project json_str
       | evaluate http_request_post(uri, headers, dynamic(null))
        | project period=ResponseBody.period, change_point=series_add(0, ResponseBody.isChangePoint), confidence=ResponseBody.confidenceScores
        | extend _tsid=toscalar(_tsid)
       )
};
let ts = range x from 1 to 300 step 1
| extend y=iff(x between (100 .. 110) or x between (200 .. 220), 20, 5)
| extend ts=datetime(2021-01-01)+x*1d
| extend y=y+4*rand()
| summarize ts=make_list(ts), y=make_list(y)
| extend sid=1;
ts
| invoke series_uv_change_points_fl('y', 0.8, 10, 'sid')
| join ts on $left._tsid == $right.sid
| project-away _tsid
| project-reorder y, *      //  just to visualize the anomalies on top of y series
| render anomalychart with(xcolumn=ts, ycolumns=y, confidence, anomalycolumns=change_point)

出力

次のグラフは、時系列の変更ポイントを示しています。

時系列の変更ポイントを示すグラフ。

この機能はサポートされていません。