留一法交叉验证 Leave-One-Out Cross Validation

简介: 留一法交叉验证 Leave-One-Out Cross Validation

交叉验证法,就是把一个大的数据集分为 k k k 个小数据集,其中 k − 1 k-1 k−1 个作为训练集,剩下的 1 1 1 个作为测试集,在训练和测试的时候依次选择训练集和它对应的测试集。这种方法也被叫做 k k k 折交叉验证法(k-fold cross validation)。最终的结果是这 k 次验证的均值。


此外,还有一种交叉验证方法就是 留一法(Leave-One-Out,简称LOO),顾名思义,就是使 k k k 等于数据集中数据的个数,每次只使用一个作为测试集,剩下的全部作为训练集,这种方法得出的结果与训练整个测试集的期望值最为接近,但是成本过于庞大。


我们用SKlearn库来实现一下LOO:


from sklearn.model_selection import LeaveOneOut


# 一维示例数据
data_dim1 = [1, 2, 3, 4, 5]
# 二维示例数据
data_dim2 = [[1, 1, 1, 1],
             [2, 2, 2, 2],
             [3, 3, 3, 3],
             [4, 4, 4, 4],
             [5, 5, 5, 5]]
loo = LeaveOneOut() # 实例化LOO对象
# 取LOO训练、测试集数据索引
for train_idx, test_idx in loo.split(data_dim1):
    # train_idx 是指训练数据在总数据集上的索引位置
    # test_idx 是指测试数据在总数据集上的索引位置
    print("train_index: %s, test_index %s" % (train_idx, test_idx))
# 取LOO训练、测试集数据值
for train_idx, test_idx in loo.split(data_dim1):
    # train_idx 是指训练数据在总数据集上的索引位置
    # test_idx 是指测试数据在总数据集上的索引位置
    train_data = [data_dim1[i] for i in train_idx]
    test_data = [data_dim1[i] for i in test_idx]
    print("train_data: %s, test_data %s" % (train_data, test_data))


data_dim1的输出:


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]
train_data: [2, 3, 4, 5], test_data [1]
train_data: [1, 3, 4, 5], test_data [2]
train_data: [1, 2, 4, 5], test_data [3]
train_data: [1, 2, 3, 5], test_data [4]
train_data: [1, 2, 3, 4], test_data [5]


data_dim2的输出:


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]
train_data: [[2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5]], test_data [[1, 1, 1, 1]]
train_data: [[1, 1, 1, 1], [3, 3, 3, 3], [4, 4, 4, 4], [5, 5, 5, 5]], test_data [[2, 2, 2, 2]]
train_data: [[1, 1, 1, 1], [2, 2, 2, 2], [4, 4, 4, 4], [5, 5, 5, 5]], test_data [[3, 3, 3, 3]]
train_data: [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [5, 5, 5, 5]], test_data [[4, 4, 4, 4]]
train_data: [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]], test_data [[5, 5, 5, 5]]
相关文章
|
PyTorch 算法框架/工具
Pytorch中Trying to backward through the graph和one of the variables needed for gradient错误解决方案
Pytorch中Trying to backward through the graph和one of the variables needed for gradient错误解决方案
2313 0
Pytorch中Trying to backward through the graph和one of the variables needed for gradient错误解决方案
|
6月前
|
TensorFlow API 算法框架/工具
【Tensorflow】解决Inputs to eager execution function cannot be Keras symbolic tensors, but found [<tf.Te
文章讨论了在使用Tensorflow 2.3时遇到的一个错误:"Inputs to eager execution function cannot be Keras symbolic tensors...",这个问题通常与Tensorflow的eager execution(急切执行)模式有关,提供了三种解决这个问题的方法。
66 1
|
机器学习/深度学习 TensorFlow 算法框架/工具
交叉验证(Cross-Validation)
交叉验证(Cross-Validation)是一种常用的评估机器学习模型性能的技术。它通过将数据集分为训练集和验证集,并多次重复这个过程,以获得对模型性能的更准确估计。
296 2
|
监控
DFNet: Enhance Absolute Pose Regression withDirect Feature Matching
DFNet: Enhance Absolute Pose Regression withDirect Feature Matching
179 0
|
机器学习/深度学习 存储 数据挖掘
Global Constraints with Prompting for Zero-Shot Event Argument Classification 论文解读
确定事件论元的角色是事件抽取的关键子任务。大多数以前的监督模型都利用了昂贵的标注,这对于开放域应用程序是不实际的。
86 0
|
SQL PyTorch 算法框架/工具
pytorch损失函数binary_cross_entropy和binary_cross_entropy_with_logits的区别
binary_cross_entropy和binary_cross_entropy_with_logits都是来自torch.nn.functional的函数
1672 0
sklearn中的cross_val_score交叉验证
sklearn中的cross_val_score交叉验证
169 0
|
PyTorch 算法框架/工具
Pytorch Loss Functions总结
Pytorch Loss Functions总结
189 0
|
机器学习/深度学习 异构计算
COVID-19 Cases Prediction (Regression)(二)
COVID-19 Cases Prediction (Regression)
496 0
COVID-19 Cases Prediction (Regression)(二)