我执行一个10倍交叉验证的回归模型。
for train, test in kf.split(X, Y):
print ("Fold ", cv)
print("Train", X[train].shape)
print("Test", X[test].shape)
# define the model
Breg = BayesianRidge(n_iter = 500, tol=0.0000000001)
# fit the data to the model
Breg.fit(X[train], Y[train])
# calculate R2 for each fold and save the value into a file
R2.append(Breg.score(X[test], Y[test]))
# predict in test set
ypred_test = Breg.predict(X[test])
Y_pred_test.append(ypred_test)
# calculate mean squared error for each fold and save into a list
mae.append(mean_absolute_error(Y[test], ypred_test))当我运行模型时,我观察到训练和测试的规模发生了变化。
Fold 1
Train (14754, 9)
Test (1640, 9)
Fold 2
Train (14754, 9)
Test (1640, 9)
Fold 3
Train (14754, 9)
Test (1640, 9)
Fold 4
Train (14754, 9)
Test (1640, 9)
Fold 5
Train (14755, 9)
Test (1639, 9)
Fold 6
Train (14755, 9)
Test (1639, 9)
Fold 7
Train (14755, 9)
Test (1639, 9)
Fold 8
Train (14755, 9)
Test (1639, 9)
Fold 9
Train (14755, 9)
Test (1639, 9)
Fold 10
Train (14755, 9)
Test (1639, 9)你可以看到,在第5折之后,训练的规模增加了1,而测试的规模减少了1。
你知道这是怎么发生的吗?
提前感谢
发布于 2020-08-28 06:34:49
答案可以在文档 of KFold中找到,我认为它是您在kf.split中的kf所代表的。
在注释中,它说:
第一个
n_samples % n_splits褶皱有n_samples // n_splits + 1大小,其他褶皱有n_samples // n_splits大小,其中n_samples是样本数。
通过插入数字,您可以看到前4个分叉的大小为n_samples // n_splits + 1,其余的为n_samples // n_splits大小,因此正好是+1的大小差异。
https://stackoverflow.com/questions/63623602
复制相似问题