0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ランダムフォレストは決定木についてバギングを行っている。
そこで線形モデルをバギングしたらどうなるのかやってみた。

ライブラリ

from sklearn.datasets import make_blobs
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression as LR
from sklearn.ensemble import BaggingClassifier
import numpy as np
import matplotlib.pyplot as plt

データのロード

x, y = make_blobs(n_samples=300, centers=4, random_state=0, cluster_std=1.0)
plt.scatter(x[:, 0], x[:, 1], c=y, cmap="brg")
plt.show()

決定境界描画

def visualize_classifier(model, X, y, ax=None, cmap='brg'):
    ax = ax or plt.gca()
    
    # Plot the training points
    ax.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap=cmap,
               clim=(y.min(), y.max()), zorder=3)
    ax.axis('tight')
    ax.axis('off')
    xlim = ax.get_xlim()
    ylim = ax.get_ylim()
    
    # fit the estimator
    model.fit(X, y)
    xx, yy = np.meshgrid(np.linspace(*xlim, num=200),
                         np.linspace(*ylim, num=200))
    Z = model.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape)

    # Create a color plot with the results
    n_classes = len(np.unique(y))
    contours = ax.contourf(xx, yy, Z, alpha=0.3,
                           levels=np.arange(n_classes + 1) - 0.5,
                           cmap=cmap, clim=(y.min(), y.max()),
                           zorder=1)

    ax.set(xlim=xlim, ylim=ylim)

SVM

visualize_classifier(SVC(kernel="linear"), x, y, ax=None, cmap='brg')

image.png

ランダムSVM

visualize_classifier(BaggingClassifier(estimator=SVC(kernel="linear"), n_estimators=500), x, y, ax=None, cmap='brg')

image.png

ランダムSVMの方が直線的ですね。普通のSVMは真ん中のところが少し角になっています。

Logit

visualize_classifier(LR(), x, y, ax=None, cmap='brg')

image.png

ランダムLogit

visualize_classifier(BaggingClassifier(estimator=LR(), n_estimators=500), x, y, ax=None, cmap='brg')

image.png
ロジスティック回帰はあんまり変わらないですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?