Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scikit learn Scikit学习:以时间为单位的测试大小_Scikit Learn - Fatal编程技术网

Scikit learn Scikit学习:以时间为单位的测试大小

Scikit learn Scikit学习:以时间为单位的测试大小,scikit-learn,Scikit Learn,我正在使用Scikit Learn TimeSeriesPlit将我的数据拆分为训练集和测试集。目前,timeSeries数据集的第一次拆分为50%,之后的第二次拆分为30%。我想要一个固定的10%的数据用作测试集 tscv = TimeSeriesSplit(n_splits=3) for train_index, test_index in tscv.split(X): print(train_index, test_index) 输出为: [ 0 1 2 ...,

我正在使用Scikit Learn TimeSeriesPlit将我的数据拆分为训练集和测试集。目前,timeSeries数据集的第一次拆分为50%,之后的第二次拆分为30%。我想要一个固定的10%的数据用作测试集

tscv = TimeSeriesSplit(n_splits=3)
for train_index, test_index in tscv.split(X):
    print(train_index, test_index)
输出为:

[   0    1    2 ..., 1067 1068 1069] [1070 1071 1072 ..., 2136 2137 2138]
[   0    1    2 ..., 2136 2137 2138] [2139 2140 2141 ..., 3205 3206 3207]
[   0    1    2 ..., 3205 3206 3207] [3208 3209 3210 ..., 4274 4275 4276]
我想要这样的东西:
tscv=TimeSeriesSplit(n\u splits=3,test\u size==0.1)
类似于
train\u test\u split


如何只分割10%的条目进行测试?

这能满足您的需要吗?这是一个列/测试拆分,最后10%的行作为测试集

train_rows = round(0.9 * X.shape[0])

X_train = X.loc[:train_rows-1, :]
X_test  = X.loc[train_rows:, :]

assert X_train.shape[0] + X_test.shape[0] == X.shape[0]

没有用于指定百分比的直接参数。但是您可以相应地修改n_分割以获得所需的结果

在:-

在第k次拆分中,它返回第一个k次折叠作为列车组和 (k+1)次折叠作为测试集

现在你想要最后10%作为测试,其余作为训练。因此,使用n_分割=9。然后,在for循环的最后一次迭代中,它将前9次作为序列输出,最后1次作为测试输出

因此,相应地更改代码:

test_size = 0.1

# This conversion is found in the source of TimeSeriesSplit

n_splits = (1//test_size)-1   # using // for integer division

tscv = TimeSeriesSplit(n_splits=n_splits)
for train_index, test_index in tscv.split(X):
    print(train_index, test_index)

    # Read below comments about following code
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]
如果将X_列、X_测试等保持在for循环内,则测试大小将保持在0.1,但列车数据将相应更改(因为在时间序列中,只有测试索引之前的值可以用作列车)

如果这是保持在for回路之外,则只有一组列车和试验,其中0.9列车和0.1试验

编辑: 我说不出他们为什么选择k+1作为测试集。请看一看。 但在试验中,他们使用了根据n_分割计算得出的测试_大小:-

n_samples = _num_samples(X)
n_splits = self.n_splits
n_folds = n_splits + 1
test_size = (n_samples // n_folds)
因此,在下一个版本中,他们可能会将
测试大小
作为参数。
希望这有帮助。如果有任何疑问,请随时在此发表评论。

Timeseriessplit的工作方式与其他cv迭代器不同。你希望你的产出指数是多少?例如,您是否仍然希望第三个列车组是第一个和第二个列车组的超集(因为您使用了timeseriessplit),如文档中所述,`在第k次拆分中,它返回第一个k倍作为列车组,返回第(k+1)倍作为测试集''的原因是什么?为什么它不像
train\u test\u split
那样,有一个
test\u size
?@suku我说不出他们为什么选择k+1作为测试集。请看一看。但在中,他们使用了
测试大小
,根据
n\u分割
计算得出(我在这里使用的反向计算方法用于确定上述答案中的
n\u分割
)。因此,也许在下一版本中,他们可以将
test\u size
作为参数。请编辑您的答案并将上述注释放在其中。真是太好了relevant@VivekKumar,因此,如果您的测试大小是,比如说,0.5,那么我猜代码将无法工作,因为这将导致n_sample=1?@Riley是的,我知道这一点。它被发布为,并希望在未来的版本中得到纠正。如何构建一个验证集?