hpsklearn调参实例

本文介绍使用Hyperopt-sklearn进行高效模型选择的方法。通过在鸢尾花数据集上进行分类任务及波士顿房价数据集上的回归任务,演示如何自动优化机器学习管道中的预处理步骤、模型选择及其参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from __future__ import print_function
# import numpy as np
from sklearn import datasets
from sklearn.cross_validation import train_test_split
from hyperopt import tpe
import hpsklearn
import sys

def test_demo_iris():

    iris = datasets.load_iris()
    X_train, X_test, y_train, y_test = train_test_split(
        iris.data, iris.target, test_size=.25, random_state=1)

    estimator = hpsklearn.HyperoptEstimator(
        preprocessing=hpsklearn.components.any_preprocessing('pp'),
        classifier=hpsklearn.components.any_classifier('clf'),
        algo=tpe.suggest,
        trial_timeout=15.0,  # seconds
        max_evals=10,
        seed=1
    )

    # /BEGIN `Demo version of estimator.fit()`
    print('', file=sys.stderr)
    print('====Demo classification on Iris dataset====', file=sys.stderr)

    iterator = estimator.fit_iter(X_train, y_train)
    next(iterator)

    n_trial = 0
    while len(estimator.trials.trials) < estimator.max_evals:
        iterator.send(1)  # -- try one more model
        n_trial += 1
        print('Trial', n_trial, 'loss:', estimator.trials.losses()[-1], 
              file=sys.stderr)
        # hpsklearn.demo_support.scatter_error_vs_time(estimator)
        # hpsklearn.demo_support.bar_classifier_choice(estimator)

    estimator.retrain_best_model_on_full_data(X_train, y_train)

    # /END Demo version of `estimator.fit()`

    print('Test accuracy:', estimator.score(X_test, y_test), file=sys.stderr)
    print('====End of demo====', file=sys.stderr)


def test_demo_boston():

    boston = datasets.load_boston()
    X_train, X_test, y_train, y_test = train_test_split(
        boston.data, boston.target, test_size=.25, random_state=1)

    estimator = hpsklearn.HyperoptEstimator(
        preprocessing=hpsklearn.components.any_preprocessing('pp'),
        regressor=hpsklearn.components.any_regressor('reg'),
        algo=tpe.suggest,
        trial_timeout=15.0,  # seconds
        max_evals=10,
        seed=1
    )

    # /BEGIN `Demo version of estimator.fit()`
    print('', file=sys.stderr)
    print('====Demo regression on Boston dataset====', file=sys.stderr)


    iterator = estimator.fit_iter(X_train, y_train)
    next(iterator)

    n_trial = 0
    while len(estimator.trials.trials) < estimator.max_evals:
        iterator.send(1)  # -- try one more model
        n_trial += 1
        print('Trial', n_trial, 'loss:', estimator.trials.losses()[-1], 
              file=sys.stderr)
        # hpsklearn.demo_support.scatter_error_vs_time(estimator)
        # hpsklearn.demo_support.bar_classifier_choice(estimator)

    estimator.retrain_best_model_on_full_data(X_train, y_train)

    # /END Demo version of `estimator.fit()`

    print('Test R2:', estimator.score(X_test, y_test), file=sys.stderr)
    print('====End of demo====', file=sys.stderr)

参考文献
1、https://github.com/hyperopt/hyperopt-sklearn/tree/master/hpsklearn
2、http://hyperopt.github.io/hyperopt-sklearn/#documentation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值