gpt4 book ai didi

python - 在 scikit-learn 中使用 RandomizedSearchCV 有条件调整超参数

转载 作者:太空宇宙 更新时间:2023-11-03 20:20:51 24 4
gpt4 key购买 nike

我想在 sklearn 中使用 RandomizedSearchCV 来搜索数据集上支持向量分类器的最佳超参数值。我正在优化的超参数是“kernel”、“C”和“gamma”。然而,在“poly”内核的情况下,我还想优化第四个超参数“度”(多项式内核函数的索引)。

我意识到,由于当内核不是“poly”时,度超参数被忽略,因此我可以将度包含在我提供给 RandomizedSearchCV 的参数字典中(正如我在下面的代码中所做的那样)。然而,理想情况下,我想在非多边形内核加上每个程度的多边形内核上均匀搜索,即我想在例如[(内核=“线性”),(内核=“rbf”),(内核=“聚”,度= 2),(内核=“聚”,度= 3)]。因此,我想知道是否可以有条件地引入一个超参数进行调整,即 if kernel="poly"Degree=np.linspace(2, 5, 4), else Degree=0。

我在 RandomizedSearchCV 文档中找不到这样的示例,因此想知道这里是否有人遇到过同样的问题并且能够提供帮助。谢谢!

from sklearn.svm import SVC
from sklearn.model_selection import RandomizedSearchCV
from sklearn.model_selection import StratifiedKFold

clf = SVC()
params = {'kernel': ['linear', 'poly', 'rbf', 'sigmoid'],
'degree': np.linspace(2, 5, 4),
'C': np.logspace(-3, 5, 17),
'gamma': np.logspace(-3, 5, 17)}

random_search = RandomizedSearchCV(
estimator=clf, param_distributions=params, n_iter=200, n_jobs=-1,
cv=StratifiedKFold(n_splits=5), iid=False
)

最佳答案

不幸的是,GridsearchCVRandomizedSearchCV不支持条件调整超参数。

Hyperopt支持超参数的条件调整,检查这个wiki了解更多详情。

示例:

space4svm = {
'C': hp.uniform('C', 0, 20),
'kernel': hp.choice('kernel', [
{'ktype': 'linear'},
{'ktype': 'poly', 'degree': hp.lognormal('degree', 0, 1)},
]),
'gamma': hp.uniform('gamma', 0, 20),
'scale': hp.choice('scale', [0, 1]),
'normalize': hp.choice('normalize', [0, 1])
}

关于python - 在 scikit-learn 中使用 RandomizedSearchCV 有条件调整超参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58168297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com