sklearn cross validation_python sklearn
sklearn,cross,validation,python
2025-03-13 21:27:30 时间
KFold通过提供index来给你确定不同组的训练集以及测试的index,来构造交叉验证数据集。
参数(n, n_folds=3, shuffle=False, random_state=None)
n为总数
n_folds为分为多少个交叉验证集
shuffle为是否随机
random_state设置随机因子
from sklearn.cross_validation import KFold
import numpy as np
X = np.arange(24).reshape(12,2)
X
array([[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11],
[12, 13],
[14, 15],
[16, 17],
[18, 19],
[20, 21],
[22, 23]])
1.shuffle=False
kf = KFold(12,n_folds=5,shuffle=False)
for i,(train_index,test_index) in enumerate(kf):
print(i,train_index,test_index)
0 [ 3 4 5 6 7 8 9 10 11] [0 1 2]
1 [ 0 1 2 6 7 8 9 10 11] [3 4 5]
2 [ 0 1 2 3 4 5 8 9 10 11] [6 7]
3 [ 0 1 2 3 4 5 6 7 10 11] [8 9]
4 [0 1 2 3 4 5 6 7 8 9] [10 11]
kf = KFold(12,n_folds=5,shuffle=False)
for i,(train_index,test_index) in enumerate(kf):
print(i,train_index,test_index)
0 [ 3 4 5 6 7 8 9 10 11] [0 1 2]
1 [ 0 1 2 6 7 8 9 10 11] [3 4 5]
2 [ 0 1 2 3 4 5 8 9 10 11] [6 7]
3 [ 0 1 2 3 4 5 6 7 10 11] [8 9]
4 [0 1 2 3 4 5 6 7 8 9] [10 11]
2.shuffle=True,俩次不同了
kf = KFold(12,n_folds=5,shuffle=True)
for i,(train_index,test_index) in enumerate(kf):
print(i,train_index,test_index)
0 [ 0 1 2 3 4 6 7 8 11] [ 5 9 10]
1 [ 0 2 3 4 5 8 9 10 11] [1 6 7]
2 [ 0 1 2 3 4 5 6 7 9 10] [ 8 11]
3 [ 0 1 2 5 6 7 8 9 10 11] [3 4]
4 [ 1 3 4 5 6 7 8 9 10 11] [0 2]
kf = KFold(12,n_folds=5,shuffle=True)
for i,(train_index,test_index) in enumerate(kf):
print(i,train_index,test_index)
0 [ 0 3 4 6 7 8 9 10 11] [1 2 5]
1 [ 1 2 5 6 7 8 9 10 11] [0 3 4]
2 [ 0 1 2 3 4 5 8 9 10 11] [6 7]
3 [ 0 1 2 3 4 5 6 7 8 10] [ 9 11]
4 [ 0 1 2 3 4 5 6 7 9 11] [ 8 10]
3.shuffle=True,random_state赋值,俩次又相同了
kf = KFold(12, n_folds=5, shuffle=True, random_state=5)
for i,(train_index,test_index) in enumerate(kf):
print(i,train_index,test_index)
0 [ 0 1 3 4 6 8 9 10 11] [2 5 7]
1 [ 0 1 2 3 5 6 7 8 10] [ 4 9 11]
2 [ 0 2 3 4 5 6 7 9 10 11] [1 8]
3 [ 1 2 3 4 5 6 7 8 9 11] [ 0 10]
4 [ 0 1 2 4 5 7 8 9 10 11] [3 6]
kf = KFold(12, n_folds=5, shuffle=True, random_state=5)
for i,(train_index,test_index) in enumerate(kf):
print(i,train_index,test_index)
0 [ 0 1 3 4 6 8 9 10 11] [2 5 7]
1 [ 0 1 2 3 5 6 7 8 10] [ 4 9 11]
2 [ 0 2 3 4 5 6 7 9 10 11] [1 8]
3 [ 1 2 3 4 5 6 7 8 9 11] [ 0 10]
4 [ 0 1 2 4 5 7 8 9 10 11] [3 6]
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/193297.html原文链接:https://javaforall.cn
相关文章
- python线性回归算法「建议收藏」
- 使用 setup.py 将 Python 库打包分发到 PyPI 踩坑指南
- Python装饰器详解
- python安装虚拟环境步骤_python虚拟环境迁移
- pycharm执行代码快捷键_python 函数调用
- termux更改镜像源_pycharm自带python
- python 快速读取压缩包内文件
- pycharm断点运行_python断点调试技巧
- 为什么python读取不了文件_python系统找不到指定文件怎么办
- Python 模板渲染库 yaml 和 jinja2 的实战经验分享
- pycharm中使用anaconda部署python环境_anaconda虚拟环境是什么
- Pycharm和Anaconda的python版本问题
- pycharm 风格_python主题更改
- python执行cmd命令并解析结果_python如何打包成可执行程序
- pycharm版本区别_怎么看pycharm的python版本
- python读写json_python格式化json
- Python基础18-异常处理
- python 变量与数据类型
- Python 命令行参数的3种传入方式
- python+pycharm安装_pycharm安装教程2020