我很难理解科学知识-学习的StratifiedKfold从validation.html#stratification
并通过添加RandomOversample实现了示例部分。
X, y = np.ones((50, 1)), np.hstack(([0] * 45, [1] * 5))
from imblearn.over_sampling import RandomOverSampler
ros = RandomOverSampler(sampling_strategy='minority',random_state=0)
X_ros, y_ros = ros.fit_sample(X, y)
skf = StratifiedKFold(n_splits=5,shuffle = True)
for train, test in skf.split(X_ros, y_ros):
print('train - {} | test - {}'.format(
np.bincount(y_ros[train]), np.bincount(y_ros[test])))
print(f"y_ros_test {y_ros[test]}")输出
train - [36 36] | test - [9 9]
y_ros_test [0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1]
train - [36 36] | test - [9 9]
y_ros_test [0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1]
train - [36 36] | test - [9 9]
y_ros_test [0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1]
train - [36 36] | test - [9 9]
y_ros_test [0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1]
train - [36 36] | test - [9 9]
y_ros_test [0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1]我的问题:
y_ros_test时,为什么要用9 0's和9 1's来获取n_splits=5?根据探索,它应该是50/5 = 10,所以不是5 1's和5 0's在每个分裂?发布于 2021-01-06 19:38:56
关于你的第一个问题:当使用交叉验证( CV )时,没有任何的训练测试分裂;所发生的是,在每一轮简历中,一次被用作测试集,其余作为训练。因此,当n_splits=5,如这里,在每轮1/5 (即20%)的数据被用作测试集,其余的4/5 (即80%)用于培训。因此,是的,确定n_splits参数唯一地定义了拆分,并且不需要任何进一步的确定(对于n_splits=4,您将得到75-25拆分)。
关于你的第二个问题,你似乎忘记了,在分裂之前,你的数据被高估了。使用初始X和y (即没有过采样)运行代码确实会得到大小为50/5 = 10的y_test,尽管这是不平衡的(平衡是过采样的结果),但分层(每个折叠保留原始数据的类类比):
skf = StratifiedKFold(n_splits=5,shuffle = True)
for train, test in skf.split(X, y):
print('train - {} | test - {}'.format(
np.bincount(y[train]), np.bincount(y[test])))
print(f"y_test {y[test]}")结果:
train - [36 4] | test - [9 1]
y_test [0 0 0 0 0 0 0 0 0 1]
train - [36 4] | test - [9 1]
y_test [0 0 0 0 0 0 0 0 0 1]
train - [36 4] | test - [9 1]
y_test [0 0 0 0 0 0 0 0 0 1]
train - [36 4] | test - [9 1]
y_test [0 0 0 0 0 0 0 0 0 1]
train - [36 4] | test - [9 1]
y_test [0 0 0 0 0 0 0 0 0 1]由于过度采样少数类实际上增加了数据集的大小,因此只需要得到一个与y_test相关的更大的y_test(这里是18个样本而不是10个)。
从方法上讲,如果您已经对数据进行过抽样以平衡类表示,则实际上不需要分层抽样。
https://stackoverflow.com/questions/65600245
复制相似问题