gpt4 book ai didi

python - scikit-learn:为什么这个 2 折交叉验证图看起来像 4 折交叉验证?

转载 作者:太空宇宙 更新时间:2023-11-03 20:27:49 25 4
gpt4 key购买 nike

doc用这个图来说明2折交叉验证

enter image description here

很明显测试集占了1/4,虽然代码是n_splits=2

>>> import numpy as np
>>> from sklearn.model_selection import KFold

>>> X = ["a", "b", "c", "d"]
>>> kf = KFold(n_splits=2)
>>> for train, test in kf.split(X):
... print("%s %s" % (train, test))
[2 3] [0 1]
[0 1] [2 3]

为什么这个图看起来像4折交叉验证?这是一个不匹配的数字吗?

最佳答案

图片来自4交叉验证,你是对的。根据您的代码片段,您有两部分。

它看起来与 Kfold 的文档类似:​​

>>> import numpy as np
>>> from sklearn.model_selection import KFold
>>> X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
>>> y = np.array([1, 2, 3, 4])
>>> kf = KFold(n_splits=2)
>>> kf.get_n_splits(X)
2
>>> print(kf)
KFold(n_splits=2, random_state=None, shuffle=False)
>>> for train_index, test_index in kf.split(X):
... print("TRAIN:", train_index, "TEST:", test_index)
... X_train, X_test = X[train_index], X[test_index]
... y_train, y_test = y[train_index], y[test_index]
TRAIN: [2 3] TEST: [0 1]
TRAIN: [0 1] TEST: [2 3]

关于python - scikit-learn:为什么这个 2 折交叉验证图看起来像 4 折交叉验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57702179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com