Note
Go to the end to download the full example code. or to run this example in your browser via Binder
随机梯度下降:加权样本#
绘制加权数据集的决策函数,其中点的大小与其权重成正比。
import matplotlib.pyplot as plt
import numpy as np
from sklearn import linear_model
# 我们创建了20个点
np.random.seed(0)
X = np.r_[np.random.randn(10, 2) + [1, 1], np.random.randn(10, 2)]
y = [1] * 10 + [-1] * 10
sample_weight = 100 * np.abs(np.random.randn(20))
# 并且给最后10个样本分配更大的权重
sample_weight[:10] *= 10
# 绘制加权数据点
xx, yy = np.meshgrid(np.linspace(-4, 5, 500), np.linspace(-4, 5, 500))
fig, ax = plt.subplots()
ax.scatter(
X[:, 0],
X[:, 1],
c=y,
s=sample_weight,
alpha=0.9,
cmap=plt.cm.bone,
edgecolor="black",
)
# 拟合无权重模型
clf = linear_model.SGDClassifier(alpha=0.01, max_iter=100)
clf.fit(X, y)
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
no_weights = ax.contour(xx, yy, Z, levels=[0], linestyles=["solid"])
# 拟合加权模型
clf = linear_model.SGDClassifier(alpha=0.01, max_iter=100)
clf.fit(X, y, sample_weight=sample_weight)
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
samples_weights = ax.contour(xx, yy, Z, levels=[0], linestyles=["dashed"])
no_weights_handles, _ = no_weights.legend_elements()
weights_handles, _ = samples_weights.legend_elements()
ax.legend(
[no_weights_handles[0], weights_handles[0]],
["no weights", "with weights"],
loc="lower left",
)
ax.set(xticks=(), yticks=())
plt.show()
Total running time of the script: (0 minutes 0.054 seconds)
Related examples
SVM:加权样本
使用局部离群因子(LOF)进行新颖性检测
在 XOR 数据集上展示高斯过程分类 (GPC)
sphx_glr_auto_examples_exercises_plot_iris_exercise.py
SVM 练习