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. 后端程序猿怎能不会的linux命令

    (图片超清,可放大网页查看) 来源 https://zhuanlan.zhihu.com/p/28674639

  2. uname command

    The command uname helps us in development special in scripts, see help of the uname uname --help Usa ...

  3. 几个OOD概念

    Composition vs. Aggregation Composition和Aggregation都是”包含”的关系 (part of, made up of) ,不同的是生命周期.对于Compo ...

  4. HI35XX NVR

    NVR类型的:3515-3520-3531-3535-3536 后面的高端

  5. ML(附录1)——梯度下降

    梯度下降是迭代法的一种,可以用于求解最小二乘问题(线性和非线性都可以).在求解机器学习算法的模型参数,即无约束优化问题时,梯度下降(Gradient Descent)是最常采用的方法之一,另一种常用的 ...

  6. kolla-ansible部署单节点OpenStack-Pike

    一.准备工作 最小化安装CentOS 7.5,装完后,进行初始化 selinux,防火墙端口无法访问,主机名问题,都是安装的常见错误,一定要细心确认. kolla的安装,要求目标机器是两块网卡: en ...

  7. NET设计模式 第二部分 创建型模式(6):创建型模式专题总结(Creational Pattern)

    创建型模式专题总结(Creational Pattern) ——.NET设计模式系列之七 Terrylee,2006年1月 概述 创建型模式,就是用来创建对象的模式,抽象了实例化的过程.它帮助一个系统 ...

  8. [转]跳板机Jumpserve的生产环境配置

    6.跳板机Jumpserver]   Jumpserver是国内一款开源的轻便的跳板机系统,他们的官网:http://www.jumpserver.org/ 使用这款软件意在提高公司内部登录生产环境服 ...

  9. flask-appbuilder 快速入门

    简介: Flask-AppBuilder是基于Flask实现的一个用于快速构建Web后台管理系统的简单的框架.主要用于解决构建Web后台管理系统时避免一些重复而繁琐的工作,提高项目完成时间,它可以和 ...

  10. php 二维数组按照指定字段进行排序

    $allItem = [ ["id"=>10,"updated_at"=>"2018-11-01"], ["id&qu ...