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. xencenter如何安装Centos7虚拟机系统

    xencenter的ip地址192.168.245.134(win10系统) 首先我们在win10系统安装好xencenter(这个软件可以直接在xenserver启动后,通过访问xenserver的 ...

  2. 常用C语言time时间函数

    常见的时间函数有time( ).ctime( ).gmtime( ).localtime( ).mktime( ).asctime( ).difftime( ).gettimeofday( ).set ...

  3. mysql 严格模式 Strict Mode说明(转)

    转自https://www.cnblogs.com/jhcelue/p/7290243.html 1.开启与关闭Strict Mode方法 找到mysql安装文件夹下的my.cnf(windows系统 ...

  4. Ubuntu server 运行.net core api 心得

    1.安装.net core sdk 在微软.net core 安装页面找到linux 安装,按照步骤安装好 2.安装mysql 参考 Ubuntu安装mysql 3.配置mysql 1.需要将mysq ...

  5. linux shell写入单行、多行内容到文件

    1.单行文本 #! /bin/bashecho 'hello world' > filename.txt 2.多行文本 代码1: #! /bin/bashcat>filename.txt& ...

  6. JDBC 编程步骤

    java访问数据库的步骤如下: 加载数据库驱动 通常使用Class.forName(driverClass)来加载驱动 // 加载MySQL驱动 Class.forName("com.mys ...

  7. Visual Studio 2010 出现关于ActivityLog.xml错误的解决方案

    在用VS编写程序是第一次会跳出“Visual Studio has encountered an exception.This may be caused by an extension. You c ...

  8. flume-ng-sql-source实现oracle增量数据读取

    一.下载编译flume-ng-sql-source 下载地址:https://github.com/keedio/flume-ng-sql-source.git ,安装说明文档编译和拷贝jar包 嫌麻 ...

  9. 企业数据总线(ESB)和注册服务管理(dubbo)的区别

    企业数据总线(ESB)和注册服务管理(dubbo)的区别 转载 2015年11月04日 09:05:14 7607  企业数据总线(ESB)和注册服务管理(dubbo)的区别 2015-03-09 0 ...

  10. WebService安全性的几种实现方法【身份识别】

     转:http://blog.csdn.net/yongping8204/article/details/8619577 WebService安全性的几种实现方法[身份识别] 标签: webservi ...