机器学习:调参与最终模型hyperopt(Python)

from hyperopt import fmin, tpe, hp
import hyperopt.pyll.stochastic
import numpy as np
from sklearn.datasets import load_digits
import matplotlib.pyplot as plt
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler  # 类别标签编码,标准化处理
from sklearn.svm import SVC
from sklearn.decomposition import PCA # 主成分分析

best = fmin(
    fn=lambda x: -np.sin(x) / x,
    space=hp.uniform('x', -20, 20),
    algo=tpe.suggest,
    max_evals=100)
print(best)

digits = load_digits()
X, y =digits.data, digits.target

print(digits.images[0])

fig = plt.figure(figsize=(12, 8))
fig.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=0.05, wspace=0.05)
for i in range(24):
    ax = fig.add_subplot(4, 6, i + 1, xticks=[], yticks=[])
    ax.imshow(digits.images[i])
plt.show()

params_space_svc = {'C': hp.uniform('C', 0, 100),
                    'kernel': hp.choice('kernel', ['poly', 'rbf']),
                    'gamma': hp.loguniform('gamma', np.log(0.001), np.log(0.1))}   #  hp.uniform('gamma', 0.001, 0.1)
print(hyperopt.pyll.stochastic.sample(params_space_svc))

pipe_svc = make_pipeline(StandardScaler(), PCA(n_components=20), SVC())
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=0, shuffle=True, stratify=y)

count = 0   # 每一次参数组合的枚举次数
cv_scores = []
def hyperopt_train_val(args):
    clf = SVC(**args)
    score = cross_val_score(clf, X_train, y_train, cv=5).mean()
    cv_scores.append(score)
    global count
    count = count + 1
    print("[%d], %s, Validate ACC: %.5f" % (count, args, score))
    return -score

#  max_evals指定枚举次数上限,返回目前搜索到的最优解,不一定全局最优
best = fmin(hyperopt_train_val, params_space_svc, algo=tpe.suggest, max_evals=100)

kernel_list = ['rbf', 'poly']
best["kernel"] = kernel_list[best["kernel"]]
print('best params: ', best)

clf = SVC(**best)   # 根据最佳参数对测试数据进行预测
clf.fit(X_train, y_train)
print("The best params of svc, test scores = %.5f" % clf.score(X_test, y_test))

plt.figure(figsize=(8, 6))
plt.plot(cv_scores, 'ko--', markersize=5, markeredgecolor="r")
plt.xlabel("eval times", fontsize=12)
plt.ylabel("scores", fontsize=12)
plt.grid(ls=":")
plt.title("Super parameter tuning of SVM by Hyperopt", fontsize=14)
plt.show()

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

捕捉一只Diu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值