留出法
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,random_state=2,test_size=0.25)
from sklearn.model_selection import LeaveOneOut
data = [1,2,3,4]
loo = LeaveOneOut()
for train,test in loo.split(data):
print("%s %s" % (train,test))
交叉验证法
import numpy as np
from sklearn.model_selection import KFold,StratifiedKFold
x = np.array([
[1,2,3,4],
[11,12,13,14],
[21,22,23,24],
[31,32,33,34],
[41,42,43,44],
[51,52,53,54],
[61,62,63,64],
[71,72,73,74]
])
y = np.array([1,1,0,0,1,1,0,0])
print("KFold")
folder = KFold(n_splits=4,random_state=0,shuffle=False)
for train,test in folder.split(x,y):
print("train:%s,test:%s" % (train,test))
print("StratifiedKFold")
sfolder = StratifiedKFold(n_splits=4,random_state=0,shuffle=False)
for train,test in sfolder.split(x,y):
print("train:%s,test:%s" % (train,test))
自助法
总结