在 scikit-learn 中使用 RandomizedSearchCV 有条件地调整超参数
Conditional tuning of hyperparameters with RandomizedSearchCV in scikit-learn
我想在 sklearn 中使用 RandomizedSearchCV 在我的数据集上搜索支持向量分类器的最佳超参数值。我正在优化的超参数是 "kernel"、"C" 和 "gamma"。但是,在 "poly" 核的情况下,我还想优化第四个超参数 "degree"(多项式核函数的索引)。
我意识到,由于当内核不是 "poly" 时度数超参数被忽略,我可以将度数包含在我提供给 RandomizedSearchCV 的参数字典中(正如我在下面的代码中所做的那样)。然而,理想情况下,我想在非多边形核加上每个多边形核的度数上均匀搜索,即我想在例如[(kernel="linear"), (kernel="rbf"), (kernel="poly", degree=2), (kernel="poly", degree=3)]。因此,我想知道是否可以有条件地引入超参数进行调优,即如果 kernel="poly" degree=np.linspace(2, 5, 4),否则 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
)
我不确定您是否可以为 gridsearch 或在 gridsearch 内创建条件参数(感觉它是一个有用的功能)。但是,解决此问题的一种解决方案是简单地设置 randomizesearchcv
的所有超参数并使用 errors_raise
参数,这将允许您通过通常会失败的迭代并停止您的过程。像这样:
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,errors_raise=0)
然而
从 sklearn's SVC documentation 开始你应该不会有任何问题通过 degree
:
degree : int, optional (default=3)
Degree of the polynomial kernel function (‘poly’). Ignored by all other kernels.
遗憾的是,GridsearchCV and RandomizedSearchCV不支持超参数的条件调整。
Hyperopt supports conditional tuning of hyperparameters, check this 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])
}
我想在 sklearn 中使用 RandomizedSearchCV 在我的数据集上搜索支持向量分类器的最佳超参数值。我正在优化的超参数是 "kernel"、"C" 和 "gamma"。但是,在 "poly" 核的情况下,我还想优化第四个超参数 "degree"(多项式核函数的索引)。
我意识到,由于当内核不是 "poly" 时度数超参数被忽略,我可以将度数包含在我提供给 RandomizedSearchCV 的参数字典中(正如我在下面的代码中所做的那样)。然而,理想情况下,我想在非多边形核加上每个多边形核的度数上均匀搜索,即我想在例如[(kernel="linear"), (kernel="rbf"), (kernel="poly", degree=2), (kernel="poly", degree=3)]。因此,我想知道是否可以有条件地引入超参数进行调优,即如果 kernel="poly" degree=np.linspace(2, 5, 4),否则 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
)
我不确定您是否可以为 gridsearch 或在 gridsearch 内创建条件参数(感觉它是一个有用的功能)。但是,解决此问题的一种解决方案是简单地设置 randomizesearchcv
的所有超参数并使用 errors_raise
参数,这将允许您通过通常会失败的迭代并停止您的过程。像这样:
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,errors_raise=0)
然而
从 sklearn's SVC documentation 开始你应该不会有任何问题通过 degree
:
degree : int, optional (default=3) Degree of the polynomial kernel function (‘poly’). Ignored by all other kernels.
遗憾的是,GridsearchCV and RandomizedSearchCV不支持超参数的条件调整。
Hyperopt supports conditional tuning of hyperparameters, check this 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])
}