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]

最新文章

  1. membership与成员资格
  2. std::string的split函数
  3. windows下搭建scrapywindows 7 (64) + python 3.5 (64)
  4. 【转】MySQL的Replace into 与Insert into on duplicate key update真正的不同之处
  5. java读取文件批量插入记录
  6. idea快捷键(转)
  7. SpringMVC,MyBatis商品的增删改查
  8. Android 百度地图 SDK v3.0.0 (三) 添加覆盖物Marker与InfoWindow的使用
  9. 我是如何开发一个连锁企业的信息系统的,NO.1
  10. C# - 使用 List<> 泛型给GridView控件数据
  11. poj1269
  12. tomcat原理(二)
  13. Java集合--TreeSet详细解析
  14. numpy(五)
  15. Kali学习笔记37:APPSCAN
  16. canvas-star3
  17. sencha touch 在新版谷歌浏览器中painted事件无法触发解决方案以及carousel 控件、togglefield控件、滚动条失效
  18. executeBatch()批量执行Sql语句
  19. wordpress改不了固定连接的解决办法
  20. c# 以多个字符串分隔字符串数据 分组 分隔 split 正则分组

热门文章

  1. 我们到底为什么要用 IoC 和 AOP
  2. Flink-v1.12官方网站翻译-P002-Fraud Detection with the DataStream API
  3. Yacc使用优先级
  4. Java排序算法(四)希尔排序1
  5. 2021年的十五个DevOps趋势预测
  6. 【uva 1658】Admiral(图论--网络流 最小费用最大流)
  7. int和longlong的范围
  8. Python实现AES的CBC模式加密和解密过程详解 和 chr() 函数 和 s[a:b:c] 和函数lambda
  9. poj2411 Mondriaan's Dream (用1*2的矩形铺)
  10. Musical Theme POJ - 1743 后缀数组