为什么随机搜索重新返回线性估计的度或伽马值?

2024-09-28 05:27:00 发布

您现在位置:Python中文网/ 问答频道 /正文

对于Sklearn,我使用的是随机化搜索CV,在特定情况下,最佳估计值为:

SVR(C=1594.0828461797396, degree=0.8284528822863231, gamma=1.1891370222133257,kernel='linear')

但是根据sklearn documentationdegreegamma只用于rbfpoly内核。 为什么我得到带有gammadegree值的linar估计量

from sklearn.model_selection import RandomizedSearchCV
from scipy.stats import expon, reciprocal

param_distribs = {
        'kernel': ['linear', 'rbf','poly','sigmoid'],
        'C': reciprocal(20, 200000),
        'gamma': expon(scale=1.0),
        'degree': expon(scale=1.0),
    }

svm_reg = SVR()
rnd_search = RandomizedSearchCV(svm_reg, param_distributions=param_distribs,
                                n_iter=50, cv=5, scoring='neg_mean_squared_error',
                                verbose=2, random_state=42)
rnd_search.fit(X, y)

Tags: fromimportparamsklearnkernellineargammapoly
1条回答
网友
1楼 · 发布于 2024-09-28 05:27:00

RandomizedSearchCV将始终随机设置估计器的所有指定参数,而不考虑此类限制,因为没有实现内部方法来检查哪些组合对特定估计器有意义。由于gammadegreelinear内核一起被忽略,因此它也不会引发错误,并且算法每次运行时都会设置所有参数

如果要避免此类行为,可以将参数网格作为字典列表传递,指定允许哪些组合。documentation为此类情况指定:

If a list of dicts is given, first a dict is sampled uniformly, and then a parameter is sampled using that dict as above.

例如,假设您将以下内容定义为参数网格:

param_distribs = [
    {
        'kernel': ['rbf','poly'],
        'C': reciprocal(20, 200000),
        'gamma': expon(scale=1.0),
        'degree': expon(scale=1.0)
    },
    {
        'kernel': ['linear','sigmoid'],
        'C': reciprocal(20, 200000)
    }
]

这将避免RandomizedSearchCV在迭代中选择带有linear内核的字典时设置gammadegree。相反,如果它在特定迭代中选择另一个字典,它也将设置gammadegree

相关问题 更多 >

    热门问题