diff options
| author | vin <git@vineetk.net> | 2025-08-23 13:35:32 -0400 |
|---|---|---|
| committer | vin <git@vineetk.net> | 2025-08-23 13:35:32 -0400 |
| commit | 4ec10c33159f435963b73da5a9bd12f25f65cda9 (patch) | |
| tree | 266c7edd418666e3145539bd2f1338fc94d551de | |
| parent | d5e69d1debc6df412f0862cb47ea90deebcd5dbf (diff) | |
add helper script to split dataset.jsonl
| -rw-r--r-- | split_dataset.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/split_dataset.py b/split_dataset.py new file mode 100644 index 0000000..9ac7bbf --- /dev/null +++ b/split_dataset.py | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | import sys | ||
| 2 | import json | ||
| 3 | import random | ||
| 4 | import argparse | ||
| 5 | |||
| 6 | def split_dataset( | ||
| 7 | input_filepath: str, | ||
| 8 | train_filepath: str = "train.jsonl", | ||
| 9 | validation_filepath: str = "validation.jsonl", | ||
| 10 | split_ratio: float = 0.95 | ||
| 11 | ): | ||
| 12 | """ | ||
| 13 | Reads a .jsonl file, shuffles it, and splits it into training and validation files. | ||
| 14 | """ | ||
| 15 | print(f"Loading data from {input_filepath}...") | ||
| 16 | try: | ||
| 17 | with open(input_filepath, 'r', encoding='utf-8') as f: | ||
| 18 | lines = f.readlines() | ||
| 19 | except FileNotFoundError: | ||
| 20 | print(f"Error: Input file not found at {input_filepath}", file=sys.stderr) | ||
| 21 | sys.exit(1) | ||
| 22 | |||
| 23 | # Shuffle the dataset to ensure random distribution | ||
| 24 | print("Shuffling data...") | ||
| 25 | random.shuffle(lines) | ||
| 26 | |||
| 27 | # Determine the split point | ||
| 28 | split_index = int(len(lines) * split_ratio) | ||
| 29 | |||
| 30 | # Split the data | ||
| 31 | train_lines = lines[:split_index] | ||
| 32 | validation_lines = lines[split_index:] | ||
| 33 | |||
| 34 | # Write the training file | ||
| 35 | print(f"Writing {len(train_lines)} lines to {train_filepath}...") | ||
| 36 | with open(train_filepath, 'w', encoding='utf-8') as f: | ||
| 37 | f.writelines(train_lines) | ||
| 38 | |||
| 39 | # Write the validation file | ||
| 40 | print(f"Writing {len(validation_lines)} lines to {validation_filepath}...") | ||
| 41 | with open(validation_filepath, 'w', encoding='utf-8') as f: | ||
| 42 | f.writelines(validation_lines) | ||
| 43 | |||
| 44 | print("\nSplitting complete.") | ||
| 45 | |||
| 46 | if __name__ == "__main__": | ||
| 47 | parser = argparse.ArgumentParser(description="Split a .jsonl dataset into training and validation sets.") | ||
| 48 | parser.add_argument("input_file", type=str, help="Path to the input .jsonl file.") | ||
| 49 | parser.add_argument( | ||
| 50 | "--split", | ||
| 51 | type=float, | ||
| 52 | default=0.95, | ||
| 53 | help="Ratio for the training set split (e.g., 0.95 for a 95/5 split). Default is 0.95." | ||
| 54 | ) | ||
| 55 | |||
| 56 | args = parser.parse_args() | ||
| 57 | |||
| 58 | if not 0 < args.split < 1: | ||
| 59 | print("Error: Split ratio must be between 0 and 1.", file=sys.stderr) | ||
| 60 | sys.exit(1) | ||
| 61 | |||
| 62 | split_dataset(args.input_file, split_ratio=args.split) \ No newline at end of file | ||
