在 GridSearchCV 中使用 sample_weight [英] Using sample_weight in GridSearchCV
问题描述
是否可以执行 GridSearchCV
(以获得最佳 SVM 的 C)并使用 scikit-learn 指定 sample_weight
?
这是我的代码和我遇到的错误:
gs = GridSearchCV(支持向量机.SVC(C=1),[{'内核':['线性'],'C': [.1, 1, 10],'概率':[真],'sample_weight':sw_train,}])gs.fit(Xtrain, ytrain)
<块引用>
>>ValueError:估计器 SVC 的参数 sample_weight 无效
<小时>
我通过获取最新的 scikit-learn 版本并使用以下内容解决了这个问题:
gs.fit(Xtrain, ytrain, fit_params={'sample_weight': sw_train})
只是想解决这个悬而未决的问题...
您需要获取最新版本的 SKL 并使用以下内容:
gs.fit(Xtrain, ytrain, fit_params={'sample_weight': sw_train})
但是,将fit_params
传递给构造函数更符合文档:
gs = GridSearchCV(svm.SVC(C=1), [{'kernel': ['linear'], 'C': [.1, 1, 10], 'probability': [True], 'sample_weight': sw_train}], fit_params={'sample_weight': sw_train})gs.fit(Xtrain, ytrain)
Is it possible to perform a GridSearchCV
(to get the best SVM's C) and yet specify the sample_weight
with scikit-learn?
Here's my code and the error I'm confronted to:
gs = GridSearchCV(
svm.SVC(C=1),
[{
'kernel': ['linear'],
'C': [.1, 1, 10],
'probability': [True],
'sample_weight': sw_train,
}]
)
gs.fit(Xtrain, ytrain)
>> ValueError: Invalid parameter sample_weight for estimator SVC
Edit: I solved the issue by getting the latest scikit-learn version and using the following:
gs.fit(Xtrain, ytrain, fit_params={'sample_weight': sw_train})
Just trying to close out this long hanging question...
You needed to get the last version of SKL and use the following:
gs.fit(Xtrain, ytrain, fit_params={'sample_weight': sw_train})
However, it is more in line with the documentation to pass fit_params
to the constructor:
gs = GridSearchCV(svm.SVC(C=1), [{'kernel': ['linear'], 'C': [.1, 1, 10], 'probability': [True], 'sample_weight': sw_train}], fit_params={'sample_weight': sw_train})
gs.fit(Xtrain, ytrain)
这篇关于在 GridSearchCV 中使用 sample_weight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!