python - scikit-learn:为什么这个 2 折交叉验证图看起来像 4 折交叉验证?
问题描述
该文档使用此图来说明 2 折交叉验证
很明显测试集占了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]
推荐阅读
- apache-spark - 根据 pyspark 中的条件查找 json 列表中的总和
- ios - 初始化'dispatch_source_t'(又名'NSObject)的不兼容指针类型
*') 类型为 'NSString *' 的表达式 - qt - qml MediaPlayer VideoOutput 如何在单独的线程中运行?
- qt - Qt Quick 3D 错误:QSSG.internal_error:找不到适合 QSurfaceFormat 的 OpenGL 版本(版本 2.1
- fgets - fgets() 表现得好像它不在这里
- java - JWT 认证成功,但是邮递员返回 404
- javascript - 进行两次数据库调用并在一个 ejs 页面中呈现由结果填充的两个不同数组的内容
- google-chrome-extension - 如何同时拥有 Chrome 扩展的用户和开发者组
- python - 如何在 Python 上用不同大小的数组列表填充矩阵?
- multithreading - 每个线程都有它的主要功能吗?