【 ワン・クラス・サポートベクターマシン (one-class-SVM)で簡単な異常検知】

Поделиться
HTML-код
  • Опубликовано: 16 ноя 2024

Комментарии • 1

  • @EastWood19802
    @EastWood19802  3 месяца назад

    ◎Rのプログラム
    # 1. ライブラリの読み込み
    library(kernlab)
    # 2. 乱数のシードを設定
    set.seed(123)
    # 3. ランダムデータを生成
    x = rnorm(1000)
    y = rnorm(1000)
    data = data.frame(type = 1, x, y)
    # 4. One-Class SVMモデルを作成
    one_class_SVM = ksvm(type ~ ., data = data,
    type = "one-svc", kernel = "rbfdot",
    kpar = list(sigma = 0.1), cross = 10, nu = 0.1)
    # 5. 正常値と外れ値を予測
    pre = predict(one_class_SVM)
    # 6. 予測結果の変換
    c.pre = ifelse(pre == TRUE, 1, 2)
    # 7. 予測結果の結合
    data.result = cbind(data, c.pre)
    # 8. 結果のプロット:正常値は青、外れ値は赤、外れ値は×で表示
    plot(data.result[, 2:3],
    pch = ifelse(data.result$c.pre == 1, 21, 4),
    bg = ifelse(data.result$c.pre == 1, "blue", "red"),
    col = ifelse(data.result$c.pre == 1, "blue", "red"),
    cex = ifelse(data.result$c.pre == 1, 1, 1.5),
    lwd = ifelse(data.result$c.pre == 1, 1, 2),
    xlab = "X 値", ylab = "Y 値",
    main = "One-Class SVMによる異常検知",
    col.main = "blue", col.lab = "blue", col.axis = "blue")

    legend("topright", legend = c("正常値", "外れ値"),
    col = c("blue", "red"),
    pch = c(21, 4),
    pt.bg = c("blue", "red"),
    pt.cex = c(1, 1.5),
    lwd = c(1, 2))