天天看点

ML:模型训练评估中常用的两种方法代码实现(留一法一次性切分训练和K折交叉验证训练)

模型训练评估中常用的两种方法代码实现

T1、留一法一次性切分训练

T2、K折交叉验证训

print("data split:")

if kfold_flag:   #T1、采用K折交叉验证训练

   kf = KFold(n_splits=2, shuffle=False)  # K折交叉验证

   for train_index, test_index in kf.split(X_train):

       x_train_, y_train_ = X_train[train_index], y_train[train_index]

       x_test_, y_test_ = X_train[test_index], y_train[test_index]

       ModelC = ModelC_Train(XGBC_Best, x_train_,y_train_, x_test_,y_test_)

else:            #T2、采用K折交叉训练

   # train_test_split

   x_train_, x_test_, y_train_, y_test_ = train_test_split(X_train,  y_train, test_size=0.3, random_state=33)

   ModelC = ModelC_Train(XGBC_Best, x_train_, x_test_, y_train_, y_test_)

继续阅读