多項ロジスティック回帰と 1 対残りロジスティック回帰の描画

Beginner

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

はじめに

ロジスティック回帰は、2 値分類と多クラス分類問題に使用できる分類アルゴリズムです。この実験では、scikit-learn ライブラリを使用して、多項ロジスティック回帰と one-vs-rest ロジスティック回帰の 2 つのロジスティック回帰モデルの決定面を描画します。3 クラスのデータセットを使用して、2 つのモデルの決定境界を描画して、それらの性能を比較します。

VM のヒント

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

場合によっては、Jupyter Notebook が読み込み終わるまで数秒待つ必要があります。Jupyter Notebook の制限により、操作の検証を自動化することはできません。

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

ライブラリのインポート

この実験に必要なライブラリをインポートして始めましょう。scikit-learn ライブラリを使用してデータセットを生成し、ロジスティック回帰モデルを学習し、matplotlib ライブラリを使用して決定境界を描画します。

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.linear_model import LogisticRegression
from sklearn.inspection import DecisionBoundaryDisplay

データセットの生成

scikit-learn のmake_blobs関数を使用して、3 クラスのデータセットを生成します。1000 個のサンプルを使用し、ブロブの中心を[-5, 0], [0, 1.5], [5, -1]に設定します。その後、データセットを変換行列を使って変換して、分類がより難しくなるようにします。

centers = [[-5, 0], [0, 1.5], [5, -1]]
X, y = make_blobs(n_samples=1000, centers=centers, random_state=40)
transformation = [[0.4, 0.2], [-0.4, 1.2]]
X = np.dot(X, transformation)

多項ロジスティック回帰モデルの学習

ここでは、scikit-learn のLogisticRegression関数を使って多項ロジスティック回帰モデルを学習します。ソルバーをsagに、最大反復回数を 100 に、乱数シードを 42 に、多クラスオプションを"multinomial"に設定します。その後、モデルの学習スコアを表示します。

clf = LogisticRegression(
        solver="sag", max_iter=100, random_state=42, multi_class="multinomial"
    ).fit(X, y)

print("training score : %.3f (%s)" % (clf.score(X, y), "multinomial"))

多項ロジスティック回帰モデルの決定境界の描画

ここでは、scikit-learn のDecisionBoundaryDisplay関数を使って、多項ロジスティック回帰モデルの決定面を描画します。応答方法を"predict"に、カラーマップを"plt.cm.Paired"に設定し、学習ポイントも描画します。

_, ax = plt.subplots()
DecisionBoundaryDisplay.from_estimator(
        clf, X, response_method="predict", cmap=plt.cm.Paired, ax=ax
    )
plt.title("Decision surface of LogisticRegression (multinomial)")
plt.axis("tight")

colors = "bry"
for i, color in zip(clf.classes_, colors):
        idx = np.where(y == i)
        plt.scatter(
            X[idx, 0], X[idx, 1], c=color, cmap=plt.cm.Paired, edgecolor="black", s=20
        )

1 対残りロジスティック回帰モデルの学習

ここでは、ステップ 3 と同じパラメータを使用して、1 対残りロジスティック回帰モデルを学習しますが、多クラスオプションを"ovr"に設定します。その後、モデルの学習スコアを表示します。

clf = LogisticRegression(
        solver="sag", max_iter=100, random_state=42, multi_class="ovr"
    ).fit(X, y)

print("training score : %.3f (%s)" % (clf.score(X, y), "ovr"))

1 対残りロジスティック回帰モデルの決定境界の描画

ここでは、ステップ 4 と同じパラメータを使用して、1 対残りロジスティック回帰モデルの決定面を描画しますが、3 つの 1 対残り分類器に対応するハイパープレーンを破線で描画します。

_, ax = plt.subplots()
DecisionBoundaryDisplay.from_estimator(
        clf, X, response_method="predict", cmap=plt.cm.Paired, ax=ax
    )
plt.title("Decision surface of LogisticRegression (ovr)")
plt.axis("tight")

colors = "bry"
for i, color in zip(clf.classes_, colors):
        idx = np.where(y == i)
        plt.scatter(
            X[idx, 0], X[idx, 1], c=color, cmap=plt.cm.Paired, edgecolor="black", s=20
        )

xmin, xmax = plt.xlim()
ymin, ymax = plt.ylim()
coef = clf.coef_
intercept = clf.intercept_

def plot_hyperplane(c, color):
        def line(x0):
            return (-(x0 * coef[c, 0]) - intercept[c]) / coef[c, 1]

        plt.plot([xmin, xmax], [line(xmin), line(xmax)], ls="--", color=color)

for i, color in zip(clf.classes_, colors):
        plot_hyperplane(i, color)

プロットを可視化する

ここでは、2 つのモデルの決定境界を比較するために、両方のプロットを横並びに可視化します。

plt.subplot(1,2,1)
_, ax = plt.subplots()
DecisionBoundaryDisplay.from_estimator(
        clf, X, response_method="predict", cmap=plt.cm.Paired, ax=ax
    )
plt.title("Multinomial Logistic Regression")
plt.axis("tight")

colors = "bry"
for i, color in zip(clf.classes_, colors):
        idx = np.where(y == i)
        plt.scatter(
            X[idx, 0], X[idx, 1], c=color, cmap=plt.cm.Paired, edgecolor="black", s=20
        )

plt.subplot(1,2,2)
_, ax = plt.subplots()
DecisionBoundaryDisplay.from_estimator(
        clf, X, response_method="predict", cmap=plt.cm.Paired, ax=ax
    )
plt.title("One-vs-Rest Logistic Regression")
plt.axis("tight")

colors = "bry"
for i, color in zip(clf.classes_, colors):
        idx = np.where(y == i)
        plt.scatter(
            X[idx, 0], X[idx, 1], c=color, cmap=plt.cm.Paired, edgecolor="black", s=20
        )

xmin, xmax = plt.xlim()
ymin, ymax = plt.ylim()
coef = clf.coef_
intercept = clf.intercept_

def plot_hyperplane(c, color):
        def line(x0):
            return (-(x0 * coef[c, 0]) - intercept[c]) / coef[c, 1]

        plt.plot([xmin, xmax], [line(xmin), line(xmax)], ls="--", color=color)

for i, color in zip(clf.classes_, colors):
        plot_hyperplane(i, color)

plt.subplots_adjust(wspace=0.5)
plt.show()

まとめ

この実験では、多項ロジスティック回帰と 1 対残りロジスティック回帰の 2 つのロジスティック回帰モデルの決定面を描画する方法を学びました。3 クラスのデータセットを使用し、2 つのモデルの決定境界を描画することでそれらの性能を比較しました。多項ロジスティック回帰モデルは決定境界が滑らかであるのに対し、1 対残りロジスティック回帰モデルは各クラスに対して 3 つの別々の決定境界を持っていることが観察されました。