赞
踩
目录
1. K折交叉验证(K-fold cross-validation)
2. 分层K折交叉验证(Stratified K-fold cross-validation)
3. 时间序列交叉验证(Time Series Split)
4. 留一交叉验证(Leave-One-Out Cross-Validation,LOOCV)
5. 留P交叉验证(Leave-P-Out Cross-Validation,LPOCV)
6. 重复K折交叉验证(Repeated K-Fold Cross-Validation)
7. 留出交叉验证(Holdout Cross-Validation)
8. 自助采样交叉验证(Bootstrap Cross-Validation)
9. 蒙特卡洛交叉验证(Monte Carlo Cross-Validation)
10. 重复随机子采样交叉验证(Repeated Random Subsampling Cross-Validation)
方法 | 任务类型 | 划分方式 | 计算量 | 优点 | 缺点 | 适用场景 |
---|---|---|---|---|---|---|
K折交叉验证 | 通用 | 将数据集分为 K 个子集,每次选取一个子集作为验证集 | 中等 | 充分利用数据,可以更准确地评估模型性能 | 计算量较大 | 通用 |
分层K折交叉验证 | 通用 | 类似于 K 折交叉验证,但保持每个折中类别比例 | 中等 | 对于不平衡数据集,保证了每个折中的类别比例 | 计算量较大 | 数据不平衡时 |
时间序列交叉验证 | 时间序列数据 | 根据时间顺序划分数据,保证训练集在测试集之前 | 低 | 考虑了时间序列数据的顺序性,适合于时间序列预测任务 | 不适用于非时间序列数据 | 时间序列预测任务 |
留一交叉验证 | 通用 | 每次将一个样本作为验证集 | 高 | 最大程度利用数据,评估结果准确 | 计算量巨大,效率低下 | 数据集较小时 |
留P交叉验证 | 通用 | 每次留下 P 个样本作为验证集 | 高 | 可以自定义留下的样本数量 P,适用于不同情况 | 计算量较大,效率低下 | 数据集较小时 |
重复K折交叉验证 | 通用 | 对 K 折交叉验证进行多次重复 | 高 | 提供更稳健的评估结果,减少因随机性引起的评估误差 | 计算量更大 | 需要更加稳健的评估结果 |
留出交叉验证 | 通用 | 将数据集划分为训练集和测试集,通常使用固定比例 | 低 | 计算量低,简单易用 | 不充分利用数据 | 数据集较大时 |
自助采样交叉验证 | 通用 | 通过自助采样的方式随机采样训练集 | 高 | 充分利用数据,对于小样本数据集效果好 | 计算量较大,可能会产生相似的训练样本,引入估计偏差 | 数据集较小,或者需要处理小样本数据时 |
蒙特卡洛交叉验证 | 通用 | 随机重复采样和验证 | 高 | 可以得到对数据的全面评估 | 计算量非常大 | 需要对模型进行全面评估时 |
重复随机子采样交叉验证 | 通用 | 通过随机子采样的方式重复采样训练集 | 高 | 充分利用数据,减少因样本选择的随机性引起的评估误差 | 计算量较大,可能会产生相似的训练样本,引入估计偏差 | 数据集较小,或者需要处理小样本数据时 |
- import numpy as np
- from sklearn.model_selection import KFold
-
- # 创建数据集
- X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
- y = np.array([1, 2, 3, 4])
-
- # 创建K折交叉验证对象
- kf = KFold(n_splits=2, shuffle=True)
-
- # 遍历每一次交叉验证
- for train_index, test_index in kf.split(X):
- X_train, X_test = X[train_index], X[test_index]
- y_train, y_test = y[train_index], y[test_index]
- # 在此处训练和评估模型
- import numpy as np
- from sklearn.model_selection import StratifiedKFold
-
- # 创建数据集
- X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
- y = np.array([0, 0, 1, 1])
-
- # 创建分层K折交叉验证对象
- skf = StratifiedKFold(n_splits=2, shuffle=True)
-
- # 遍历每一次交叉验证
- for train_index, test_index in skf.split(X, y):
- X_train, X_test = X[train_index], X[test_index]
- y_train, y_test = y[train_index], y[test_index]
- # 在此处训练和评估模型
- import numpy as np
- from sklearn.model_selection import TimeSeriesSplit
-
- # 创建时间序列数据集
- X = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]])
- y = np.array([1, 2, 3, 4, 5])
-
- # 创建时间序列交叉验证对象
- tscv = TimeSeriesSplit(n_splits=3)
-
- # 遍历每一次交叉验证
- for train_index, test_index in tscv.split(X):
- X_train, X_test = X[train_index], X[test_index]
- y_train, y_test = y[train_index], y[test_index]
- # 在此处训练和评估模型
- import numpy as np
- from sklearn.model_selection import LeaveOneOut
-
- # 创建数据集
- X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
- y = np.array([1, 2, 3, 4])
-
- # 创建Leave-One-Out交叉验证对象
- loo = LeaveOneOut()
-
- # 遍历每一次交叉验证
- for train_index, test_index in loo.split(X):
- X_train, X_test = X[train_index], X[test_index]
- y_train, y_test = y[train_index], y[test_index]
- # 在此处训练和评估模型
- import numpy as np
- from sklearn.model_selection import LeavePOut
-
- # 创建数据集
- X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
- y = np.array([1, 2, 3, 4])
-
- # 创建Leave-P-Out交叉验证对象
- lpocv = LeavePOut(p=2)
-
- # 遍历每一次交叉验证
- for train_index, test_index in lpocv.split(X):
- X_train, X_test = X[train_index], X[test_index]
- y_train, y_test = y[train_index], y[test_index]
- # 在此处训练和评估模型
- import numpy as np
- from sklearn.model_selection import RepeatedKFold
-
- # 创建数据集
- X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
- y = np.array([1, 2, 3, 4])
-
- # 创建Repeated K-fold交叉验证对象
- rkf = RepeatedKFold(n_splits=2, n_repeats=2)
-
- # 遍历每一次交叉验证
- for train_index, test_index in rkf.split(X):
- X_train, X_test = X[train_index], X[test_index]
- y_train, y_test = y[train_index], y[test_index]
- # 在此处训练和评估模型
- import numpy as np
- from sklearn.model_selection import train_test_split
-
- # 创建数据集
- X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
- y = np.array([1, 2, 3, 4])
-
- # 划分训练集和测试集
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
-
- import numpy as np
- from sklearn.utils import resample
-
- # 创建数据集
- X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
- y = np.array([1, 2, 3, 4])
-
- # 创建Bootstrap交叉验证对象
- n_iterations = 5
- n_samples = len(X)
- for i in range(n_iterations):
- X_train, X_test, y_train, y_test = resample(X, y, n_samples=n_samples, replace=True)
- import numpy as np
- from sklearn.model_selection import ShuffleSplit
-
- # 创建数据集
- X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
- y = np.array([1, 2, 3, 4])
-
- # 创建Monte Carlo交叉验证对象
- n_iterations = 5
- ss = ShuffleSplit(n_splits=n_iterations, test_size=0.2, random_state=42)
-
- # 遍历每一次交叉验证
- for train_index, test_index in ss.split(X):
- X_train, X_test = X[train_index], X[test_index]
- y_train, y_test = y[train_index], y[test_index]
- # 在此处训练和评估模型

- import numpy as np
- from sklearn.model_selection import ShuffleSplit, cross_val_score
-
- # 创建数据集
- X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]])
- y = np.array([1, 2, 3, 4])
-
- # 定义重复次数
- n_repeats = 5
-
- # 定义随机子采样交叉验证
- rss_cv = ShuffleSplit(n_splits=10, test_size=0.3, random_state=42)
-
- # 使用交叉验证评估模型
- scores = cross_val_score(model, X, y, cv=rss_cv, n_jobs=-1)
-
- # 输出结果
- for i, score in enumerate(scores):
- print(f"Cross-validation run {i+1}: {score}")

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。