YuQia030 2023-11-28 20:25 采纳率: 0%
浏览 7

使用hyperopt算法优化超参但返回的结果不对

想使用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

img

  • 写回答

3条回答 默认 最新

  • IT工程师_二师兄 2023-11-28 22:46
    关注

    你把具体代码段复制出来发给我看一下

    评论

报告相同问题?

问题事件

  • 创建了问题 11月28日

悬赏问题

  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
  • ¥15 栅极驱动低侧烧毁MOSFET
  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题
  • ¥20 基于matlab的航迹融合 航迹关联 航迹插补
  • ¥15 用Matlab实现图中的光线追迹
  • ¥15 联想笔记本开机出现系统更新界面