AdaBoost による決定木の分類

Machine LearningMachine LearningBeginner
オンラインで実践に進む

This tutorial is from open-source community. Access the source code

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

このドキュメントでは、AdaBoost を使って決定木を学習し、2 次元の 2 つのガウス分布によるクラスターから構成されるデータセットを分類する方法について、手順を追って説明します。

VM のヒント

VM の起動が完了したら、画面の左上隅をクリックしてノートブックタブに切り替え、Jupyter Notebook を使って練習しましょう。

Jupyter Notebook の読み込みには数秒かかる場合があります。Jupyter Notebook の制限により、操作の検証は自動化できません。

学習中に問題がある場合は、Labby にお問い合わせください。セッション終了後にフィードバックを提供してください。すぐに問題を解決いたします。

必要なライブラリをインポートする

このステップでは、この実験に必要なライブラリをインポートします。

import numpy as np
import matplotlib.pyplot as plt

from sklearn.ensemble import AdaBoostClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import make_gaussian_quantiles
from sklearn.inspection import DecisionBoundaryDisplay

データセットを構築する

このステップでは、sklearn.datasetsモジュールのmake_gaussian_quantiles関数を使って、2 つのガウス分布によるクラスターから構成される非線形分離可能な分類データセットを作成します。また、2 つのクラスターを連結して、それぞれにラベルを割り当てます。

X1, y1 = make_gaussian_quantiles(
    cov=2.0, n_samples=200, n_features=2, n_classes=2, random_state=1
)
X2, y2 = make_gaussian_quantiles(
    mean=(3, 3), cov=1.5, n_samples=300, n_features=2, n_classes=2, random_state=1
)
X = np.concatenate((X1, X2))
y = np.concatenate((y1, -y2 + 1))

AdaBoost による決定木を作成して適合させる

このステップでは、sklearn.ensembleモジュールのAdaBoostClassifierクラスを使って、AdaBoost による決定木を作成します。ベースの推定器として決定木を使い、max_depthパラメータを 1 に設定します。また、algorithmパラメータを"SAMME"に、n_estimatorsパラメータを 200 に設定します。最後に、分類器をデータセットに適合させます。

bdt = AdaBoostClassifier(
    DecisionTreeClassifier(max_depth=1), algorithm="SAMME", n_estimators=200
)

bdt.fit(X, y)

決定境界と学習ポイントを描画する

このステップでは、決定境界と学習ポイントを描画します。sklearn.inspectionモジュールのfrom_estimatorメソッドを使ってDecisionBoundaryDisplayオブジェクトを作成し、AdaBoost 分類器、データセット、その他のパラメータを渡します。また、各クラスに異なる色を使って学習ポイントを描画します。

plot_colors = "br"
plot_step = 0.02
class_names = "AB"

plt.figure(figsize=(10, 5))

## 決定境界を描画する
ax = plt.subplot(121)
disp = DecisionBoundaryDisplay.from_estimator(
    bdt,
    X,
    cmap=plt.cm.Paired,
    response_method="predict",
    ax=ax,
    xlabel="x",
    ylabel="y",
)
x_min, x_max = disp.xx0.min(), disp.xx0.max()
y_min, y_max = disp.xx1.min(), disp.xx1.max()
plt.axis("tight")

## 学習ポイントを描画する
for i, n, c in zip(range(2), class_names, plot_colors):
    idx = np.where(y == i)
    plt.scatter(
        X[idx, 0],
        X[idx, 1],
        c=c,
        cmap=plt.cm.Paired,
        s=20,
        edgecolor="k",
        label="Class %s" % n,
    )
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.legend(loc="upper right")

plt.title("Decision Boundary")

2 クラスの決定スコアを描画する

このステップでは、2 クラスの決定スコアを描画します。AdaBoost 分類器のdecision_functionメソッドを使って、データセットの各サンプルに対する決定スコアを取得します。そして、各クラスの決定スコアのヒストグラムを描画します。

## 2 クラスの決定スコアを描画する
twoclass_output = bdt.decision_function(X)
plot_range = (twoclass_output.min(), twoclass_output.max())
plt.subplot(122)
for i, n, c in zip(range(2), class_names, plot_colors):
    plt.hist(
        twoclass_output[y == i],
        bins=10,
        range=plot_range,
        facecolor=c,
        label="Class %s" % n,
        alpha=0.5,
        edgecolor="k",
    )
x1, x2, y1, y2 = plt.axis()
plt.axis((x1, x2, y1, y2 * 1.2))
plt.legend(loc="upper right")
plt.ylabel("サンプル数")
plt.xlabel("スコア")
plt.title("決定スコア")

plt.tight_layout()
plt.subplots_adjust(wspace=0.35)
plt.show()

まとめ

この実験では、AdaBoost を使って決定木を訓練し、2 つのガウス分布によるクラスターから構成される 2 次元のデータセットを分類する方法を学びました。また、分類器の決定境界と決定スコアを描画する方法も学びました。