I am using sklearn to fit a SVM to some data. Since I wanted to use cross-validation and evaluate my classification accuracy using permutations, I am using the permutation_test_score() function (https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.permutation_test_score.html#sklearn.model_selection.permutation_test_score)

I have implemented my SVM like this:

svc = SVC(kernel='linear', verbose=True)
nr_perm = 100
cv = StratifiedKFold(n_splits=5)
score, pscores, pvalue = permutation_test_score(svc, X, y, scoring="accuracy", cv=cv, n_permutations=nr_perm)

According to the documentation, this function fits and evaluates the model using cross-validation. However, even after using permutation_test_score I cannot use coef_ to get the weights of the features of my model. I get the following error message:

AttributeError: 'SVC' object has no attribute 'dual_coef_'

If I use svc.fit(X, y), I can then use svc.coef_ to access them:

svc = SVC(kernel='linear', verbose=True)
coefs = svc.coef_

I have encountered the same problem when using other functions from sklearn.model_selection.

Does anyone know how I can access the feature weights?


When you call a model with model.fit(X,y), you can get its weights because they're fitted on a single dataset. However, in cross validation or permutation tests, the same model is fitted several times using portions (or modified versions) of the training data and several models are outputted. So, the weights you seek for is not unique, which explains the lack of weights for your call.

  • $\begingroup$ Yeah, that's what I was expecting. I have always used the feature weights for nice visualisation though, do you know of an alternative? $\endgroup$ – Max Sep 3 '20 at 9:41
  • 1
    $\begingroup$ You can plot an image of trials x feature weights, i.e. $T\times F$. But, you need to get the weights for each run first by either writing a derived Model class for SVC, and save the weights (e.g. to files) after each fit operation or doing the permutation test yourself. $\endgroup$ – gunes Sep 3 '20 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.