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. git温习

    git init:将文件变成git仓库 ls -ah:查看隐藏目录 git add 文件1  文件2 ...:将文件添加到缓存区 git commit -m ‘提交说明’:提交到本地仓库一次  并说明 ...

  2. 数据结构 - 表插入排序 具体解释 及 代码(C++)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/24323125 表插入排序 具体解释 及 代码 ...

  3. git reset与git revert的区别

    http://alpha-blog.wanglianghome.org/2010/07/30/git-partial-rollback/ reset(版本撤回) 格式 git reset [-q] [ ...

  4. windows下复制文件报错“文件名对目标文件夹可能过长 。您可以缩短文件名并重试,或者......”

    我将一个路径下文件夹复制到另一个路径下时,出现了报错,报错图片如下: 然后查资料发现: 1.文件名长度最大为255个英文字符,其中包括文件扩展名在内.一个汉字相当于两个英文字符.2.文件的全路径名长度 ...

  5. AIX stack_hard参数

    OS:AIX 7.1 root.oracle.grid用户查看资源限制,其中stack受限,需要在/etc/security/limits中增加stack_hard = -1,受硬限制限制! [ora ...

  6. python commands包不支持windows环境与如何在windows下使用的简易方法

    commands模块不支持windows环境,让我们来看看. >>> import commands >>> print commands.getoutput('d ...

  7. .Net Core部署IIS

    1.环境安装 https://dotnet.microsoft.com/download/dotnet-core 下载Runtime & Hosting Bundle包安装,iis模块会出现支 ...

  8. laravel----------Client error: `POST http://47.98.116.219/oauth/token` resulted in a `401 Unauthorized` response: {"error":"invalid_client","message":"Client authentication failed"}

    1.设备没有授权,原因是 这个client_id的值就是数据库wk_oauth_clients 的主键ID,查看下表是否有这条数据

  9. PeopleSoft底层表,闪存查找历史代码(不小心改)

    Oracle 闪存查找历史代码 select * from (SELECT * FROM  PSPCMTXT      AS OF TIMESTAMP to_timestamp('20180725 1 ...

  10. 设置VS2017背景图片

    设置方法很简单:安装扩展ClaudiaIDE 1.在这里下载扩展,https://visualstudiogallery.msdn.microsoft.com/9ba50f8d-f30c-4e33-a ...