The random.shuffle() function in python randomly sorts all elements of the sequence

1. Function details:

2. Example:

import random

a = [1,2,3,4,5,6]
random.shuffle(a)
print(a)   ## [1, 5, 6, 3, 2, 4]

3. Application:

It can be used to divide the data set, such as dividing into training set and validation set.

import random
import os

## 超参
in_dir = 'folder1/number.txt'
out_dir = 'folder1'
val_size = 3

## 读数据
with open(in_dir,'r',encoding='utf-8') as f:
    lines = f.readlines()

## 随机排序
random.shuffle(lines)

## 划分训练集、验证集
with open(os.path.join(out_dir, "train.txt"), "w", encoding="utf-8") as f:
    for m in lines[val_size :]:
        f.write(m.strip() + "\n")
with open(os.path.join(out_dir, "val.txt"), "w", encoding="utf-8") as f:
    for m in lines[: val_size]:
        f.write(m.strip() + "\n")

 

 

 

 

Guess you like

Origin blog.csdn.net/m0_46483236/article/details/123820042