use*_*ser 10 python cross-validation kaggle
我正在看这个教程:https://www.dataquest.io/mission/74/getting-started-with-kaggle
我得到第9部分,做出预测.在那里,在一个名为titanic的数据框中有一些数据,然后使用以下方式将其分成折叠:
# Generate cross validation folds for the titanic dataset. It return the row indices corresponding to train and test.
# We set random_state to ensure we get the same splits every time we run this.
kf = KFold(titanic.shape[0], n_folds=3, random_state=1)
Run Code Online (Sandbox Code Playgroud)
我不确定它究竟在做什么以及kf是什么样的对象.我试过阅读文档,但没有多大帮助.此外,有三个折叠(n_folds = 3),为什么以后只能访问火车和测试(我怎么知道它们被称为火车和测试)?
for train, test in kf:
Run Code Online (Sandbox Code Playgroud)
qma*_*ruf 15
KFold将提供列车/测试索引,以便在列车和测试集中分割数据.它会将数据集拆分为k连续的折叠(默认情况下不进行洗牌).然后每个折叠使用一次验证集,而k - 1剩余的折叠形成训练集(源).
比方说,你有一些从1到10的数据索引.如果你使用n_fold=k,在第一次迭代中,你将得到i' (i<=k)折叠作为测试指数和剩余(k-1)折叠(没有那个i折叠)作为列车索引.
一个例子
import numpy as np
from sklearn.cross_validation import KFold
x = [1,2,3,4,5,6,7,8,9,10,11,12]
kf = KFold(12, n_folds=3)
for train_index, test_index in kf:
print (train_index, test_index)
Run Code Online (Sandbox Code Playgroud)
产量
折叠1:[4 5 6 7 8 9 10 11] [0 1 2 3]
折叠2:[0 1 2 3 8 9 10 11] [4 5 6 7]
折叠3:[0 1 2 3 4 5 6 7] [8 9 10 11]
| 归档时间: |
|
| 查看次数: |
16113 次 |
| 最近记录: |