xgb.cv进行交叉验证

  1. 模型评估的方法

    留出法
    留出法是将原数据集分成互斥的两组,一组作为训练集,另一组作为测试集
    交叉验证发
    交叉验证(cross-validation 简称cv)将数据集分为k等份,对于每一份数据集,其中k-1份用作训练集,单独的那一份用作验证集。
    通常情况下,留一法对模型的评估可能会不准确,一般采用xgboost.cv可以进行交叉验证

以下基于kaggle的heart-disease数据进行交叉验证

import numpy as np
import pandas as pd

from sklearn.model_selection import train_test_split,StratifiedKFold,train_test_split,GridSearchCV
from sklearn.metrics import accuracy_score, confusion_matrix, mean_squared_error,roc_auc_score
from xgboost import plot_importance
from matplotlib import pyplot as plt
import xgboost as xgb

dataset=pd.read_csv('heart.csv')
dataset.head(5)

数据前五行如下:
在这里插入图片描述
利用straightkfold做交叉验证输出混淆矩阵
StratifiedKFold用法类似Kfold,但是他是分层采样,确保训练集,测试集中各类别样本的比例与原始数据集中相同

rng=np.random.RandomState(31337)
kf=StratifiedKFold(n_splits=3,shuffle=True,random_state=rng)#采用三折交叉验证
for train_index,test_index in kf.split(X,Y):
    xgb_model = xgb.XGBClassifier().fit(X.iloc[train_index], Y.iloc[train_index])
    predictions = xgb_model.predict(X.iloc[test_index])
    actuals = Y.iloc[test_index]
    print(confusion_matrix(actuals, predictions))

输出混淆矩阵
[[36 10]
[ 3 52]]
[[33 13]
[13 42]]
[[35 11]
[10 45]]

利用xgb.cv找出最优的树

#利用train_test_split方法,将X,y随机划分问,训练集(X_train),训练集标签(X_test),测试卷(y_train),
#测试集标签(y_test),训练集:测试集=7:3的

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, random_state=42)
#xgboost可以加载libsvm格式的文本数据,加载的数据格式可以为numpy的二维数组和xgboost的二进制的缓存文件。加载的数据存储在对象dmatrix中。
xgtrain = xgb.DMatrix(X_train, label=y_train)


clf = xgb.XGBClassifier(missing=9999999999,
                max_depth = 7,
                n_estimators=80,
                learning_rate=0.1, 
                nthread=4,
                subsample=1.0,
                colsample_bytree=0.5,
                min_child_weight = 3,
                seed=1301)
xgb_param = clf.get_xgb_params()



#do cross validation
print ('Start cross validation')
cvresult = xgb.cv(xgb_param, xgtrain, num_boost_round=5000, nfold=5, metrics=['auc'],
     early_stopping_rounds=50, stratified=True, seed=1301)
#交叉验证后最好的树
print('Best number of trees = {}'.format(cvresult.shape[0]))
clf.set_params(n_estimators=cvresult.shape[0])#把clf的参数设置成最好的树对应的参数
print('Fit on the trainingsdata')
clf.fit(X_train, y_train, eval_metric='auc')#训练clf
print('Overall AUC:', roc_auc_score(y_train, clf.predict_proba(X_train)[:,1]))#训练auc
print('Predict the probabilities based on features in the test set')
pred = clf.predict_proba(X_test, ntree_limit=cvresult.shape[0])
print('Fit on the testingsdata')
print('Overall AUC:', roc_auc_score(y_test, clf.predict_proba(X_test)[:,1]))#测试auc
#测试集上的概率     
submission= pd.DataFrame({"ID":X_test.index, "TARGET":pred[:,1]})
submission.to_csv("submission.csv", index=False)
#测试集分类结果的预测
print('Fit on the testingsdata')
print('Overall AUC:', roc_auc_score(y_test, clf.predict(X_test)))#测试auc

其中cvresult的结果为
在这里插入图片描述
输出的结果如下
Start cross validation
Best number of trees = 22
Fit on the trainingsdata
Overall AUC: 0.9752577319587629
Predict the probabilities based on features in the test set
Fit on the testingsdata using predict_proba
Overall AUC: 0.888780487804878
Fit on the testingsdata using predict
Overall AUC: 0.8102439024390243

  • 13
    点赞
  • 85
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
在R语言中,基于xgboost的xgb.cv函数进行随机网格调参和十折交叉验证可以使用以下步骤: 1. 加载xgboost包: ```R library(xgboost) ``` 2. 准备数据集: ```R data(iris) x <- iris[,1:4] y <- iris[,5] ``` 3. 定义参数空间和随机搜索的迭代次数: ```R param_space <- list( eta = runif(10, 0.01, 0.2), max_depth = sample(c(3,6,9), 10, replace = TRUE), subsample = runif(10, 0.5, 0.9), colsample_bytree = runif(10, 0.5, 0.9), min_child_weight = sample(c(1,5,10), 10, replace = TRUE) ) n_iter <- 10 ``` 在上述代码中,我们定义了一个参数空间param_space,其中包括eta、max_depth、subsample、colsample_bytree和min_child_weight等参数,并对每个参数进行了随机采样。我们还定义了随机搜索的迭代次数n_iter,这里设置为10次。 4. 定义xgboost模型和交叉验证参数: ```R xgb_model <- xgb.cv( params = list( objective = "multi:softmax", num_class = length(unique(y)), eval_metric = "mlogloss" ), data = xgb.DMatrix(x, label = y), nrounds = 100, nfold = 10, early_stopping_rounds = 10, verbose = 0 ) ``` 在上述代码中,我们使用xgb.cv函数定义了xgboost模型,并指定了模型的参数、数据集和交叉验证的参数。我们将数据集转换为xgb.DMatrix格式,并指定目标变量的标签为y。我们还指定了模型的nrounds、nfold、early_stopping_rounds和verbose等参数,用于控制模型的训练和输出。 5. 输出最佳模型参数: ```R xgb_model$best_iteration xgb_model$best_score xgb_model$best_params ``` 在上述代码中,我们输出了最佳模型的迭代次数、得分和参数组合。 综上所述,通过使用xgb.cv函数进行随机网格调参和十折交叉验证,我们可以快速找到最佳的xgboost模型参数组合,并在分类、回归等任务中取得更好的预测性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值