为什么使用 SVC 作为基本估计量的 n_estimators=1 的 AdaBoost 与仅使用 SVC 的预测结果存在差异
Why's there a difference in prediction result between AdaBoost with n_estimators=1 that uses SVC as a base estimator, and just SVC
我目前正在使用日常财务数据来拟合我的 SVM 和 AdaBoost。为了检查我的结果,我尝试了 n_estimators=1 的 AdaBoost,这样它会 return 与我 运行 单个 SVM 的结果相同。
from sklearn.ensemble import AdaBoostClassifier
from sklearn.svm import SVC
svm2 = SVC(C=box_const, kernel='rbf', degree=3, gamma='scale', coef0=0.0,
shrinking=True, tol=0.001, cache_size=1000, class_weight='balanced',
verbose=False, max_iter=-1, decision_function_shape='ovr', probability=True)
model2 = AdaBoostClassifier(base_estimator=svm2,
n_estimators=1,
algorithm='SAMME.R')
model2.fit(X_train, y_train)
svm2.fit(X_train, y_train)
然而,相反,我发现即使我设置n_estimators=1,他们也产生了不同的预测结果。我做错了什么吗?或者这个结果有特定的原因吗?
>>> model2.predict(X_test)
array([1., 1., 1., 1., 1.])
>>> model2.base_estimator
SVC(C=1, cache_size=1000, class_weight='balanced', probability=True)
>>> svm2.predict(X_test)
array([0., 1., 1., 0., 0.])
>>> svm2
SVC(C=1, cache_size=1000, class_weight='balanced', probability=True)
[编辑]
我发现我将 sample_weight 添加到 scikit learn 的 SVC 的方式存在显着差异。
当我这样定义我的模型时
svm2 = SVC(C=box_const, kernel='rbf', degree=3, gamma='scale', coef0=0.0,
shrinking=True, tol=0.001, cache_size=1000, class_weight='balanced',
verbose=False, max_iter=-1, decision_function_shape='ovr', probability=True)
这两个预测结果相同
svm2.fit(X, y, sample_weight=[1] * len(X))
svm2.fit(X, y)
而
svm2.fit(X, y, sample_weight=[1 / len(X)] * len(X))
产生不同的结果。我认为由于 AdaBoost 初始化样本权重为 1 / len(X),所以会出现这种问题。我在向 SVM 中插入样本权重时做错了什么吗?
你没有做错任何事。每次 运行 分类器都会设置一个新的随机状态。要解决此问题,只需将 random_state
参数设置为您喜欢的任何值即可。
例如:
AdaBoostClassifier(base_estimator=svm2,
n_estimators=1,
algorithm='SAMME.R',
random_state=21
)
更多详情请查看官方documentation
几年前 stats.SE 回答了这个问题:
Sample weights scaling in sklearn.svm.SVC
另见 the documentation:
sample_weight : array-like of shape (n_samples,), default=None
Per-sample weights. Rescale C per sample. Higher weights force the classifier to put more emphasis on these points.
总而言之,通过修改正则化参数C
将样本权重纳入sklearn中的SVM算法。您可以通过增加基础 SVC
.
中的参数 C
来补偿 AdaBoost
我目前正在使用日常财务数据来拟合我的 SVM 和 AdaBoost。为了检查我的结果,我尝试了 n_estimators=1 的 AdaBoost,这样它会 return 与我 运行 单个 SVM 的结果相同。
from sklearn.ensemble import AdaBoostClassifier
from sklearn.svm import SVC
svm2 = SVC(C=box_const, kernel='rbf', degree=3, gamma='scale', coef0=0.0,
shrinking=True, tol=0.001, cache_size=1000, class_weight='balanced',
verbose=False, max_iter=-1, decision_function_shape='ovr', probability=True)
model2 = AdaBoostClassifier(base_estimator=svm2,
n_estimators=1,
algorithm='SAMME.R')
model2.fit(X_train, y_train)
svm2.fit(X_train, y_train)
然而,相反,我发现即使我设置n_estimators=1,他们也产生了不同的预测结果。我做错了什么吗?或者这个结果有特定的原因吗?
>>> model2.predict(X_test)
array([1., 1., 1., 1., 1.])
>>> model2.base_estimator
SVC(C=1, cache_size=1000, class_weight='balanced', probability=True)
>>> svm2.predict(X_test)
array([0., 1., 1., 0., 0.])
>>> svm2
SVC(C=1, cache_size=1000, class_weight='balanced', probability=True)
[编辑] 我发现我将 sample_weight 添加到 scikit learn 的 SVC 的方式存在显着差异。
当我这样定义我的模型时
svm2 = SVC(C=box_const, kernel='rbf', degree=3, gamma='scale', coef0=0.0,
shrinking=True, tol=0.001, cache_size=1000, class_weight='balanced',
verbose=False, max_iter=-1, decision_function_shape='ovr', probability=True)
这两个预测结果相同
svm2.fit(X, y, sample_weight=[1] * len(X))
svm2.fit(X, y)
而
svm2.fit(X, y, sample_weight=[1 / len(X)] * len(X))
产生不同的结果。我认为由于 AdaBoost 初始化样本权重为 1 / len(X),所以会出现这种问题。我在向 SVM 中插入样本权重时做错了什么吗?
你没有做错任何事。每次 运行 分类器都会设置一个新的随机状态。要解决此问题,只需将 random_state
参数设置为您喜欢的任何值即可。
例如:
AdaBoostClassifier(base_estimator=svm2,
n_estimators=1,
algorithm='SAMME.R',
random_state=21
)
更多详情请查看官方documentation
几年前 stats.SE 回答了这个问题:
Sample weights scaling in sklearn.svm.SVC
另见 the documentation:
sample_weight : array-like of shape (n_samples,), default=None
Per-sample weights. Rescale C per sample. Higher weights force the classifier to put more emphasis on these points.
总而言之,通过修改正则化参数C
将样本权重纳入sklearn中的SVM算法。您可以通过增加基础 SVC
.
C
来补偿 AdaBoost