简体   繁体   中英

Object has no attribute in scikit-learn, how can I access it?

I would like to use different parameters of scikit's SVC classifier with cross-vlidation , so I tried the following:

Then, let's use SVC algorithm:

from sklearn import svm
print('Support vector machine(SVM):   {:.2f}'.format(metrics.accuracy_score(
            y, stratified_cv(X, y, svm.SVC(kernel='linear')))))

But it seems I can not access to the object:

AttributeError                            Traceback (most recent call last)
<ipython-input-16-dacd8d429376> in <module>()
      5 
      6 print('Support vector machine(SVM):   {:.2f}'.format(metrics.accuracy_score(
----> 7             y, stratified_cv(X, y, svm.SVC(kernel='linear')))))
      8 

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

Interestingly, when I try this:

print('Support vector machine(SVM):   {:.2f}'.format(metrics.accuracy_score(
            y, stratified_cv(X, y, svm.SVC))))

I get:

Support vector machine(SVM):   0.46

What could be happening?...any idea of given the above cross validation strategy, how to set up my own SVM configuration?. Thanks in advance guys!

You need a partial from python. In general, your function requires you to pass something that can be called with clf_class(**kwargs) , so if you pass a particular object (obtained through clf = SVC(kernel='linear') ) it won't work, as you try to do

SVC(kernel='linear')(**kwargs) # error!

you want to call

SVC(kernel='linear', **kwargs)

so you can declare the partial function in python

from functools import partial
linear_svm = partial(svm.SVC, kernel='linear')

and now you can call

linear_svm(**kwargs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM