File size: 1,022 Bytes
d9e8645 |
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 |
import json
from sklearn.model_selection import train_test_split
def split_data(file_name: str, data_type: str):
if data_type == "json":
with open(file_name, 'r') as json_file:
data = json.load(json_file)["data"]
json_file.close()
train, test = train_test_split(data, train_size=0.9, random_state=42)
return(train, test)
def save_json(data: dict, file_name: str):
"""
Method to save the json file.
Parameters:
----------
data: dict,
data to be saved in file.
file_name: str,
name of the file.
Returns:
--------
None
"""
# save the split
with open(file_name, "w") as data_file:
json.dump(data, data_file, indent=2)
data_file.close()
if __name__ == "__main__":
# split the train data
train, test = split_data("train.json", "json")
# save the train split
save_json({"data": train}, "data/train.json")
# save the test split
save_json({"data": test}, "data/test.json")
|