sklearn 学习之 model_selection
生活随笔
收集整理的這篇文章主要介紹了
sklearn 学习之 model_selection
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
(1.1) sklearn.model_selection.KFold(n_splits=3, shuffle=False, random_state=None)
這個數(shù)據(jù)分割器與之前學(xué)到的 GroupKFold()和GroupShuffleSplit() 有很大的不同,這個分割器既不要求我們對數(shù)據(jù)進(jìn)行預(yù)先的分組(及運(yùn)行split()函數(shù)是要求提供 groups 參數(shù)),也不讓我們自行定義每次分割時test數(shù)據(jù)和train數(shù)據(jù)分別占總數(shù)據(jù)的比例(初始化GroupShuffleSplit()實(shí)例時傳入的test_size和train_size參數(shù))。只需我們簡單的傳入一個n_splits參數(shù)即可(如果要求分割時為隨機(jī)采樣數(shù)據(jù)進(jìn)行分割,則需傳入shuffle=true,默認(rèn)為根據(jù)采樣點(diǎn)出現(xiàn)的先后順序連續(xù)的將數(shù)據(jù)分成 k 份,n_splits參數(shù)與 k-fold 中的 k 相對應(yīng))。調(diào)用KFold()實(shí)例的方法split()時,將數(shù)據(jù)平均分割為 k 份,每次取其中的一份作為 validation 數(shù)據(jù),其余的 k-1 份作為訓(xùn)練數(shù)據(jù),總共進(jìn)行 k 次,每次都返回的為 train/validation 數(shù)據(jù)的下標(biāo)。
example: from sklearn.model_selection import KFold import numpy as np x = np.arange(48).reshape(12,4) # 生成數(shù)據(jù)集 y = np.array([0,0,0,1,1,0,1,1,0,1,1,0]) # 生成對應(yīng)的label spliter = KFold(n_splits=4, shuffle=False) # 用KFold初始化一個數(shù)據(jù)分割器,將數(shù)據(jù)分割為 4 個 fold for train_index, test_index in spliter.split(x, y): print("Train data index: ", train_index, "Test data index: ", test_index) print("Train data: ", x[train_index], "Test data: ", x[test_index]) print("Train label: ", y[train_index], "Test label: ", y[test_index]) print("\n") 輸出如下: Train data index:? [ 3? 4? 5? 6? 7? 8? 9 10 11] Test data index:? [0 1 2]
Train data:? [[12 13 14 15]
?[16 17 18 19]
?[20 21 22 23]
?[24 25 26 27]
?[28 29 30 31]
?[32 33 34 35]
?[36 37 38 39]
?[40 41 42 43]
?[44 45 46 47]] Test data:? [[ 0? 1? 2? 3]
?[ 4? 5? 6? 7]
?[ 8? 9 10 11]]
Train label:? [1 1 0 1 1 0 1 1 0] Test label:? [0 0 0]
Train data index:? [ 0? 1? 2? 6? 7? 8? 9 10 11] Test data index:? [3 4 5]
Train data:? [[ 0? 1? 2? 3]
?[ 4? 5? 6? 7]
?[ 8? 9 10 11]
?[24 25 26 27]
?[28 29 30 31]
?[32 33 34 35]
?[36 37 38 39]
?[40 41 42 43]
?[44 45 46 47]] Test data:? [[12 13 14 15]
?[16 17 18 19]
?[20 21 22 23]]
Train label:? [0 0 0 1 1 0 1 1 0] Test label:? [1 1 0]
Train data index:? [ 0? 1? 2? 3? 4? 5? 9 10 11] Test data index:? [6 7 8]
Train data:? [[ 0? 1? 2? 3]
?[ 4? 5? 6? 7]
?[ 8? 9 10 11]
?[12 13 14 15]
?[16 17 18 19]
?[20 21 22 23]
?[36 37 38 39]
?[40 41 42 43]
?[44 45 46 47]] Test data:? [[24 25 26 27]
?[28 29 30 31]
?[32 33 34 35]]
Train label:? [0 0 0 1 1 0 1 1 0] Test label:? [1 1 0]
Train data index:? [0 1 2 3 4 5 6 7 8] Test data index:? [ 9 10 11]
Train data:? [[ 0? 1? 2? 3]
?[ 4? 5? 6? 7]
?[ 8? 9 10 11]
?[12 13 14 15]
?[16 17 18 19]
?[20 21 22 23]
?[24 25 26 27]
?[28 29 30 31]
?[32 33 34 35]] Test data:? [[36 37 38 39]
?[40 41 42 43]
?[44 45 46 47]]
Train label:? [0 0 0 1 1 0 1 1 0] Test label:? [1 1 0]
總結(jié)
以上是生活随笔為你收集整理的sklearn 学习之 model_selection的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab行距,Matlab cdfp
- 下一篇: 低蓝光认证:TUV莱茵与TUV南德 有啥