リビジョン adb5da50
| learn/python/ai/book-introductory_anomaly_detection/one-class_svm/inout_approach_ocsvm.py | ||
|---|---|---|
|
""" 書籍「Pythonではじめる異常検知入門」 5-4のOne-Class SVM抜粋 """
|
||
|
import matplotlib.pyplot as plt
|
||
|
import matplotlib_fontja
|
||
|
import numpy as np
|
||
|
import pandas as pd
|
||
|
from pandas import DataFrame
|
||
|
from sklearn.svm import OneClassSVM
|
||
|
|
||
|
|
||
|
### データの精鋭
|
||
|
# 生成するデータ数
|
||
|
N_DATA: int = 200
|
||
|
|
||
|
def generate_data(num: int) -> DataFrame:
|
||
|
""" 引数で指定した次元の乱数を2つ生成、DataFrameのx列とy列に設定し返却する """
|
||
|
df = pd.DataFrame()
|
||
|
np.random.seed(seed=0) # 乱数生成列を固定
|
||
|
df["x"] = 10.0 * np.random.randn(num) + 50 # num次元の乱数(平均50, 分散10)を生成
|
||
|
df["y"] = 10.0 * np.random.randn(num) + 50
|
||
|
return df
|
||
|
|
||
|
def plot_ocsvm(ax, data, predict, xx, yy, Z, label):
|
||
|
"""指定したグラフ上に、指定したデータの散布図、境界、ラベルをプロットする"""
|
||
|
ax.set_title("Outlier detection")
|
||
|
ax.contour(xx, yy, Z, levels=[0], linewidths=2, colors="black", linestyles="--")
|
||
|
ax.contourf(xx, yy, Z, levels=[0, Z.max()], colors="lightgreen")
|
||
|
ax.scatter(data["x"].iloc[np.where(predict>0)], data["y"].iloc[np.where(predict>0)], c="black", edgecolors="black", s=50)
|
||
|
ax.scatter(data["x"].iloc[np.where(predict<0)], data["y"].iloc[np.where(predict<0)], c="white", edgecolors="black", s=50)
|
||
|
ax.set_xlabel(r"$x_1$")
|
||
|
ax.set_ylabel(r"$x_2$")
|
||
|
ax.set_xlim(20, 80)
|
||
|
ax.set_ylim(20, 80)
|
||
|
ax.annotate(label, xy=(22,75), size=16, color="black")
|
||
|
|
||
|
data: DataFrame = generate_data(N_DATA)
|
||
|
# 可視化用の点データ
|
||
|
xx, yy = np.meshgrid(np.linspace(0, 100, 1000), np.linspace(0, 100, 1000))
|
||
|
|
||
|
### 可視化
|
||
|
# ハイパーパラメータ(ν、γ)を変えてプロット
|
||
|
fig = plt.figure(figsize=(16, 12))
|
||
|
fig.subplots_adjust(hspace=0.4, wspace=0.2)
|
||
|
ax1 = fig.add_subplot(331)
|
||
|
ax2 = fig.add_subplot(332)
|
||
|
ax3 = fig.add_subplot(333)
|
||
|
ax4 = fig.add_subplot(334)
|
||
|
ax5 = fig.add_subplot(335)
|
||
|
ax6 = fig.add_subplot(336)
|
||
|
ax7 = fig.add_subplot(337)
|
||
|
ax8 = fig.add_subplot(338)
|
||
|
ax9 = fig.add_subplot(339)
|
||
|
|
||
|
# 左上の図
|
||
|
clf = OneClassSVM(nu=0.05, kernel="rbf", gamma=0.0001)
|
||
|
clf.fit(data[["x", "y"]])
|
||
|
pred = clf.predict(data[["x", "y"]]) # データの判定結果(-1は外れ値、1は正常値)
|
||
|
|
||
|
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
||
|
Z = Z.reshape(xx.shape)
|
||
|
|
||
|
plot_ocsvm(ax1, data, pred, xx, yy, Z, r"$\nu=0.05,\gamma=0.0001$")
|
||
|
|
||
|
# 中上の図
|
||
|
clf = OneClassSVM(nu=0.05, kernel="rbf", gamma=0.001)
|
||
|
clf.fit(data[["x", "y"]])
|
||
|
pred = clf.predict(data[["x", "y"]])
|
||
|
|
||
|
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
||
|
Z = Z.reshape(xx.shape)
|
||
|
|
||
|
plot_ocsvm(ax2, data, pred, xx, yy, Z, r"$\nu=0.05,\gamma=0.001$")
|
||
|
|
||
|
# 右上の図
|
||
|
clf = OneClassSVM(nu=0.05, kernel="rbf", gamma=0.01)
|
||
|
clf.fit(data[["x", "y"]])
|
||
|
pred = clf.predict(data[["x", "y"]])
|
||
|
|
||
|
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
||
|
Z = Z.reshape(xx.shape)
|
||
|
|
||
|
plot_ocsvm(ax3, data, pred, xx, yy, Z, r"$\nu=0.05,\gamma=0.01$")
|
||
|
|
||
|
## 左中の図
|
||
|
clf = OneClassSVM(nu=0.1, kernel="rbf", gamma=0.0001)
|
||
|
clf.fit(data[["x", "y"]])
|
||
|
pred = clf.predict(data[["x", "y"]])
|
||
|
|
||
|
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
||
|
Z = Z.reshape(xx.shape)
|
||
|
|
||
|
plot_ocsvm(ax4, data, pred, xx, yy, Z, r"$\nu=0.1,\gamma=0.0001$")
|
||
|
|
||
|
## 中々の図
|
||
|
clf = OneClassSVM(nu=0.1, kernel="rbf", gamma=0.001)
|
||
|
clf.fit(data[["x", "y"]])
|
||
|
pred = clf.predict(data[["x", "y"]])
|
||
|
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
||
|
Z = Z.reshape(xx.shape)
|
||
|
plot_ocsvm(ax5, data, pred, xx, yy, Z, r"$\nu=0.1,\gamma=0.001$")
|
||
|
|
||
|
## 右中の図
|
||
|
clf = OneClassSVM(nu=0.1, kernel="rbf", gamma=0.01)
|
||
|
clf.fit(data[["x", "y"]])
|
||
|
pred = clf.predict(data[["x", "y"]])
|
||
|
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
||
|
Z = Z.reshape(xx.shape)
|
||
|
plot_ocsvm(ax6, data, pred, xx, yy, Z, r"$\nu=0.1,\gamma=0.01$")
|
||
|
|
||
|
## 左下の図
|
||
|
clf = OneClassSVM(nu=0.2, kernel="rbf", gamma=0.01)
|
||
|
clf.fit(data[["x", "y"]])
|
||
|
pred = clf.predict(data[["x", "y"]])
|
||
|
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
||
|
Z = Z.reshape(xx.shape)
|
||
|
plot_ocsvm(ax7, data, pred, xx, yy, Z, r"$\nu=0.2,\gamma=0.01$")
|
||
|
|
||
|
## 中下の図
|
||
|
clf = OneClassSVM(nu=0.2, kernel="rbf", gamma=0.1)
|
||
|
clf.fit(data[["x", "y"]])
|
||
|
pred = clf.predict(data[["x", "y"]])
|
||
|
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
||
|
Z = Z.reshape(xx.shape)
|
||
|
plot_ocsvm(ax8, data, pred, xx, yy, Z, r"$\nu=0.2,\gamma=0.1$")
|
||
|
|
||
|
## 右下の図
|
||
|
clf = OneClassSVM(nu=0.2, kernel="rbf", gamma=0.2)
|
||
|
clf.fit(data[["x", "y"]])
|
||
|
pred = clf.predict(data[["x", "y"]])
|
||
|
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
||
|
Z = Z.reshape(xx.shape)
|
||
|
plot_ocsvm(ax9, data, pred, xx, yy, Z, r"$\nu=0.2,\gamma=0.2$")
|
||
|
|
||
|
plt.show()
|
||
Add sample based on chapter 5-4