Python将样本划分为训练集/验证集/测试集

 1 """ Python将样本划分为训练集/验证集/测试集 """
 2 import os, random, shutil
 3 
 4 path = input("D:/图片原先存储路径:")
 5 new_path = input("D:/Train_Sample存放路径:")
 6 
 7 for root, dirs, files in os.walk(path):
 8     fileNumber = len(files)
 9     rate = 0.2
10     pickNumber = int(rate * fileNumber)
11     sample = random.sample(files, pickNumber)
12     for name in sample:
13 
14         file_path = root + '/' + name
15         new_file_path = new_path + '/' + name
16         shutil.move(file_path, new_file_path)
Python按比率划分训练/验证/测试样本

猜你喜欢

转载自www.cnblogs.com/Junlong/p/11367069.html