首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >交叉验证时改变测试和训练的大小

交叉验证时改变测试和训练的大小
EN

Stack Overflow用户
提问于 2020-08-27 20:17:10
回答 1查看 246关注 0票数 0

我执行一个10倍交叉验证的回归模型。

代码语言:javascript
复制
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))

当我运行模型时,我观察到训练和测试的规模发生了变化。

代码语言:javascript
复制
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。

你知道这是怎么发生的吗?

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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的大小差异。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63623602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档