Skip to content

Instantly share code, notes, and snippets.

@whiledoing
Last active February 1, 2020 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whiledoing/d977331667610bced39983c704d44a85 to your computer and use it in GitHub Desktop.
Save whiledoing/d977331667610bced39983c704d44a85 to your computer and use it in GitHub Desktop.
[sklearn-snippets] skl-learn snippets #python #sklearn
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets as widget
from sklearn.datasets import make_blobs
from sklearn.svm import SVC
def plot_svc_decision_function(model, ax=None, plot_support=True):
"""Plot the decision function for a 2D SVC"""
ax = ax or plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# create grid to evaluate model
x = np.linspace(xlim[0], xlim[1], 30)
y = np.linspace(ylim[0], ylim[1], 30)
Y, X = np.meshgrid(y, x)
# vstack将数据垂直放置,类似于append,在T一下,得到的数据类似
# xy = np.array([(i, j) for i in x for j in y])
xy = np.vstack([X.ravel(), Y.ravel()]).T
# contour的计算需要P转化为row*col的格式,所以还需要reshape回来
P = model.decision_function(xy).reshape(X.shape)
# plot decision boundary and margins
ax.contour(X, Y, P, colors='k',
levels=[-1, 0, 1], alpha=0.5,
linestyles=['--', '-', '--'])
# plot support vectors
# support_vectors_中记录的是SVM训练得到的分界点
# facecolors记录点的中心颜色,linecolors记录点的边界颜色,这里只保持边界颜色
if plot_support:
ax.scatter(model.support_vectors_[:, 0],
model.support_vectors_[:, 1],
s=300, linewidth=1, facecolors='none');
ax.set_xlim(xlim)
ax.set_ylim(ylim)
# @note 神器,ipywidgets的interact可以提供简单的UI空间,进而控制计算过程
# 这里可以根据[10, 200, 10]选择数据来看不同的N情况下,decision boundary的变化情况
@widget.interact(N=(10, 200, 10), ax=widget.fixed(None))
def plot_svm(N=10, ax=None):
X, y = make_blobs(n_samples=200, centers=2,
random_state=0, cluster_std=0.60)
X = X[:N]
y = y[:N]
model = SVC(kernel='linear', C=1E10)
model.fit(X, y)
ax = ax or plt.gca()
ax.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
ax.set_xlim(-1, 4)
ax.set_ylim(-1, 6)
plot_svc_decision_function(model, ax)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment