Iris數據集上的標簽傳播與SVM的決策邊界?

標簽傳播和SVM之間在鳶尾花數據集上生成的決策邊界的比較。

這表明標簽傳播即使在標簽數據較少的情況下也能很好地學習。

輸出:

輸入:

print(__doc__)

# 作者: Clay Woolam <clay@woolam.org>
# 執照: BSD

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn import svm
from sklearn.semi_supervised import LabelSpreading

rng = np.random.RandomState(0)

iris = datasets.load_iris()

X = iris.data[:, :2]
y = iris.target

# 網格中的步長
h = .02

y_30 = np.copy(y)
y_30[rng.rand(len(y)) < 0.3] = -1
y_50 = np.copy(y)
y_50[rng.rand(len(y)) < 0.5] = -1
# 我們創建一個SVM實例并擬合數據。由于要繪制支持向量,因此我們不縮放數據
ls30 = (LabelSpreading().fit(X, y_30), y_30)
ls50 = (LabelSpreading().fit(X, y_50), y_50)
ls100 = (LabelSpreading().fit(X, y), y)
rbf_svc = (svm.SVC(kernel='rbf', gamma=.5).fit(X, y), y)

# 創建要繪制的網格
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h))

# 圖像的標題
titles = ['Label Spreading 30% data',
          'Label Spreading 50% data',
          'Label Spreading 100% data',
          'SVC with rbf kernel']

color_map = {-1: (111), 0: (00.9), 1: (100), 2: (.8.60)}

for i, (clf, y_train) in enumerate((ls30, ls50, ls100, rbf_svc)):
    # 繪制決策邊界。
    # 為此,我們將為網格[x_min,x_max] x [y_min,y_max]中的每個點分配顏色。
    plt.subplot(22, i + 1)
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])

    # 將結果放入顏色圖
    Z = Z.reshape(xx.shape)
    plt.contourf(xx, yy, Z, cmap=plt.cm.Paired)
    plt.axis('off')

    # 繪制訓練點
    colors = [color_map[y] for y in y_train]
    plt.scatter(X[:, 0], X[:, 1], c=colors, edgecolors='black')

    plt.title(titles[i])

plt.suptitle("Unlabeled points are colored white", y=0.1)
plt.show()

腳本的總運行時間:0分1.185秒。