import xgboost as xgb
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import make_scorer
from sklearn.metrics import accuracy_score
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.model_selection import ParameterGrid
from sklearn.model_selection import ParameterSampler
from scipy.stats.distributions import expon
import numpy as np #从给定分布中生成采样参数
np.random.seed(0)
param = {"a":[1,2],"b":expon()}
param = list(ParameterSampler(param,n_iter=4)) ################################################################################## def get_model_GridSearchCV(estimator,parameters,X_train,y_train,scoring,cv=5):
"""
return:返回训练过的最好模型
""" #refit:Refit an estimator using the best found parameters on the whole dataset.
model = GridSearchCV(estimator=estimator,param_grid=parameters,scoring=scoring,cv=5,refit=True) model.fit(X_train, y_train) #打印结果
print("best score in GridSearchCV:\n",model.best_score_)
print("best param in GridSearchCV:\n",model.best_params_) return model.best_estimator_ #########################################测试######################################## X,y = load_breast_cancer(return_X_y=True) #分隔训练集和测试集
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.3,random_state=0,stratify=y) #配置参数
param = [{
"learning_rate":[0.1,0.3,0.6],
"max_depth":[5,6,7],
"n_estimators":[100,200,300],
}] #scoring = make_scorer(accuracy_score, greater_is_better=True) estimator = xgb.XGBClassifier(objective="reg:logistic") #训练模型
model = get_model_GridSearchCV(estimator=estimator,
parameters=param,
cv=5,
X_train=X_train,
y_train=y_train,
scoring="roc_auc") #采用训练得模型做测试
"""
decision_function(*args, **kwargs) Call decision_function on the estimator with the best found parameters.
fit(X[, y, groups]) Run fit with all sets of parameters.
get_params([deep]) Get parameters for this estimator.
inverse_transform(*args, **kwargs) Call inverse_transform on the estimator with the best found params.
predict(*args, **kwargs) Call predict on the estimator with the best found parameters.
predict_log_proba(*args, **kwargs) Call predict_log_proba on the estimator with the best found parameters.
predict_proba(*args, **kwargs) Call predict_proba on the estimator with the best found parameters.
score(X[, y]) Returns the score on the given data, if the estimator has been refit.
set_params(**params) Set the parameters of this estimator.
transform(*args, **kwargs) Call transform on the estimator with the best found parameters.
"""
y_pred = model.predict(X_test) #模型评价
print(accuracy_score(y_test,y_pred))

封装GridSearchCV的训练包的更多相关文章

  1. 将PL/SQL代码封装在机灵的包中

    将代码封装在机灵的包中 http://www.oracle.com/technetwork/issue-archive/2013/13-jan/o13plsql-1872456.html 绝大多数基于 ...

  2. 第32节:Java中-构造函数,静态方法,继承,封装,多态,包

    构造函数实例 class Cat{ // 设置私有的属性 name private String name; // 设置name的方法 public void setName(String Name) ...

  3. 如何将自定义标签封装成一个Jar包

    当我们在一个web应用中开发好一些自定义标签的时候,这些自定义标签通常有标签处理器Java类,和一个描述这些标签tld文件,如果我们想在以后别的web工程中还能用上这些标签,可以将这些自定义标签封装在 ...

  4. 把封装脚本做成jar包

    前提: eclipse, selenium, maven 把二次封装过的脚本做成jar包, 这样可以在新建工程里也调用封装过的方法. 实现步骤: 1. project 右键 => maven = ...

  5. java基础课程笔记 static 主函数 静态工具类 classpath java文档注释 静态代码块 对象初始化过程 设计模式 继承 子父类中的函数 继承中的构造函数 对象转型 多态 封装 抽象类 final 接口 包 jar包

    Static那些事儿 Static关键字 被static修饰的变量成为静态变量(类变量) 作用:是一个修饰符,用于修饰成员(成员变量,成员方法) 1.被static修饰后的成员变量只有一份 2.当成员 ...

  6. Dapper的封装、二次封装、官方扩展包封装,以及ADO.NET原生封装

    前几天偶然看到了dapper,由于以前没有用过,只用过ef core,稍微看了一下,然后写了一些简单的可复用的封装. Dapper的用法比较接近ADO.NET所以性能也是比较快.所以我们先来看看使用A ...

  7. Java -- 封装访问控制级别,包, instanceof 运算符, 初始化块

    1. 可以用 package name1.name2; 显式的定义包名, *.class文件位置应该对应包 name1 name2 的目录下. 2. instanceof 运算符 Object obj ...

  8. Intellij IDEA 封装Jar包(提示错误: 找不到或无法加载主类)

    封装的过程如下: 然后准备打包 选择Build或者ReBuild即可. 但这样就会引起开始第一个图的问题.提示无法加载主类,另外一个情况就是所有的外部第三方jar包都被封装到一个jar包里面了. 那么 ...

  9. Oracle调用Java方法(下)复杂Jar包封装成Oracle方法以及ORA-29521错误

    上一篇随笔中已经说了简单的Jar是如何封装的,但是我的需求是根据TIPTOP的查询条件产生XML文件并上传到FTP主机中,那么就要涉及到XML生成的方法和FTP上传的方法 所以在Eclipse写的时候 ...

随机推荐

  1. Compoxure example 应用说明

    Compoxure 官方提供了一个demo应用,包含了cache,error,layout 等功能 环境准备 demo 使用docker-compose 运行 clone 代码 git clone h ...

  2. loki grafana 团队开源的,类似Prometheus 的log 系统

    Prometheus 主要面向的是metrics,但是loki 是log,这样加上grafana 强大的可视化以及alert能力, 我们可以做好多事情,loki 的设计来源于Prometheus. 组 ...

  3. drone 1.0 新功能试用以及说明

    drone 1.0 rc 已经发布,新的功能很强大,界面比旧版本更加人性化,和git 的集成也更高了 测试环境准备 试用gogs 做为git 管理工具 docker-compose 文件 versio ...

  4. is_null, empty, isset的区别

    参考博客: http://www.jb51.net/article/24666.htm http://www.jb51.net/article/36223.htm is_null, empty, is ...

  5. 对象的继承(prototype)

    修改构造函数的原型对象,批量修改所有子对象的继承关系

  6. 初始Openwrt

    系统结构 在上一章我们已经完成了刷机工作,这个时候系统进行了首次启动,并且格式化了它的"可写"分区.那么在设备里分区到底是怎么样进行的呢?我们首先需要知道:不同的处理器下OpenW ...

  7. pandas读取MySql/SqlServer数据 (转)

    在 Anacondas环境中,conda install pymssql ,一直报包冲突,所以采用先在 https://www.lfd.uci.edu/~gohlke/pythonlibs/#nump ...

  8. 通过Hibernate API编写访问数据库的代码

    private Configuration config;// 1.声明私有配置对象类private ServiceRegistry serviceRegistry;// 2.声明私有服务注册对象类p ...

  9. 如何在Macbook苹果笔记本上按右键点击(适用小米黑苹果)

    1.按下Control键.保持按下Control(Ctrl)键,同时点击鼠标. 这一操作相当于在一个双键鼠标上右击. 点击鼠标后,你可以松开Control键. 该方法适用于单键鼠标或者MacBook ...

  10. 基于nginx + lua实现的反向代理动态更新

    大家都知道,nginx是当前应用非常广泛的web服务器,热度因为他的高并发高性能高可靠性,且轻量级!牛逼的不行,不多说这些. 今天要介绍的是,如何基于nginx和lua脚本,也就是在openresty ...