はじめに
この実験では、Python の scikit - learn ライブラリを使って、いくつかのランダムに生成された分類データセットをプロットする方法を示します。x 軸と y 軸にプロットされた 2 つの特徴量を使って、すべてのデータセットを可視化します。各点の色はそのクラスラベルを表します。
VM のヒント
VM の起動が完了したら、左上隅をクリックしてノートブックタブに切り替え、Jupyter Notebook にアクセスして練習します。
時々、Jupyter Notebook が読み込み終了するまで数秒待つ必要があります。Jupyter Notebook の制限により、操作の検証を自動化することはできません。
学習中に問題に遭遇した場合は、Labby にお問い合わせください。セッション後にフィードバックを提供してください。すぐに問題を解決いたします。
ライブラリのインポート
まず、必要なライブラリをインポートする必要があります。matplotlib と scikit - learn を使用します。
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.datasets import make_blobs
from sklearn.datasets import make_gaussian_quantiles
グラフサイズの設定とサブプロットの調整
グラフのサイズを設定し、サブプロットを調整して、読みやすくします。
plt.figure(figsize=(8, 8))
plt.subplots_adjust(bottom=0.05, top=0.9, left=0.05, right=0.95)
1 つの情報提供特徴量、クラスごとに 1 つのクラスタ
1 つの情報提供特徴量とクラスごとに 1 つのクラスタを持つデータセットを作成し、プロットします。
plt.subplot(321)
plt.title("One informative feature, one cluster per class", fontsize="small")
X1, Y1 = make_classification(n_features=2, n_redundant=0, n_informative=1, n_clusters_per_class=1)
plt.scatter(X1[:, 0], X1[:, 1], marker="o", c=Y1, s=25, edgecolor="k")
2 つの情報提供特徴量、クラスごとに 1 つのクラスタ
2 つの情報提供特徴量とクラスごとに 1 つのクラスタを持つデータセットを作成し、プロットします。
plt.subplot(322)
plt.title("Two informative features, one cluster per class", fontsize="small")
X1, Y1 = make_classification(n_features=2, n_redundant=0, n_informative=2, n_clusters_per_class=1)
plt.scatter(X1[:, 0], X1[:, 1], marker="o", c=Y1, s=25, edgecolor="k")
2 つの情報提供特徴量、クラスごとに 2 つのクラスタ
2 つの情報提供特徴量とクラスごとに 2 つのクラスタを持つデータセットを作成し、プロットします。
plt.subplot(323)
plt.title("Two informative features, two clusters per class", fontsize="small")
X2, Y2 = make_classification(n_features=2, n_redundant=0, n_informative=2)
plt.scatter(X2[:, 0], X2[:, 1], marker="o", c=Y2, s=25, edgecolor="k")
多クラス、2 つの情報提供特徴量、1 つのクラスタ
複数のクラスと 2 つの情報提供特徴量、そして 1 つのクラスタを持つデータセットを作成し、プロットします。
plt.subplot(324)
plt.title("Multi-class, two informative features, one cluster", fontsize="small")
X1, Y1 = make_classification(n_features=2, n_redundant=0, n_informative=2, n_clusters_per_class=1, n_classes=3)
plt.scatter(X1[:, 0], X1[:, 1], marker="o", c=Y1, s=25, edgecolor="k")
3 つのブロブ
3 つのブロブを持つデータセットを作成し、プロットします。
plt.subplot(325)
plt.title("Three blobs", fontsize="small")
X1, Y1 = make_blobs(n_features=2, centers=3)
plt.scatter(X1[:, 0], X1[:, 1], marker="o", c=Y1, s=25, edgecolor="k")
3 つの四分位数に分割されたガウス分布
3 つの四分位数に分割されたガウス分布を持つデータセットを作成し、プロットします。
plt.subplot(326)
plt.title("Gaussian divided into three quantiles", fontsize="small")
X1, Y1 = make_gaussian_quantiles(n_features=2, n_classes=3)
plt.scatter(X1[:, 0], X1[:, 1], marker="o", c=Y1, s=25, edgecolor="k")
プロットを表示する
最終的なプロットを表示します。
plt.show()
まとめ
この実験では、Python の scikit - learn ライブラリを使って、いくつかのランダムに生成された分類データセットをプロットする方法を示しました。x 軸と y 軸にプロットされた 2 つの特徴量を使ってすべてのデータセットを可視化します。各点の色はそのクラスラベルを表します。