留一法交叉验证 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错误解决方案
1451 0
Pytorch中Trying to backward through the graph和one of the variables needed for gradient错误解决方案
|
9月前
|
机器学习/深度学习 TensorFlow 算法框架/工具
交叉验证(Cross-Validation)
交叉验证(Cross-Validation)是一种常用的评估机器学习模型性能的技术。它通过将数据集分为训练集和验证集,并多次重复这个过程,以获得对模型性能的更准确估计。
134 2
|
9月前
|
监控
DFNet: Enhance Absolute Pose Regression withDirect Feature Matching
DFNet: Enhance Absolute Pose Regression withDirect Feature Matching
79 0
|
9月前
|
机器学习/深度学习 存储 数据挖掘
Global Constraints with Prompting for Zero-Shot Event Argument Classification 论文解读
确定事件论元的角色是事件抽取的关键子任务。大多数以前的监督模型都利用了昂贵的标注,这对于开放域应用程序是不实际的。
51 0
|
9月前
|
机器学习/深度学习 自然语言处理 算法
Joint Information Extraction with Cross-Task and Cross-Instance High-Order Modeling 论文解读
先前的信息抽取(IE)工作通常独立地预测不同的任务和实例(例如,事件触发词、实体、角色、关系),而忽略了它们的相互作用,导致模型效率低下。
58 0
|
PyTorch 算法框架/工具
Please ensure they have the same size. return F.mse_loss(input, target, reduction=self.reduction) 怎么解决?
这个通常是由于 input 和 target 张量的维度不匹配导致的,因此可以通过调整它们的维度来解决。
235 0
sklearn中的cross_val_score交叉验证
sklearn中的cross_val_score交叉验证
|
SQL PyTorch 算法框架/工具
pytorch损失函数binary_cross_entropy和binary_cross_entropy_with_logits的区别
binary_cross_entropy和binary_cross_entropy_with_logits都是来自torch.nn.functional的函数
1392 0
|
机器学习/深度学习 自然语言处理 数据挖掘

热门文章

最新文章