python - 超参数调整

标签 python machine-learning scikit-learn cross-validation hyperparameters

我目前正在自己​​做一个项目。对于这个项目,我尝试比较多种算法的结果。 但我想确保测试的每个算法都配置为给出最佳结果。

所以我使用交叉验证来测试参数的每个组合并选择最佳的。

例如:

def KMeanstest(param_grid, n_jobs): 

    estimator = KMeans()

    cv = ShuffleSplit(n_splits=10, test_size=0.2, random_state=42)

    regressor = GridSearchCV(estimator=estimator, cv=cv, param_grid=param_grid, n_jobs=n_jobs) 

    regressor.fit(X_train, y_train) 

    print("Best Estimator learned through GridSearch") 
    print(regressor.best_estimator_)

    return cv, regressor.best_estimator_

param_grid={'n_clusters': [2], 
            'init': ['k-means++', 'random'],
            'max_iter': [100, 200, 300, 400, 500],
            'n_init': [8, 9, 10, 11, 12, 13, 14, 15, 16], 
            'tol': [1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6], 
            'precompute_distances': ['auto', True, False], 
            'random_state': [42],
            'copy_x': [True, False],
            'n_jobs': [-1],
            'algorithm': ['auto', 'full', 'elkan']
           }

n_jobs=-1

cv,best_est=KMeanstest(param_grid, n_jobs)

但这非常耗时。 我想知道这种方法是否是最好的,或者我是否需要使用不同的方法。

感谢您的帮助

最佳答案

正如您所说,GridSearch 的问题是它非常耗时。有时,随机搜索是一个不错的选择,但它并不是最佳选择。

贝叶斯优化是另一种选择。这使我们能够使用概率方法快速确定最佳参数集。我亲自尝试过使用 hyperopt python 中的库,它运行得非常好。看看这个tutorial了解更多信息。您也可以从我的GitHub下载相关笔记本

好处是,由于您已经尝试过 GridSearch,因此您可以大致了解哪些参数范围效果不佳。因此,您可以为贝叶斯优化运行定义更准确的搜索空间,这将进一步减少时间。此外,hyperopt 可用于比较多种算法及其各自的参数。

关于python - 超参数调整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60071585/

相关文章:

python - Python 中的正则表达式替换

python - 蒙版张量损失

python - 如何在python中对数字进行一次热编码?

python - 在 python 中使用 k 均值聚类提取质心?

python - 将列值分配给 pandas 数据框中的唯一行

Python:从列表中删除逗号,以便我可以使用 pandas 将数据导入到 Excel 中的单独单元格中

amazon-web-services - 此数据文件中的架构必须与数据源 : Amazon Machine Learning 匹配

python - 导入错误: cannot import name BayesianGaussianMixture

python - 如果左右 df 的键不同,pandas merge 会做奇怪的工作

c - fminunc如何优化梯度下降中的学习率(步长比例)值?