使用自定义颜色可视化 SVM

Visualise SVMs with custom colours

我尝试使用 https://scikit-learn.org/stable/auto_examples/svm/plot_iris_svc.html 中的代码可视化 SVM,并希望能够为每个 class 指定颜色。为此,我使用 LinearSegmentedColormap.from_list 创建了自定义 colormap。此方法适用于 6 classes 或更少,但对于 >6 classes,等高线图的颜色通常是错误的。

如何为 >6 classes 指定颜色?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
from sklearn import svm, preprocessing


# create dummy data
use = (
        ((1,9),(.2,.1),'b'),
        ((4,0),(.2,.4),'r'),
        ((1,5),(.2,.3),'g'),
        ((6,3),(.3,.2),'m'),
        ((5,6),(.1,.2),'c'),
        ((4,2),(.1,.1),'xkcd:orange'),
        ((5,4),(.3,.2),'xkcd:peach'),
        ((3,1),(.1,.4),'xkcd:bright pink'),
        ((2,1),(.2,.1),'xkcd:crimson'),
      )
sx = [np.random.normal(loc=u[0][0], scale=u[1][0], size=(20,)) for u in use]
sy = [np.random.normal(loc=u[0][1], scale=u[1][1], size=(20,)) for u in use]
X = np.array([[ix[i], iy[i]] for ix, iy in zip(sx, sy) for i in range(20)])
y = np.array([i for i in range(len(use)) for n in range(20)])


# scale the data
Scaler = preprocessing.StandardScaler().fit(X)
X = Scaler.transform(X)


# color map
cm = LinearSegmentedColormap.from_list('use', [u[2] for u in use], N=len(use))


def make_meshgrid(x, y, h=.02):
    """Create a mesh of points to plot in

    Parameters
    ----------
    x: data to base x-axis meshgrid on
    y: data to base y-axis meshgrid on
    h: stepsize for meshgrid, optional

    Returns
    -------
    xx, yy : ndarray
    """
    x_min, x_max = x.min() - 1, x.max() + 1
    y_min, y_max = y.min() - 1, y.max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))
    return xx, yy


def plot_contours(ax, clf, xx, yy, **params):
    """Plot the decision boundaries for a classifier.

    Parameters
    ----------
    ax: matplotlib axes object
    clf: a classifier
    xx: meshgrid ndarray
    yy: meshgrid ndarray
    params: dictionary of params to pass to contourf, optional
    """
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z/Z.max()
    Z = Z.reshape(xx.shape)
    out = ax.contourf(xx, yy, Z, **params)
    return out


# we create an instance of SVM and fit out data.
C = 1.0  # SVM regularization parameter
models = (svm.SVC(kernel='linear', C=C, decision_function_shape='ovo'),
          svm.SVC(kernel='sigmoid', C=C, decision_function_shape='ovo'),
          svm.SVC(kernel='rbf', gamma=0.7, C=C, decision_function_shape='ovo'),
          svm.SVC(kernel='poly', degree=3, gamma='auto', C=C, decision_function_shape='ovo'))
models = (clf.fit(X, y) for clf in models)

# title for the plots
titles = ('SVC with linear kernel',
          'SVC with sigmoid kernel',
          'SVC with RBF kernel',
          'SVC with polynomial (degree 3) kernel')

# Set-up 2x2 grid for plotting.
fig, sub = plt.subplots(2, 2)
# plt.subplots_adjust(wspace=0.4, hspace=0.4)

X0, X1 = X[:, 0], X[:, 1]
xx, yy = make_meshgrid(X0, X1)

for clf, title, ax in zip(models, titles, sub.flatten()):
    plot_contours(ax, clf, xx, yy, cmap=cm, alpha=0.7)
    ax.scatter(X0, X1, c=y, cmap=cm, s=20, edgecolors='k')
    ax.set_xlim(xx.min(), xx.max())
    ax.set_ylim(yy.min(), yy.max())
    ax.set_xticks(())
    ax.set_yticks(())
    ax.set_title(title)

plt.show()

如果想要强制执行这样的颜色,则必须将级别指定为类似列表的对象。级别列表应包含带有 类 边界的 $n+1$ 个条目,其中 $n$ 是 类 的数量。因此 类 等于 range(len(use)) 这应该是 [i - .5 for i in range(len(use) + 1)],因此可以使用以下内容来获得所需的输出:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
from sklearn import svm, preprocessing


# create dummy data
use = (
        ((1,9),(.2,.1),'b'),
        ((4,0),(.2,.4),'r'),
        ((1,5),(.2,.3),'g'),
        ((6,3),(.3,.2),'m'),
        ((5,6),(.1,.2),'c'),
        ((4,2),(.1,.1),'xkcd:orange'),
        ((5,4),(.3,.2),'xkcd:peach'),
        ((3,1),(.1,.4),'xkcd:bright pink'),
        ((2,1),(.2,.1),'xkcd:crimson'),
      )
sx = [np.random.normal(loc=u[0][0], scale=u[1][0], size=(20,)) for u in use]
sy = [np.random.normal(loc=u[0][1], scale=u[1][1], size=(20,)) for u in use]
X = np.array([[ix[i], iy[i]] for ix, iy in zip(sx, sy) for i in range(20)])
y = np.array([i for i in range(len(use)) for n in range(20)])


# scale the data
Scaler = preprocessing.StandardScaler().fit(X)
X = Scaler.transform(X)


# color map
cm = LinearSegmentedColormap.from_list('use', [u[2] for u in use], N=len(use))


def make_meshgrid(x, y, h=.02):
    """Create a mesh of points to plot in

    Parameters
    ----------
    x: data to base x-axis meshgrid on
    y: data to base y-axis meshgrid on
    h: stepsize for meshgrid, optional

    Returns
    -------
    xx, yy : ndarray
    """
    x_min, x_max = x.min() - 1, x.max() + 1
    y_min, y_max = y.min() - 1, y.max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))
    return xx, yy


def plot_contours(ax, clf, xx, yy, **params):
    """Plot the decision boundaries for a classifier.

    Parameters
    ----------
    ax: matplotlib axes object
    clf: a classifier
    xx: meshgrid ndarray
    yy: meshgrid ndarray
    params: dictionary of params to pass to contourf, optional
    """
    Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
    Z = Z.reshape(xx.shape)
    out = ax.contourf(xx, yy, Z, **params)
    return out


# we create an instance of SVM and fit out data.
C = 1.0  # SVM regularization parameter
models = (svm.SVC(kernel='linear', C=C, decision_function_shape='ovo'),
          svm.SVC(kernel='sigmoid', C=C, decision_function_shape='ovo'),
          svm.SVC(kernel='rbf', gamma=0.7, C=C, decision_function_shape='ovo'),
          svm.SVC(kernel='poly', degree=3, gamma='auto', C=C, decision_function_shape='ovo'))
models = (clf.fit(X, y) for clf in models)

# title for the plots
titles = ('SVC with linear kernel',
          'SVC with sigmoid kernel',
          'SVC with RBF kernel',
          'SVC with polynomial (degree 3) kernel')

# Set-up 2x2 grid for plotting.
fig, sub = plt.subplots(2, 2)
# plt.subplots_adjust(wspace=0.4, hspace=0.4)

X0, X1 = X[:, 0], X[:, 1]
xx, yy = make_meshgrid(X0, X1)

for clf, title, ax in zip(models, titles, sub.flatten()):
    plot_contours(ax, clf, xx, yy,
                  cmap=cm, alpha=0.7,
                  levels=[i - .5 for i in range(len(use) + 1)])
    ax.scatter(X0, X1, c=y, cmap=cm, s=20, edgecolors='k')
    ax.set_xlim(xx.min(), xx.max())
    ax.set_ylim(yy.min(), yy.max())
    ax.set_xticks(())
    ax.set_yticks(())
    ax.set_title(title)

plt.show()