2010-11-24 21:08:17 +08:00
|
|
|
"""
|
|
|
|
|
=====================
|
|
|
|
|
SVM: Weighted samples
|
|
|
|
|
=====================
|
|
|
|
|
|
|
|
|
|
Plot decision function of a weighted dataset, where the size of points
|
|
|
|
|
is proportional to its weight.
|
|
|
|
|
"""
|
|
|
|
|
print __doc__
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
import pylab as pl
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn import svm
|
2010-11-24 21:08:17 +08:00
|
|
|
|
|
|
|
|
# we create 20 points
|
|
|
|
|
np.random.seed(0)
|
2010-11-28 07:21:05 +08:00
|
|
|
X = np.r_[np.random.randn(10, 2) + [1, 1], np.random.randn(10, 2)]
|
2011-12-17 05:55:42 +08:00
|
|
|
Y = [1] * 10 + [-1] * 10
|
2010-11-24 21:08:17 +08:00
|
|
|
sample_weight = 100 * np.abs(np.random.randn(20))
|
|
|
|
|
# and assign a bigger weight to the last 10 samples
|
|
|
|
|
sample_weight[:10] *= 10
|
|
|
|
|
|
|
|
|
|
# # fit the model
|
|
|
|
|
clf = svm.SVC()
|
|
|
|
|
clf.fit(X, Y, sample_weight=sample_weight)
|
|
|
|
|
|
2010-11-29 19:30:45 +08:00
|
|
|
# plot the decision function
|
2010-11-24 21:08:17 +08:00
|
|
|
xx, yy = np.meshgrid(np.linspace(-4, 5, 500), np.linspace(-4, 5, 500))
|
|
|
|
|
|
|
|
|
|
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
|
|
|
|
|
Z = Z.reshape(xx.shape)
|
|
|
|
|
|
|
|
|
|
# plot the line, the points, and the nearest vectors to the plane
|
2012-05-21 16:41:11 +08:00
|
|
|
pl.contourf(xx, yy, Z, alpha=0.75, cmap=pl.cm.bone)
|
2012-05-06 02:24:55 +08:00
|
|
|
pl.scatter(X[:, 0], X[:, 1], c=Y, s=sample_weight, alpha=0.9, cmap=pl.cm.bone)
|
2010-11-24 21:08:17 +08:00
|
|
|
|
2010-11-29 19:30:45 +08:00
|
|
|
pl.axis('off')
|
2010-11-24 21:08:17 +08:00
|
|
|
pl.show()
|