我目前正在自己做一个项目。对于这个项目,我试图比较多种算法的结果。 但是我想确保测试的每个算法都配置为提供最佳结果。
因此,我使用交叉验证并测试参数的每个组合并选择最佳参数。
例如:
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)
但这是非常耗时的时间。 我想知道这种方法是最好的还是需要使用其他方法。
谢谢您的帮助
答案 0 :(得分:3)
除了“随机搜索”和“网格搜索”外,还有一些工具和库可用于更智能的超参数调整。我成功地使用了Optuna,但是这里没有更多了。
答案 1 :(得分:1)
您可以尝试使用“随机搜索”代替“网格搜索”,“随机搜索”是一种使用超参数的随机组合为构建的模型找到最佳解决方案的技术。 它尝试一系列值的随机组合。为了通过随机搜索进行优化,该函数在参数空间中以一定数量的随机配置进行评估。
您可以在sklearn documentation page上找到详细信息。比较了随机搜索和网格搜索。
我希望您觉得这有用。
答案 2 :(得分:1)