封装GridSearchCV的训练包
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的训练包的更多相关文章
- 将PL/SQL代码封装在机灵的包中
将代码封装在机灵的包中 http://www.oracle.com/technetwork/issue-archive/2013/13-jan/o13plsql-1872456.html 绝大多数基于 ...
- 第32节:Java中-构造函数,静态方法,继承,封装,多态,包
构造函数实例 class Cat{ // 设置私有的属性 name private String name; // 设置name的方法 public void setName(String Name) ...
- 如何将自定义标签封装成一个Jar包
当我们在一个web应用中开发好一些自定义标签的时候,这些自定义标签通常有标签处理器Java类,和一个描述这些标签tld文件,如果我们想在以后别的web工程中还能用上这些标签,可以将这些自定义标签封装在 ...
- 把封装脚本做成jar包
前提: eclipse, selenium, maven 把二次封装过的脚本做成jar包, 这样可以在新建工程里也调用封装过的方法. 实现步骤: 1. project 右键 => maven = ...
- java基础课程笔记 static 主函数 静态工具类 classpath java文档注释 静态代码块 对象初始化过程 设计模式 继承 子父类中的函数 继承中的构造函数 对象转型 多态 封装 抽象类 final 接口 包 jar包
Static那些事儿 Static关键字 被static修饰的变量成为静态变量(类变量) 作用:是一个修饰符,用于修饰成员(成员变量,成员方法) 1.被static修饰后的成员变量只有一份 2.当成员 ...
- Dapper的封装、二次封装、官方扩展包封装,以及ADO.NET原生封装
前几天偶然看到了dapper,由于以前没有用过,只用过ef core,稍微看了一下,然后写了一些简单的可复用的封装. Dapper的用法比较接近ADO.NET所以性能也是比较快.所以我们先来看看使用A ...
- Java -- 封装访问控制级别,包, instanceof 运算符, 初始化块
1. 可以用 package name1.name2; 显式的定义包名, *.class文件位置应该对应包 name1 name2 的目录下. 2. instanceof 运算符 Object obj ...
- Intellij IDEA 封装Jar包(提示错误: 找不到或无法加载主类)
封装的过程如下: 然后准备打包 选择Build或者ReBuild即可. 但这样就会引起开始第一个图的问题.提示无法加载主类,另外一个情况就是所有的外部第三方jar包都被封装到一个jar包里面了. 那么 ...
- Oracle调用Java方法(下)复杂Jar包封装成Oracle方法以及ORA-29521错误
上一篇随笔中已经说了简单的Jar是如何封装的,但是我的需求是根据TIPTOP的查询条件产生XML文件并上传到FTP主机中,那么就要涉及到XML生成的方法和FTP上传的方法 所以在Eclipse写的时候 ...
随机推荐
- keycloak docker-compose 运行
内容很简单,主要是搭建一个可运行的keycloak 环境,方便开发测试,同时支持数据库的持久化 docker-compose 文件 version: "3" services: a ...
- lapis 1.7.0 更好的openresty 版本兼容以及安全数据库支持
lapis 1.7.0 今年4月2号就发布了,一直没有注意,今天看到changelog就简单的进行了一个 测试(主要是与openresty版本的测试,新变更后边会有) 使用docker-compose ...
- python从文本中提取某酒店机顶盒号和智能卡号
1.某项目中经常遇到需要关闭一些机顶盒消费权限.但是给过来的不是纯字符串,需要自己提取. 有400多个机顶盒和智能卡.nodepad++的列块模式也可以提取,但是还是稍微麻烦,因为列不对等 先复制到文 ...
- js过滤输入的emoji表情
因为emoji表情是Unicode编码, 在某些流浪器上会显示乱码, 有的数据库字节不够也无法存储, 网上有很多解决此类问题的办法, 最简单的莫过于将emoji表情替换成文本, 比如 [表情][表情] ...
- kafka 中的术语
出处:https://tech.meituan.com/2015/01/13/kafka-fs-design-theory.html table th:first-of-type { width: 7 ...
- Eclipse配置问题
1.eclipse中通过search打开第二个文件时第一个文件自动关闭问题: 解决方案: window-preferences-general-search找到第一行的一个选项 reuse edit ...
- java-http通信调用与创建
java项目使用HTTP的请求.主要有两种方式:①使用JDK自带的java.net包下的HttpURLConnection方式. ②使用apache的HttpClient方式. 一.使用JDK自带的j ...
- Jmeter中Websocket协议支持包的使用
Jmeter中Websocket协议支持包的使用(转) 参考的来源是国外一篇文章,已经整理成pdf格式(http://yunpan.cn/cFzwiyeQDKdh3 (提取码:9bcf)) 转自:ht ...
- .NET设计模式 第二部分 创建型模式(3)—建造者模式(Builder Pattern)
建造者模式(Builder Pattern) ——.NET设计模式系列之四 Terrylee,2005年12月17日 概述 在软件系统中,有时候面临着“一个复杂对象”的创建工作,其通常由各个部分的子对 ...
- linux od命令详解
Linux od命令 Linux od命令用于输出文件内容.od指令会读取所给予的文件的内容,并将其内容以八进制字码呈现出来 将指定文件以八进制形式(默认)转储到标准输出.如果指定了多于一个的文件参数 ...