はじめに
マニホールド学習は、高次元データの潜在的な構造を学習する機械学習のサブフィールドです。この実験では、球状のデータセットにさまざまなマニホールド学習手法を適用します。次元削減を使って、マニホールド学習手法に関する直感を得ます。
VM のヒント
VM の起動が完了したら、左上隅をクリックしてノートブックタブに切り替え、Jupyter Notebook を使って練習しましょう。
Jupyter Notebook の読み込みには数秒かかる場合があります。Jupyter Notebook の制限により、操作の検証は自動化できません。
学習中に問題があった場合は、Labby にお問い合わせください。セッション終了後にフィードバックを提供してください。すぐに問題を解決いたします。
ライブラリのインポート
必要なライブラリをインポートして始めましょう。scikit-learn、matplotlib、および numpy を使用します。
from time import time
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter
from sklearn import manifold
from sklearn.utils import check_random_state
import mpl_toolkits.mplot3d
球状のデータセットを作成する
次に、球状のデータセットを作成します。球体を作成し、極を切断し、その側面に薄いスライスを入れます。これにより、マニホールド学習手法が 2 次元に投影する際にデータセットを「広げる」ことができます。
n_neighbors = 10
n_samples = 1000
random_state = check_random_state(0)
p = random_state.rand(n_samples) * (2 * np.pi - 0.55)
t = random_state.rand(n_samples) * np.pi
indices = (t < (np.pi - (np.pi / 8))) & (t > ((np.pi / 8)))
colors = p[indices]
x, y, z = (
np.sin(t[indices]) * np.cos(p[indices]),
np.sin(t[indices]) * np.sin(p[indices]),
np.cos(t[indices]),
)
fig = plt.figure(figsize=(15, 8))
plt.suptitle(
"Manifold Learning with %i points, %i neighbors" % (1000, n_neighbors), fontsize=14
)
ax = fig.add_subplot(251, projection="3d")
ax.scatter(x, y, z, c=p[indices], cmap=plt.cm.rainbow)
ax.view_init(40, -10)
sphere_data = np.array([x, y, z]).T
局所的線形埋め込みによるマニホールド学習を実行する
次に、局所的線形埋め込み(Locally Linear Embedding:LLE)によるマニホールド学習を実行します。LLE は、少数のサンプルで複雑なマニホールドを展開できる強力な手法です。LLE の 4 つのバリエーションを使用し、それらの結果を比較します。
methods = ["standard", "ltsa", "hessian", "modified"]
labels = ["LLE", "LTSA", "Hessian LLE", "Modified LLE"]
for i, method in enumerate(methods):
t0 = time()
trans_data = (
manifold.LocallyLinearEmbedding(
n_neighbors=n_neighbors, n_components=2, method=method
)
.fit_transform(sphere_data)
.T
)
t1 = time()
print("%s: %.2g sec" % (methods[i], t1 - t0))
ax = fig.add_subplot(252 + i)
plt.scatter(trans_data[0], trans_data[1], c=colors, cmap=plt.cm.rainbow)
plt.title("%s (%.2g sec)" % (labels[i], t1 - t0))
ax.xaxis.set_major_formatter(NullFormatter())
ax.yaxis.set_major_formatter(NullFormatter())
plt.axis("tight")
Isomap によるマニホールド学習を実行する
次に、Isomap によるマニホールド学習を実行します。Isomap は、非線形次元削減手法であり、すべての点のペア間の測地距離を維持するデータの低次元埋め込みを求めます。
t0 = time()
trans_data = (
manifold.Isomap(n_neighbors=n_neighbors, n_components=2)
.fit_transform(sphere_data)
.T
)
t1 = time()
print("%s: %.2g sec" % ("ISO", t1 - t0))
ax = fig.add_subplot(257)
plt.scatter(trans_data[0], trans_data[1], c=colors, cmap=plt.cm.rainbow)
plt.title("%s (%.2g sec)" % ("Isomap", t1 - t0))
ax.xaxis.set_major_formatter(NullFormatter())
ax.yaxis.set_major_formatter(NullFormatter())
plt.axis("tight")
多次元尺度法(Multi-dimensional Scaling:MDS)を実行する
次に、多次元尺度法(MDS)によるマニホールド学習を実行します。MDS は、点間の距離が元の高次元空間の距離を反映するような、データの低次元表現を求める手法です。
t0 = time()
mds = manifold.MDS(2, max_iter=100, n_init=1, normalized_stress="auto")
trans_data = mds.fit_transform(sphere_data).T
t1 = time()
print("MDS: %.2g sec" % (t1 - t0))
ax = fig.add_subplot(258)
plt.scatter(trans_data[0], trans_data[1], c=colors, cmap=plt.cm.rainbow)
plt.title("MDS (%.2g sec)" % (t1 - t0))
ax.xaxis.set_major_formatter(NullFormatter())
ax.yaxis.set_major_formatter(NullFormatter())
plt.axis("tight")
スペクトラル埋め込みを実行する
次に、スペクトラル埋め込みによるマニホールド学習を実行します。スペクトラル埋め込みは、点間のペアワイズ距離を保つデータの低次元表現を求める手法です。
t0 = time()
se = manifold.SpectralEmbedding(n_components=2, n_neighbors=n_neighbors)
trans_data = se.fit_transform(sphere_data).T
t1 = time()
print("Spectral Embedding: %.2g sec" % (t1 - t0))
ax = fig.add_subplot(259)
plt.scatter(trans_data[0], trans_data[1], c=colors, cmap=plt.cm.rainbow)
plt.title("Spectral Embedding (%.2g sec)" % (t1 - t0))
ax.xaxis.set_major_formatter(NullFormatter())
ax.yaxis.set_major_formatter(NullFormatter())
plt.axis("tight")
t-分布確率的近傍埋め込み(t-distributed Stochastic Neighbor Embedding:t-SNE)を実行する
最後に、t-分布確率的近傍埋め込み(t-SNE)によるマニホールド学習を実行します。t-SNE は、点間の局所距離を保つデータの低次元表現を求める手法です。
t0 = time()
tsne = manifold.TSNE(n_components=2, random_state=0)
trans_data = tsne.fit_transform(sphere_data).T
t1 = time()
print("t-SNE: %.2g sec" % (t1 - t0))
ax = fig.add_subplot(2, 5, 10)
plt.scatter(trans_data[0], trans_data[1], c=colors, cmap=plt.cm.rainbow)
plt.title("t-SNE (%.2g sec)" % (t1 - t0))
ax.xaxis.set_major_formatter(NullFormatter())
ax.yaxis.set_major_formatter(NullFormatter())
plt.axis("tight")
plt.show()
まとめ
この実験では、球状のデータセットに対してさまざまなマニホールド学習手法を適用しました。局所的線形埋め込み(Locally Linear Embedding:LLE)、Isomap、多次元尺度法(Multi-dimensional Scaling:MDS)、スペクトラル埋め込み、および t-分布確率的近傍埋め込み(t-distributed Stochastic Neighbor Embedding:t-SNE)を使用して、マニホールド学習手法に関する直感を得ました。これらの手法は、高次元データの分析と可視化に役立ちます。