SGD: العينات المرجحة#

رسم دالة القرار لمجموعة بيانات مرجحة، حيث يتناسب حجم النقاط مع وزنها.

plot sgd weighted samples
# المؤلفون: مطوري سكايلرن
# معرف الترخيص: BSD-3-Clause

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))
# ونعطي وزناً أكبر للعينات العشر الأخيرة
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]],
    ["بدون أوزان", "مع الأوزان"],
    loc="lower left",
)

ax.set(xticks=(), yticks=())
plt.show()

Total running time of the script: (0 minutes 0.077 seconds)

Related examples

SVM: العينات ذات الأوزان

SVM: العينات ذات الأوزان

تمرين SVM

تمرين SVM

توضيح تصنيف العملية الغاوسية (GPC) على مجموعة بيانات XOR

توضيح تصنيف العملية الغاوسية (GPC) على مجموعة بيانات XOR

مثال على هوامش SVM

مثال على هوامش SVM

Gallery generated by Sphinx-Gallery