当前位置:   article > 正文

人工智能 StratifiedKFold_分层采样 机器学习

分层采样 机器学习

1、基础

StratifiedKFold——执行分层采样
sklearn.model_selection.StratifiedKFold(n_splits=,random_state=,shuffle=)
y:样本集标记序列
n:整数,数据集大小
n_flods:整数k,大于等于2
shuffle:布尔值,是否混洗数据
random_state整数——随机数种子,否则为随机数生成器

split(X[,y,groups])
X:训练数据集(n_samples,n_features)
y:标记信息(n_samples,)
划分数据集为训练集、测试集

2、代码

  1. X=np.array([[1,2,3,4],
  2. [11,12,13,14],
  3. [21,22,23,24],
  4. [31,32,33,34],
  5. [41,42,43,44],
  6. [51,52,53,54],
  7. [61,62,63,64],
  8. [71,72,73,74]])
  9. y=np.array([1,1,0,0,1,1,0,0])
  10. # 普通交叉切分
  11. folder=KFold(n_splits=4,shuffle=False)
  12. for train_index,test_index in folder.split(X,y):
  13. print("Train Index:",train_index)
  14. print("Test Index:",test_index)
  15. print("y_train:",y[train_index])
  16. print("y_test:",y[test_index])
  17. print("")
  18. # 分层采样交叉切分
  19. stratified_folder=StratifiedKFold(n_splits=4,shuffle=False)
  20. for train_index,test_index in stratified_folder.split(X,y):
  21. print("Stratified Train Index:",train_index)
  22. print("Stratified Test Index:",test_index)
  23. print("Stratified y_train:",y[train_index])
  24. print("Stratified y_test:",y[test_index])
  25. print("")

3、结果

【out】:

普通交叉切分:
Train Index: [2 3 4 5 6 7]
Test Index: [0 1]
y_train: [0 0 1 1 0 0]
y_test: [1 1]

普通交叉切分:
Train Index: [0 1 4 5 6 7]
Test Index: [2 3]
y_train: [1 1 1 1 0 0]
y_test: [0 0]

普通交叉切分:
Train Index: [0 1 2 3 6 7]
Test Index: [4 5]
y_train: [1 1 0 0 0 0]
y_test: [1 1]

普通交叉切分:
Train Index: [0 1 2 3 4 5]
Test Index: [6 7]
y_train: [1 1 0 0 1 1]
y_test: [0 0]

分层采样交叉切分:
Stratified Train Index: [1 3 4 5 6 7]
Stratified Test Index: [0 2]
Stratified y_train: [1 0 1 1 0 0]
Stratified y_test: [1 0]

分层采样交叉切分:
Stratified Train Index: [0 2 4 5 6 7]
Stratified Test Index: [1 3]
Stratified y_train: [1 0 1 1 0 0]
Stratified y_test: [1 0]

分层采样交叉切分:
Stratified Train Index: [0 1 2 3 5 7]
Stratified Test Index: [4 6]
Stratified y_train: [1 1 0 0 1 0]
Stratified y_test: [1 0]

分层采样交叉切分:
Stratified Train Index: [0 1 2 3 4 6]
Stratified Test Index: [5 7]
Stratified y_train: [1 1 0 0 1 0]
Stratified y_test: [1 0]

4、分析

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/629077
推荐阅读
相关标签
  

闽ICP备14008679号