はじめに
この実験では、scikit-learn を使って実際のデータセットに対してアウトライア検出を行います。アウトライア検出とは、データの大部分と大きく異なるデータポイントを特定するプロセスです。アウトライアは測定誤差、データの破損によって引き起こされる場合もあれば、単にまれなイベントを表す場合もあります。アウトライア検出は、不正検出、ネットワーク侵入検出、医療診断など、多くのアプリケーションにおいて重要です。
VM のヒント
VM の起動が完了したら、左上隅をクリックして ノートブック タブに切り替えて、Jupyter Notebook を使って練習しましょう。
Jupyter Notebook の読み込みには数秒かかる場合があります。Jupyter Notebook の制限により、操作の検証を自動化することはできません。
学習中に問題がある場合は、Labby にお問い合わせください。セッション終了後にフィードバックを提供してください。すぐに問題を解決いたします。
ライブラリのインポートとデータセットの読み込み
必要なライブラリをインポートし、scikit-learn から Wine データセットを読み込んで始めましょう。Wine データセットには、さまざまな種類のワインの情報、その化学的特性も含まれています。
import numpy as np
from sklearn.covariance import EllipticEnvelope
from sklearn.svm import OneClassSVM
import matplotlib.pyplot as plt
from sklearn.datasets import load_wine
## Load dataset
X1 = load_wine()["data"][:, [1, 2]] ## two clusters
X2 = load_wine()["data"][:, [6, 9]] ## "banana"-shaped
識別器と色の定義
この実験で使用するアウトライア検出識別器を定義します。また、結果をプロットする際に使用する色も定義します。
## Define "classifiers" to be used
classifiers = {
"Empirical Covariance": EllipticEnvelope(support_fraction=1.0, contamination=0.25),
"Robust Covariance (Minimum Covariance Determinant)": EllipticEnvelope(
contamination=0.25
),
"OCSVM": OneClassSVM(nu=0.25, gamma=0.35),
}
colors = ["m", "g", "b"]
2 次元データのアウトライア検出
2 次元の Wine データセットに対してアウトライア検出を行います。アウトライアを検出するために 3 つの異なる識別器を使用します。経験的共分散、頑健な共分散、および One-Class SVM です。その後、結果をプロットします。
## Learn a frontier for outlier detection with several classifiers
xx1, yy1 = np.meshgrid(np.linspace(0, 6, 500), np.linspace(1, 4.5, 500))
for i, (clf_name, clf) in enumerate(classifiers.items()):
plt.figure(1)
clf.fit(X1)
Z1 = clf.decision_function(np.c_[xx1.ravel(), yy1.ravel()])
Z1 = Z1.reshape(xx1.shape)
plt.contour(
xx1, yy1, Z1, levels=[0], linewidths=2, colors=colors[i]
)
## Plot the results (= shape of the data points cloud)
plt.figure(1) ## two clusters
plt.title("Outlier detection on a real data set (wine recognition)")
plt.scatter(X1[:, 0], X1[:, 1], color="black")
plt.xlim((xx1.min(), xx1.max()))
plt.ylim((yy1.min(), yy1.max()))
plt.ylabel("ash")
plt.xlabel("malic_acid")
plt.show()
複雑なデータのアウトライア検出
「バナナ」形状の Wine データセットに対してアウトライア検出を行います。以前と同じ 3 つの識別器を使用し、結果をプロットします。
## Learn a frontier for outlier detection with several classifiers
xx2, yy2 = np.meshgrid(np.linspace(-1, 5.5, 500), np.linspace(-2.5, 19, 500))
for i, (clf_name, clf) in enumerate(classifiers.items()):
plt.figure(2)
clf.fit(X2)
Z2 = clf.decision_function(np.c_[xx2.ravel(), yy2.ravel()])
Z2 = Z2.reshape(xx2.shape)
plt.contour(
xx2, yy2, Z2, levels=[0], linewidths=2, colors=colors[i]
)
## Plot the results (= shape of the data points cloud)
plt.figure(2) ## "banana" shape
plt.title("Outlier detection on a real data set (wine recognition)")
plt.scatter(X2[:, 0], X2[:, 1], color="black")
plt.xlim((xx2.min(), xx2.max()))
plt.ylim((yy2.min(), yy2.max()))
plt.ylabel("color_intensity")
plt.xlabel("flavanoids")
plt.show()
まとめ
この実験では、scikit-learn を使用して 2 次元の Wine データセットに対してアウトライア検出を行いました。アウトライアを検出するために 3 つの異なる識別器を使用しました。経験的共分散、頑健な共分散、および One-Class SVM です。その後、結果をプロットして、データと検出されたアウトライアを視覚化しました。アウトライア検出はデータ分析において重要なタスクであり、scikit-learn はこのタスクを効率的に実行するためのいくつかのツールを提供しています。