Skip forbidden parameter combinations when using GridSearchCV

I want to eagerly search the entire parameter space of my reference vector classifier using GridSearchCV . However, certain combinations of parameters prohibited LinearSVC and issue an exception . In particular, there are mutually exclusive combinations of parameters dual, penaltyand loss:

For example, this code:

from sklearn import svm, datasets
from sklearn.model_selection import GridSearchCV

iris = datasets.load_iris()
parameters = {'dual':[True, False], 'penalty' : ['l1', 'l2'], \
              'loss': ['hinge', 'squared_hinge']}
svc = svm.LinearSVC()
clf = GridSearchCV(svc, parameters)
clf.fit(iris.data, iris.target)

Returns ValueError: Unsupported set of arguments: The combination of penalty='l2' and loss='hinge' are not supported when dual=False, Parameters: penalty='l2', loss='hinge', dual=False

My question is: is it possible to get GridSearchCV to skip combinations of parameters that are prohibited by the model? If not, is there an easy way to build a parameter space that will not break the rules?

+6
2

, error_score=0.0 GridSearchCV:

error_score: ' ( )

. "", . , FitFailedWarning . refit, .

+12

( ), . GridSearchCV , , .

, - :

from sklearn import svm, datasets
from sklearn.model_selection import GridSearchCV
from itertools import product

iris = datasets.load_iris()

duals = [True, False]
penaltys = ['l1', 'l2']
losses = ['hinge', 'squared_hinge']
all_params = list(product(duals, penaltys, losses))
filtered_params = [{'dual': [dual], 'penalty' : [penalty], 'loss': [loss]}
                   for dual, penalty, loss in all_params
                   if not (penalty == 'l1' and loss == 'hinge') 
                   and not ((penalty == 'l1' and loss == 'squared_hinge' and dual is True))
                  and not ((penalty == 'l2' and loss == 'hinge' and dual is False))]

svc = svm.LinearSVC()
clf = GridSearchCV(svc, filtered_params)
clf.fit(iris.data, iris.target)
0

Source: https://habr.com/ru/post/1584956/


All Articles