想使用hyperopt算法进行超参数优化,能正常运行,训练过程中的超参数组合打印出来也没有问题,但最后fmin返回的最佳超参组合结果不对,在最开始运行的时候有显示过No module named 'imp',去查了看说是过时了,替换成了importlib,不知道是不是这里替换的原因,但不记得当时是替换的哪里了。
import numpy as np
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import accuracy_score
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from hyperopt import hp, tpe, Trials, fmin
# 数据集加载
data = load_breast_cancer()
X = data['data']
y = data['target']
X_train, X_val, y_train, y_val = train_test_split(
X, y, test_size=0.2, random_state=1
)
# 定义超参数搜索空间和评估函数
param_space = {
'n_estimators': hp.choice('n_estimators', range(100,500,200)),
'max_depth': hp.choice('max_depth', range(4,10,3)),
'min_samples_split': hp.choice('min_samples_split', range(2,5,2)),
'learning_rate': hp.loguniform('learning_rate', np.log(0.01), np.log(0.1))
}
def evaluate_model(params):
print(params)
model = GradientBoostingClassifier(random_state=1, **params)
model.fit(X_train, y_train)
y_pred = model.predict(X_val)
score = accuracy_score(y_val, y_pred)
return {'loss': -score, 'status': 'ok'}
# 运行Hyperband算法
trials = Trials()
best = fmin(fn=evaluate_model, space=param_space, algo=tpe.suggest, max_evals=50, trials=trials)
print(best)
结果显示如下:出现了不在超参数范围里的0