hpsklearn调参实例
2018-01-11 15:18
113 查看
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
相关文章推荐
- Python中正则表达式的用法实例汇总
- extjs构造store 实例
- DRS测试实例
- IOS UITableView分组与索引分区实例
- 使用new和反射实例化内部类对象
- 3、jvm内存分配实例
- 数据库连接池原理与实例解析
- Java实现一个小说采集程序的简单实例
- Bootstrap框架结合jQuery仿百度换肤功能实例解析
- Python列表计数及插入实例
- keepalived 多实例
- EntiryFramework中事务操作实例
- Servlet多线程单实例
- Jawe工作流建模及Shark工作流引擎的实例说明(三)
- [流媒体]实例解析MMS流媒体协议,下载LiveMediaVideo[1]
- 结合实例实习F#(三)--理解函数式语言中的函数
- spring2.5.6学习笔记五:Spring的三种实例化Bean的方式
- KETTLE“设置变量“、”变量赋值”"和“自己定义JAVA代码”的实例应用(kettle动态excel文件名需要重点参考看1.1.4)
- Flex 布局教程:实例篇