Python中的sklearn--KFold与StratifiedKFold
2024-10-19 06:06:42
KFold划分数据集的原理:根据n_split直接进行划分
StratifiedKFold划分数据集的原理:划分后的训练集和验证集中类别分布尽量和原数据集一样
#导入相关packages from sklearn.model_selection import KFold
from sklearn.model_selection import StratifiedKFold
1 import numpy as np
2 from sklearn.model_selection import StratifiedKFold
3 X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
4 y = np.array([0, 0, 1, 1])
5 skf = StratifiedKFold(n_splits=2)
6 skf.get_n_splits(X, y)
7 >>>2
8 print(skf)
9 StratifiedKFold(n_splits=2, random_state=None, shuffle=False)
10 for train_index, test_index in skf.split(X, y):
11 print("TRAIN:", train_index, "TEST:", test_index)
12 X_train, X_test = X[train_index], X[test_index] #返回数据框的方法 X_train = X.iloc[train_index]
13 y_train, y_test = y[train_index], y[test_index]
14 >>>TRAIN: [1 3] TEST: [0 2] #返回的是索引
15 >>>TRAIN: [0 2] TEST: [1 3]
最新文章
- membership与成员资格
- std::string的split函数
- windows下搭建scrapywindows 7 (64) + python 3.5 (64)
- 【转】MySQL的Replace into 与Insert into on duplicate key update真正的不同之处
- java读取文件批量插入记录
- idea快捷键(转)
- SpringMVC,MyBatis商品的增删改查
- Android 百度地图 SDK v3.0.0 (三) 添加覆盖物Marker与InfoWindow的使用
- 我是如何开发一个连锁企业的信息系统的,NO.1
- C# - 使用 List<;>; 泛型给GridView控件数据
- poj1269
- tomcat原理(二)
- Java集合--TreeSet详细解析
- numpy(五)
- Kali学习笔记37:APPSCAN
- canvas-star3
- sencha touch 在新版谷歌浏览器中painted事件无法触发解决方案以及carousel 控件、togglefield控件、滚动条失效
- executeBatch()批量执行Sql语句
- wordpress改不了固定连接的解决办法
- c# 以多个字符串分隔字符串数据 分组 分隔 split 正则分组
热门文章
- 我们到底为什么要用 IoC 和 AOP
- Flink-v1.12官方网站翻译-P002-Fraud Detection with the DataStream API
- Yacc使用优先级
- Java排序算法(四)希尔排序1
- 2021年的十五个DevOps趋势预测
- 【uva 1658】Admiral(图论--网络流 最小费用最大流)
- int和longlong的范围
- Python实现AES的CBC模式加密和解密过程详解 和 chr() 函数 和 s[a:b:c] 和函数lambda
- poj2411 Mondriaan's Dream (用1*2的矩形铺)
- Musical Theme POJ - 1743 后缀数组