K折交叉验证,英文名叫做K-fold cross-validation,用来测试算法准确性。是常用的测试方法。将数据集分成K份,轮流将其中K-1份作为训练数据,1份作为测试数据,进行试验。
# -*- coding:utf-8 -*-
#author :xinle time:19-7-4
import os
def K_flod(path,k_fold):
images=[os.path.join(path,image) for image in os.listdir(path)]
images_len=len(images)
for i in range (k_fold):
train_data=images[0:(images_len/k_fold)*(k_fold-1)]
test_data=images[(images_len/k_fold)*(k_fold-1):]
print 'trian data:',train_data,'test_data',test_data
images.insert(0, images.pop())#列表右移位
return