Using sample_weight in GridSearchCV

python, scikit-learn

Solution

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)

Problem

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}) ```

Original source

Related problems