K折交叉验证:sklearn.model_selection.KFold(n_splits=n, shuffle=False, random_state=None)
思路:将训练/测试数据集划分为n个互斥子集,每次用其中一个子集当作验证集,剩下的n-1个作为训练集,进行n次训练和测试,得到n个结果
注:对于不能均等份的数据集,其前n_samples % n子集拥有int(n_samples / n)+ 1个样本,其余子集都只有int(n_samples / n)样本
参数说明:
n_splits:表示划分几等份
shuffle:在每次划分时,是否进行洗牌(默认为False)
①若为Falses时,其效果等同于random_state等于整数,每次划分的结果相同
②若为True时,每次划分的结果都不一样,表示经过洗牌,随机取样的
random_state:随机种子数
属性:
①get_n_splits(X=None, y=None, groups=None):获取参数n_splits的值
②split(X, y=None, groups=None):将数据集划分成训练集和测试集,返回索引生成器
实例:
①设置shuffle=False,运行两次结果相同
from sklearn.model_selection import KFold
import numpy as np
X = np.arange(20).reshape(5, 4)
y = np.random.randint(1, 2, 5)
# first
kf = KFold(n_splits=5)
for train_index, test_index in kf.split(X):
print('train_index:%s , test_index: %s ' % (train_index, test_index))
train_index:[1 2 3 4] , test_index: [0]
train_index:[0 2 3 4] , test_index: [1]
train_index:[0 1 3 4] , test_index: [2]
train_index:[0 1 2 4] , test_index: [3]
train_index:[0 1 2 3] , test_index: [4]
# second
kf = KFold(n_splits=5, shuffle=False)
for train_index, test_index in kf.split(X):
print('train_index:%s , test_index: %s ' % (train_index, test_index))
train_index:[1 2 3 4] , test_index: [0]
train_index:[0 2 3 4] , test_index: [1]
train_index:[0 1 3 4] , test_index: [2]
train_index:[0 1 2 4] , test_index: [3]
train_index:[0 1 2 3] , test_index: [4]
②设置shuffle=True时,两次运行的结果不同
from sklearn.model_selection import KFold
import numpy as np
X = np.arange(20).reshape(5, 4)
y = np.random.randint(1, 2, 5)
# first
kf = KFold(n_splits=5,shuffle=True)
for train_index, test_index in kf.split(X):
print('train_index:%s , test_index: %s ' % (train_index, test_index))
train_index:[0 1 2 4] , test_index: [3]
train_index:[0 2 3 4] , test_index: [1]
train_index:[0 1 2 3] , test_index: [4]
train_index:[0 1 3 4] , test_index: [2]
train_index:[1 2 3 4] , test_index: [0]
#second
kf = KFold(n_splits=5,shuffle=True)
for train_index, test_index in kf.split(X):
print('train_index:%s , test_index: %s ' % (train_index, test_index))
train_index:[0 1 3 4] , test_index: [2]
train_index:[0 2 3 4] , test_index: [1]
train_index:[0 1 2 4] , test_index: [3]
train_index:[0 1 2 3] , test_index: [4]
train_index:[1 2 3 4] , test_index: [0]
③设置shuffle=True和random_state=整数,每次运行的结果都相同
from sklearn.model_selection import KFold
import numpy as np
X = np.arange(20).reshape(5, 4)
y = np.random.randint(1, 2, 5)
# first
kf = KFold(n_splits=5,shuffle=True,random_state=1)
for train_index, test_index in kf.split(X):
print('train_index:%s , test_index: %s ' % (train_index, test_index))
train_index:[0 1 3 4] , test_index: [2]
train_index:[0 2 3 4] , test_index: [1]
train_index:[0 1 2 3] , test_index: [4]
train_index:[1 2 3 4] , test_index: [0]
train_index:[0 1 2 4] , test_index: [3]
# second
kf = KFold(n_splits=5,shuffle=True,random_state=1)
for train_index, test_index in kf.split(X):
print('train_index:%s , test_index: %s ' % (train_index, test_index))
train_index:[0 1 3 4] , test_index: [2]
train_index:[0 2 3 4] , test_index: [1]
train_index:[0 1 2 3] , test_index: [4]
train_index:[1 2 3 4] , test_index: [0]
train_index:[0 1 2 4] , test_index: [3]
④n_splits属性值获取
kf.split(X)
<generator object _BaseKFold.split at 0x000002153CACE970>
kf.get_n_splits()
5
kf.n_splits
5
最后
以上就是烂漫鞋子最近收集整理的关于K折交叉验证(KFold)的全部内容,更多相关K折交叉验证(KFold)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复