Repository navigation
Expand file tree
/
Copy pathsplit.py
More file actions
executable file
·48 lines (36 loc) · 1.67 KB
/
Copy pathsplit.py
File metadata and controls
executable file
·48 lines (36 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# Shuffles a CSV file's lines and splits them into train.csv/validation.csv/
# test.csv in the given ratio. Doesn't look at what's in each line (column
# count, contents, anything) -- any line-oriented CSV works, not just the
# character-recognition example's samples.csv.
import argparse
import random
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("input", help="Path to the CSV file to shuffle and split")
parser.add_argument("--train", type=float, default=0.8,
help="Fraction of lines written to train.csv (default: 0.8)")
parser.add_argument("--validation", type=float, default=0.1,
help="Fraction of lines written to validation.csv (default: 0.1); "
"the remainder goes to test.csv")
args = parser.parse_args()
if args.train < 0 or args.validation < 0 or args.train + args.validation > 1.0:
parser.error("--train and --validation must each be >= 0 and sum to <= 1.0")
with open(args.input, "r") as file:
lines = file.readlines()
random.shuffle(lines)
num_lines = len(lines)
train_end = int(num_lines * args.train)
valid_end = train_end + int(num_lines * args.validation)
train_lines = lines[:train_end]
valid_lines = lines[train_end:valid_end]
test_lines = lines[valid_end:]
with open("train.csv", "w") as file:
file.writelines(train_lines)
with open("validation.csv", "w") as file:
file.writelines(valid_lines)
with open("test.csv", "w") as file:
file.writelines(test_lines)
print("Files created successfully:")
print(f"Train set: {len(train_lines)} lines")
print(f"Validation set: {len(valid_lines)} lines")
print(f"Test set: {len(test_lines)} lines")