当前位置:   article > 正文

pyhton中K折交叉验证StratifiedKFold_stratifiedkfoldpython

stratifiedkfoldpython

https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.StratifiedKFold.html#sklearn.model_selection.StratifiedKFold

  1. from sklearn.model_selection import StratifiedKFold
  2. X = np.array([[1, 2], [3, 4], [1, 2], [3, 4], [1, 2], [3, 4], [1, 2], [3, 4], [1, 2], [3, 4], [1, 2], [3, 4]])
  3. y = np.array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2])
  4. #y = np.hstack(([0]*4,[1]*4,[2]*4))
  5. skf = StratifiedKFold(n_splits=3, random_state=None, shuffle=True)
  6. skf.get_n_splits(X, y)
  7. print(skf)
  8. for train_index, test_index in skf.split(X, y):
  9. print("TRAIN:", train_index, "TEST:", test_index)
  10. X_train, X_test = X[train_index], X[test_index]
  11. y_train, y_test = y[train_index], y[test_index]
  12. StratifiedKFold(n_splits=3, random_state=None, shuffle=True)
  13. TRAIN: [ 1 3 6 7 8 10] TEST: [ 0 2 4 5 9 11]
  14. TRAIN: [ 0 1 2 4 5 7 8 9 11] TEST: [ 3 6 10]
  15. TRAIN: [ 0 2 3 4 5 6 9 10 11] TEST: [1 7 8]

 

 

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

闽ICP备14008679号