from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier as RFC
from sklearn.svm import SVC from bayes_opt import BayesianOptimization
from bayes_opt.util import Colours def get_data():
"""Synthetic binary classification dataset."""
data, targets = make_classification(
n_samples=1000,
n_features=45,
n_informative=12,
n_redundant=7,
random_state=134985745,
)
return data, targets def svc_cv(C, gamma, data, targets):
"""SVC cross validation.
This function will instantiate a SVC classifier with parameters C and
gamma. Combined with data and targets this will in turn be used to perform
cross validation. The result of cross validation is returned.
Our goal is to find combinations of C and gamma that maximizes the roc_auc
metric.
"""
estimator = SVC(C=C, gamma=gamma, random_state=2)
cval = cross_val_score(estimator, data, targets, scoring='roc_auc', cv=4)
return cval.mean() def rfc_cv(n_estimators, min_samples_split, max_features, data, targets):
"""Random Forest cross validation.
This function will instantiate a random forest classifier with parameters
n_estimators, min_samples_split, and max_features. Combined with data and
targets this will in turn be used to perform cross validation. The result
of cross validation is returned.
Our goal is to find combinations of n_estimators, min_samples_split, and
max_features that minimzes the log loss.
"""
estimator = RFC(
n_estimators=n_estimators,
min_samples_split=min_samples_split,
max_features=max_features,
random_state=2
)
cval = cross_val_score(estimator, data, targets, scoring='neg_log_loss', cv=4)
return cval.mean() def optimize_svc(data, targets):
"""Apply Bayesian Optimization to SVC parameters.""" def svc_crossval(expC, expGamma):
"""Wrapper of SVC cross validation.
Notice how we transform between regular and log scale. While this
is not technically necessary, it greatly improves the performance
of the optimizer.
"""
C = 10 ** expC
gamma = 10 ** expGamma
return svc_cv(C=C, gamma=gamma, data=data, targets=targets) optimizer = BayesianOptimization(
f=svc_crossval,
pbounds={"expC": (-3, 2), "expGamma": (-4, -1)},
random_state=1234,
verbose=2
)
optimizer.maximize(n_iter=10) print("Final result:", optimizer.max) def optimize_rfc(data, targets):
"""Apply Bayesian Optimization to Random Forest parameters.""" def rfc_crossval(n_estimators, min_samples_split, max_features):
"""Wrapper of RandomForest cross validation.
Notice how we ensure n_estimators and min_samples_split are casted
to integer before we pass them along. Moreover, to avoid max_features
taking values outside the (0, 1) range, we also ensure it is capped
accordingly.
"""
return rfc_cv(
n_estimators=int(n_estimators),
min_samples_split=int(min_samples_split),
max_features=max(min(max_features, 0.999), 1e-3),
data=data,
targets=targets,
) optimizer = BayesianOptimization(
f=rfc_crossval,
pbounds={
"n_estimators": (10, 250),
"min_samples_split": (2, 25),
"max_features": (0.1, 0.999),
},
random_state=1234,
verbose=2
)
optimizer.maximize(n_iter=10) print("Final result:", optimizer.max) if __name__ == "__main__":
data, targets = get_data() print(Colours.yellow("--- Optimizing SVM ---"))
optimize_svc(data, targets) print(Colours.green("--- Optimizing Random Forest ---"))
optimize_rfc(data, targets)

调参贝叶斯优化(BayesianOptimization)的更多相关文章

  1. DeepMind提出新型超参数最优化方法:性能超越手动调参和贝叶斯优化

    DeepMind提出新型超参数最优化方法:性能超越手动调参和贝叶斯优化 2017年11月29日 06:40:37 机器之心V 阅读数 2183   版权声明:本文为博主原创文章,遵循CC 4.0 BY ...

  2. 贝叶斯优化(Bayesian Optimization)只需要看这一篇就够了,算法到python实现

    贝叶斯优化 (BayesianOptimization) 1 问题提出 神经网咯是有许多超参数决定的,例如网络深度,学习率,正则等等.如何寻找最好的超参数组合,是一个老人靠经验,新人靠运气的任务. 穷 ...

  3. 贝叶斯优化(Bayesian Optimization)深入理解

    目前在研究Automated Machine Learning,其中有一个子领域是实现网络超参数自动化搜索,而常见的搜索方法有Grid Search.Random Search以及贝叶斯优化搜索.前两 ...

  4. 基于贝叶斯优化的超参数tuning

    https://arimo.com/data-science/2016/bayesian-optimization-hyperparameter-tuning/ 贝叶斯优化:使用高斯过程作为代理函数, ...

  5. 贝叶斯优化 Bayesian Optimization

    贝叶斯优化 Bayesian Optimization 2018年07月02日 22:28:06 余生最年轻 阅读数 4821更多 分类专栏: 机器学习   版权声明:本文为博主原创文章,遵循CC 4 ...

  6. 非参贝叶斯(Bayesian Non-parameter)初步

    0. motivations 如何确定 GMM 模型的 k,既观察到的样本由多少个高斯分布生成.由此在数据属于高维空间中时,根本就无法 visualize,更加难以建立直观,从而很难确定 k,高斯分布 ...

  7. 【转载】 自动化机器学习(AutoML)之自动贝叶斯调参

    原文地址: https://blog.csdn.net/linxid/article/details/81189154 ---------------------------------------- ...

  8. [调参]CV炼丹技巧/经验

    转自:https://www.zhihu.com/question/25097993 我和@杨军类似, 也是半路出家. 现在的工作内容主要就是使用CNN做CV任务. 干调参这种活也有两年时间了. 我的 ...

  9. Deep learning网络调参技巧

    参数初始化 下面几种方式,随便选一个,结果基本都差不多.但是一定要做.否则可能会减慢收敛速度,影响收敛结果,甚至造成Nan等一系列问题.n_in为网络的输入大小,n_out为网络的输出大小,n为n_i ...

随机推荐

  1. GRUB 的配置文件解析

    原文:http://c.biancheng.net/view/1032.html 本节,我们就来看看 GRUB 的配置文件 /boot/gmb/grub.conf 中到底写了什么.命令如下: [roo ...

  2. STM32 内部flash的读写程序

    ​ /* Base address of the Flash sectors */ #define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base ...

  3. springcloud第二步:发布服务提供者

    创建项目service-member Maven依赖 <parent> <groupId>org.springframework.boot</groupId> &l ...

  4. 原生js标识当前导航位置(给当前导航一个className=active)

    导航html结构为: <div class="header2-nav"> <a href="index.html">首页</a&g ...

  5. 使用paginate分页后数据处理

    public function index(){ $sql = ""; $list = ""; $pagenumber = 20;//默认分页条数 //查询数据 ...

  6. Hydra(爆破神器)

    PS:这款暴力密码破解工具相当强大,支持几乎所有协议的在线密码破解,其密码能否被破解关键在于字典是否足够强大.对于社会工程型渗透来说,有时能够得到事半功倍的效果.本文仅从安全角度去探讨测试,使用本文内 ...

  7. 2017UGUI之slider

    不让鼠标控制slider的滑动: 鼠标之所以可以控制滑动是因为slider具有interactable这个属性(下图红色的箭头的地方):如果取消了这个属性的运行的时候就不能滑动了.如果要代码去控制这个 ...

  8. Java基础语法-Lambda表达式

    1.Lambda表达式主要用于简写接口的操作等出现匿名内部类的地方,如下:我这里先定义一个Swim接口,以及它所定义的swim()方法 interface Swim{ void swim(); } 2 ...

  9. Centos7上安装、破解bamboo6.0.3

    1.下载bamboo安装包,地址:https://www.atlassian.com/software/bamboo/download?_ga=2.65378349.245489969.1512876 ...

  10. datatable的select()结果直接修改,会体现在表上

    如果你只是要修改DataTable里的DataRow的话没有必要找到它的行号,直接在Select后得到的DataRow上修改就行了,它的修改会直接反映到DataTable上(其实就是直接修改了Data ...