KFold.split()等类似解释
1 | class sklearn.model_selection.KFold(n_splits = 5 , * , shuffle = False , random_state = None |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | >>> import numpy as np >>> from sklearn.model_selection import KFold >>> X = np.array([[ 1 , 2 ], [ 3 , 4 ], [ 1 , 2 ], [ 3 , 4 ]]) >>> y = np.array([ 1 , 2 , 3 , 4 ]) >>> kf = KFold(n_splits = 2 ) >>> kf.get_n_splits(X) 2 >>> print (kf) KFold(n_splits = 2 , random_state = None , shuffle = False ) >>> for train_index, test_index in kf.split(X): ... print ( "TRAIN:" , train_index, "TEST:" , test_index) ... X_train, X_test = X[train_index], X[test_index] ... y_train, y_test = y[train_index], y[test_index] TRAIN: [ 2 3 ] TEST: [ 0 1 ] TRAIN: [ 0 1 ] TEST: [ 2 3 ] |
Methods
|
Returns the number of splitting iterations in the cross-validator |
|
Generate indices to split data into training and test set. |
1 2 | for flod_idx, (train_idx, val_idx) in enumerate (skf.split(train_jpg, train_jpg)): ... |
话不多说,用例子说话:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | from sklearn.model_selection import KFold kf = KFold(n_splits = 5 , random_state = 43 , shuffle = True ) a = [[ 1 , 2 ],[ 3 , 4 ],[ 5 , 6 ],[ 7 , 8 ],[ 9 , 10 ]] b = [ 1 , 2 , 3 , 4 , 5 ] for i,j in kf.split(a,b): print (i,j) #输出: [ 0 1 2 4 ] [ 3 ] [ 0 1 3 4 ] [ 2 ] [ 0 2 3 4 ] [ 1 ] [ 1 2 3 4 ] [ 0 ] [ 0 1 2 3 ] [ 4 ] |
不用多说了吧,上面的数都是索引,其实就是从0-4索引里,选一个作为输出,其他都是输入。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)