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. KNN算法原理(python代码实现)

    kNN(k-nearest neighbor algorithm)算法的核心思想是如果一个样本在特征空间中的k个最相邻的样本中的大多数属于某一个类别,则该样本也属于这个类别,并具有这个类别上样本的特性 ...

  2. laravel 一表關聯二表,二表關聯三表,通過一表controller拿三表數據

    model 一表關聯二表 public function ordercode() { return $this->hasOne(\App\Models\OrderCode::class,'id' ...

  3. bzoj 4709 [Jsoi2011]柠檬——单调栈二分处理决策单调性

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4709 题解:https://blog.csdn.net/neither_nor/articl ...

  4. C# 控件

    .ascx:Web窗体用户控件.用来存放独立的用户控件,可提供众多页面使用: <%@ Control Language="C#" AutoEventWireup=" ...

  5. c++ 函数中的部分代码执行一次

    编程时有时需要将一段代码中的某一块只执行一次: #include<iostream> using namespace std; int fun1(int a) { static bool ...

  6. 基于redis 3.x搭建集群环境

    由于我团队开发的在线坐席系统,即将面对线上每周3000W的下行投放客户,产品的咨询量可能会很大,基于前期,200W的投放时,前10分钟,大概800问题量,平均一个客户大概8个问题,也就是说每分钟10个 ...

  7. MobaXterm的一些介绍(Top 5 SSH Clients for Windows (Alternatives of PuTTY))

    Top 5 SSH Clients for Windows (Alternatives of PuTTY) http://tecadmin.net/top-5-ssh-clients-for-wind ...

  8. Linux系统构成和基本操作

    Linux的优势 Linux的目录结构 Linux目录与文件管理 列出目录内容 创建新目录(文件夹) 创建文件 复制文件或目录 删除文件或目录 移动目录或文件 查看文件属性 文件属性含义 读权限-4 ...

  9. Git常见使用方法

    图参考:http://www.ruanyifeng.com/blog/2014/06/git_remote.html 1.GitLab配置 git config --global user.name ...

  10. 在CAD二次开发中使用进度条

    ProgressMeter pm = new ProgressMeter(); pm.Start("正在执行..."); pm.SetLimit(100); for (int i ...