Nilearn sklearn classification problem/error

Hi

I adapted the SVM tutorial in nilearn (https://nilearn.github.io/auto_examples/plot_decoding_tutorial.html#sphx-glr-auto-examples-plot-decoding-tutorial-py) to my data.

After step 8.1.4.4.1:
coef_ = svc.coef_
print(coef_)
print(coef_.shape)

I get the error message: “AttributeError: ‘SVC’ object has no attribute ‘dual_coef_’”

However, sklearn.svm.svc should return the “coef_” attribute (see https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html#sklearn.svm.SVC)

I’ve also updated the sklearn version without success…

thanks for help!
mike

Are you sure that the svc you use has been trained already ?

Also, what happens when you run the python script (plot_decoding_tutorial) at the bottom of the page ?

HTH.Best,
Bertrand

@Mike_Myers What is your Python version? If it is 3.6 I suggest downgrading to 3.4 and re-running the code. There seems to be some compatibility issues with sklearn svm module in 3.6. Hope this helps :slight_smile:.

Hi Bertrand

Yes, model is trained as I can get the cv_scores. Due to some reason, I can’t get svc.coef_ which makes its impossible to plot as I have first to inverse it with masker.

Thanks Sanjula. I have version 3.7.6.
Scikit-learn from 0.23 requires Python 3.6 or greater…

It seems to be a nightmare to downgrade my Python version to 3.4…I use Anaconda. I’ll keep trying…

No, please don’t downgrade.
If you haven’t run explicitly
svc.fit(fmri_masked[:-30], conditions[:-30])
the model has not been fit, hence the error.
Simply running
cv_score = cross_val_score(svc, fmri_masked, conditions)
is not enough.

2 Likes

That’s it. Many thanks Bertrand. I somehow missed this line of code (which is indeed important :))

1 Like

Great to hear it’s been resolved @Mike_Myers! I assumed you already ran the svc.fit() function, hence the suggestion to downgrade python. Thanks @bthirion for the solution!

1 Like