sklearn的KFold,GroupKFold,StratifiedKFold区分

机器学习模型训练中对数据进行K折交叉训练是一个常见的策略,其中sklean库中的KFold,GroupKFold,StratifiedKFold就是实现对数据集K折划分的方法,这三个方法的区别总结如下。

KFold:是对数据集顺序按顺序K折划分;

GroupKFold:是按训练者自己的需求进行的自定义划分,比如定义需要某些类别在训练集上多点数据;

StratifiedKFold:则是分层采样,确保训练集,验证集中各类别样本的比例与原始数据集中相同;

这三种方法简单的代码示例:

import numpy as np
from sklearn.model_selection import KFold,GroupKFold,StratifiedKFold
X=np.array([[1,2],[3,4],[5,6],[7,8],[9,10],[11,12],[13,14],[15,16],[17,18],[19,20]])
y=np.array([1,1,1,1,2,2,2,3,3,3])

#KFold
print('KFlod:')
kf=KFold(n_splits=5)
for train_index,test_index in kf.split(X):
    print("Train Index:",train_index,",Test Index:",test_index)
    X_train,X_test=X[train_index],X[test_index]
    y_train,y_test=y[train_index],y[test_index]

# GroupKFold
print('GroupKFold:')
groups=np.array([1,1,1,1,2,2,2,3,3,3])
group_kfold=GroupKFold(n_splits=3)
for train_index,test_index in group_kfold.split(X,y,groups):
    print("Train Index:",train_index,",Test Index:",test_index)
    X_train,X_test=X[train_index],X[test_index]
    y_train,y_test=y[train_index],y[test_index]

# StratifiedKFold
print('StratifiedKFold:')
skf=StratifiedKFold(n_splits=3)
for train_index,test_index in skf.split(X,y):
    print("Train Index:",train_index,",Test Index:",test_index)
    X_train,X_test=X[train_index],X[test_index]
    y_train,y_test=y[train_index],y[test_index]


运行结果如下,打印了每次K折划分中数据集的index:

Train Index: [2 3 4 5 6 7 8 9] ,Test Index: [0 1]
Train Index: [0 1 4 5 6 7 8 9] ,Test Index: [2 3]
Train Index: [0 1 2 3 6 7 8 9] ,Test Index: [4 5]
Train Index: [0 1 2 3 4 5 8 9] ,Test Index: [6 7]
Train Index: [0 1 2 3 4 5 6 7] ,Test Index: [8 9]
GroupKFold:
Train Index: [4 5 6 7 8 9] ,Test Index: [0 1 2 3]
Train Index: [0 1 2 3 4 5 6] ,Test Index: [7 8 9]
Train Index: [0 1 2 3 7 8 9] ,Test Index: [4 5 6]
StratifiedKFold:
Train Index: [2 3 5 6 8 9] ,Test Index: [0 1 4 7]
Train Index: [0 1 3 4 6 7 9] ,Test Index: [2 5 8]
Train Index: [0 1 2 4 5 7 8] ,Test Index: [3 6 9]

Process finished with exit code 0

参考:https://www.cnblogs.com/nolonely/p/7007432.html

猜你喜欢

转载自blog.csdn.net/u010420283/article/details/106161468