はじめに
この実験では、Iris データセット上の半教師あり分類器を探索します。Iris データセット上で Label Spreading、Self-training、およびサポートベクトルマシン(SVM)によって生成される決定境界を比較します。人気のある Python 機械学習ライブラリである scikit-learn を使用して、分類器を実装し、決定境界を可視化します。
VM のヒント
VM の起動が完了したら、左上隅をクリックしてノートブックタブに切り替え、Jupyter Notebook を使って練習しましょう。
Jupyter Notebook が読み込み終わるまで数秒待つことがあります。Jupyter Notebook の制限により、操作の検証は自動化できません。
学習中に問題がある場合は、Labby にお問い合わせください。セッション後にフィードバックを提供してください。すぐに問題を解決いたします。
Iris データセットを読み込んでデータを分割する
機械学習における分類タスクで広く使用される Iris データセットを読み込みます。このデータセットには、Iris の花の 150 個のサンプルが含まれており、各サンプルには 4 つの特徴量があります。花弁の長さ、花弁の幅、花びらの長さ、花びらの幅です。このデータセットを入力特徴量とターゲットラベルに分割します。
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
## Load the Iris dataset
iris = datasets.load_iris()
## Split the dataset into input features and target labels
X = iris.data[:, :2] ## We will only use the first two features for visualization purposes
y = iris.target
Label Spreading 分類器をセットアップする
異なる割合のラベル付きデータ(30%、50%、100%)を持つ 3 つの Label Spreading 分類器をセットアップします。Label Spreading は、半教師あり学習アルゴリズムであり、ラベル付きデータポイントから非ラベル付きデータポイントへの類似性に基づいてラベルを伝播させます。
from sklearn.semi_supervised import LabelSpreading
## Set up the Label Spreading classifiers
rng = np.random.RandomState(0)
y_rand = rng.rand(y.shape[0])
y_30 = np.copy(y)
y_30[y_rand < 0.3] = -1 ## set random samples to be unlabeled
y_50 = np.copy(y)
y_50[y_rand < 0.5] = -1
ls30 = (LabelSpreading().fit(X, y_30), y_30, "Label Spreading 30% data")
ls50 = (LabelSpreading().fit(X, y_50), y_50, "Label Spreading 50% data")
ls100 = (LabelSpreading().fit(X, y), y, "Label Spreading 100% data")
Self-training 分類器をセットアップする
異なる割合のラベル付きデータ(30%と 50%)を持つ 2 つの Self-training 分類器をセットアップします。Self-training は、半教師あり学習アルゴリズムであり、ラベル付きデータ上で分類器を訓練し、その後、それを使って非ラベル付きデータのラベルを予測します。最も確信度の高い予測結果をラベル付きデータに追加し、収束するまでこのプロセスを繰り返します。
from sklearn.semi_supervised import SelfTrainingClassifier
from sklearn.svm import SVC
## Set up the Self-training classifiers
base_classifier = SVC(kernel="rbf", gamma=0.5, probability=True)
st30 = (
SelfTrainingClassifier(base_classifier).fit(X, y_30),
y_30,
"Self-training 30% data",
)
st50 = (
SelfTrainingClassifier(base_classifier).fit(X, y_50),
y_50,
"Self-training 50% data",
)
SVM 分類器をセットアップする
放射基底関数(RBF)カーネルを持つ SVM 分類器をセットアップします。SVM は、教師あり学習アルゴリズムであり、データを異なるクラスに分離する最適なハイパープレーンを見つけます。
from sklearn.svm import SVC
## Set up the SVM classifier
rbf_svc = (SVC(kernel="rbf", gamma=0.5).fit(X, y), y, "SVC with rbf kernel")
決定境界を可視化する
入力特徴空間をカバーする点のメッシュグリッドを作成し、各分類器を使用してメッシュグリッド内の点のラベルを予測します。その後、決定境界とラベル付きデータポイントをプロットします。
## Create a mesh grid to plot in
h = 0.02
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
## Define a color map for the labels
color_map = {-1: (1, 1, 1), 0: (0, 0, 0.9), 1: (1, 0, 0), 2: (0.8, 0.6, 0)}
## Set up the classifiers
classifiers = (ls30, st30, ls50, st50, ls100, rbf_svc)
## Plot the decision boundaries and labeled data points for each classifier
for i, (clf, y_train, title) in enumerate(classifiers):
## Plot the decision boundary
plt.subplot(3, 2, i + 1)
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
## Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)
plt.axis("off")
## Plot the labeled data points
colors = [color_map[y] for y in y_train]
plt.scatter(X[:, 0], X[:, 1], c=colors, edgecolors="black")
plt.title(title)
plt.suptitle("Unlabeled points are colored white", y=0.1)
plt.show()
まとめ
この実験では、アヤメデータセットに対して半教師あり分類器を検討しました。アヤメデータセットにおける Label Spreading、Self-training、および SVM によって生成される決定境界を比較しました。scikit-learn を使用して分類器を実装し、決定境界を可視化しました。少量のラベル付きデータが利用可能な場合でも、Label Spreading と Self-training が良好な決定境界を学習できることがわかりました。