当前位置:网站首页>sklearn sklearn中的模型调参利器 gridSearchCV(网格搜索)
sklearn sklearn中的模型调参利器 gridSearchCV(网格搜索)
2022-06-23 04:40:00 【郭庆汝】
sklearn sklearn中的模型调参利器 gridSearchCV(网格搜索)

代码:
import pandas as pd # 数据科学计算工具
import numpy as np # 数值计算工具
import matplotlib.pyplot as plt # 可视化
import seaborn as sns # matplotlib的高级API
from sklearn.model_selection import StratifiedKFold #交叉验证
from sklearn.model_selection import GridSearchCV #网格搜索
from sklearn.model_selection import train_test_split #将数据集分开成训练集和测试集
from xgboost import XGBClassifier #xgboost
pima = pd.read_csv("pima_indians-diabetes.csv")
print(pima.head())
x = pima.iloc[:,0:8]
y = pima.iloc[:,8]
seed = 7 #重现随机生成的训练
test_size = 0.33 #33%测试,67%训练
X_train, X_test, Y_train, Y_test = train_test_split(x, y, test_size=test_size, random_state=seed
model = XGBClassifier()
learning_rate = [0.0001,0.001,0.01,0.1,0.2,0.3] #学习率
gamma = [1, 0.1, 0.01, 0.001]
param_grid = dict(learning_rate = learning_rate,gamma = gamma)#转化为字典格式,网络搜索要求
kflod = StratifiedKFold(n_splits=10, shuffle = True,random_state=7)#将训练/测试数据集划分10个互斥子集,
grid_search = GridSearchCV(model,param_grid,scoring = 'neg_log_loss',n_jobs = -1,cv = kflod)
#scoring指定损失函数类型,n_jobs指定全部cpu跑,cv指定交叉验证
grid_result = grid_search.fit(X_train, Y_train) #运行网格搜索
print("Best: %f using %s" % (grid_result.best_score_,grid_search.best_params_))
#grid_scores_:给出不同参数情况下的评价结果。best_params_:描述了已取得最佳结果的参数的组合
#best_score_:成员提供优化过程期间观察到的最好的评分
#具有键作为列标题和值作为列的dict,可以导入到DataFrame中。
#注意,“params”键用于存储所有参数候选项的参数设置列表。
means = grid_result.cv_results_['mean_test_score']
params = grid_result.cv_results_['params']
for mean,param in zip(means,params):
print("%f with: %r" % (mean,param))

边栏推荐
- 【Cocos2d-x】可擦除的Layer:ErasableLayer
- ant使用总结(二):相关命令说明
- WordPress Core 5.8.2 - 'WP_ Query'SQL injection
- jvm-01. Instruction rearrangement
- Gplearn appears assignment destination is read only
- mysql读已提交和可重复度区别
- Remove duplicates from sorted list II of leetcode topic resolution
- Ant Usage Summary (II): description of related commands
- Multiple strings for leetcode topic resolution
- Pat class B 1026 program running time
猜你喜欢

Three most advanced certifications, two innovative technologies and two outstanding cases, Alibaba cloud appeared at the cloud native industry conference

Kotlin Android simple activity jump, simple combination of handler and thread

Wireshark TS | video app cannot play

Visual studio debugging tips

Adnroid activity screenshot save display to album view display picture animation disappear

11、 Realization of textile fabric off shelf function

Redis sentry

又到半年总结时,IT人只想躺平

Cloud native database is the future

jvm-01. Instruction rearrangement
随机推荐
Network packet capturing tcpdump User Guide
Add and multiply two polynomials using linked list
Find the number of nodes in the widest layer of a binary tree
Leetcode topic resolution single number
【开源项目】excel导出lua配置表工具
[cocos2d-x] screenshot sharing function
Infotnews | which Postcard will you receive from the universe?
Three most advanced certifications, two innovative technologies and two outstanding cases, Alibaba cloud appeared at the cloud native industry conference
Adnroid activity screenshot save display to album view display picture animation disappear
Activity startup mode and life cycle measurement results
使用aggregation API扩展你的kubernetes API
【DaVinci Developer专题】-41-APP SWC如何读取写入NVM Block数据
Kotlin interface
jvm-03. JVM memory model
Introduction to JVM principle
Radar canvas
【Cocos2d-x】截图分享功能
Possible pits in mongodb project
【DaVinci Developer专题】-42-如何生成APP SWC的Template和Header文件
Day_07 传智健康项目-Freemarker