程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python datasets are scaled

編輯:Python

There is already a file containing all the image names file.txt, Now it is divided in proportion .

according to train :test :val = 8:1:1 The proportion of , Yes file.txt The image names in the file are randomly divided .

First get file.txt The image name of the file is saved as list, Right again list Make proportional random division ( Start by randomly disrupting the order , And then divide them in proportion ), Get saved with 3 Children of different sets list. Finally, return and save to the corresponding txt In file .

# train/val/test = 8/1/1
# encoding: utf-8
import os
import random
def ran_split(full_list,shuffle=False,ratio1=0.8,ratio2=0.1):
sublists=[]
n_total = len(full_list)
offset1 = int(n_total * ratio1)
offset2 = int(n_total * ratio2) + offset1
if n_total == 0 or offset1 < 1:
return [], full_list
if shuffle:
random.shuffle(full_list) # Disorganize the order
sublist_1 = full_list[:offset1]
sublist_2 = full_list[offset1:offset2]
sublist_3 = full_list[offset2:]
sublists.append(sublist_1)
sublists.append(sublist_2)
sublists.append(sublist_3)
return sublists # sublists=[sublist_1,sublist_2,sublist_3]
def read_file(filepath):
file_list=[]
with open(filepath,'r') as fr:
data = fr.readlines()
data = ''.join(data).strip('\n').splitlines()
# ''.join() list To str
# s.strip(rm) Delete s At the beginning and end of rm character
# .splitlines() Returns a string to a list
file_list=data
return file_list
def write_file(dst1,txt):
fo=open(dst1,'w')
for item in txt:
fo.write(str(item)+'\n')
if __name__ == "__main__":
root_path=r'F:\all_date\WHU'
from_txt='file.txt'
txts=['train.txt','test.txt','val.txt']
from_path=os.path.join(root_path,from_txt)
txt_list=read_file(from_path)
sublists = ran_split(txt_list,shuffle=True,ratio1=0.8,ratio2=0.1)
# notes : Generated sublist Quantity and txts The same number
for txt_name,i in zip(txts,range(len(txts))):
to_path=os.path.join(root_path,txt_name)
write_file(to_path,sublists[i])

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved