假设你正在使用一个名为model的机器学习模型,以下是一个示例代码:
from sklearn.model_selection import GridSearchCV
param_grid = {
'param1': [value1, value2, ...],
'param2': [value1, value2, ...],
# 添加其他超参数及其可能的值
}
grid_search = GridSearchCV(model, param_grid, cv=5, scoring='accuracy')
grid_search.fit(X_train, y_train)
print("最佳超参数组合:", grid_search.best_params_)
print("最佳模型得分:", grid_search.best_score_)
best_model = grid_search.best_estimator_
best_model.fit(X_train, y_train)
accuracy = best_model.score(X_test, y_test)
print("最佳模型在测试集上的准确率:", accuracy)
上述代码中,你需要替换model、param_grid、X_train、y_train和X_test、y_test为你的实际模型、超参数、训练数据和测试数据。GridSearchCV会尝试不同的超参数组合,并返回性能最佳的模型和参数。
by chatgpt