|
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 |
|
""" |
|
|
|
|
|
with open(file_name, "w") as data_file: |
|
json.dump(data, data_file, indent=2) |
|
data_file.close() |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
train, test = split_data("train.json", "json") |
|
|
|
|
|
save_json({"data": train}, "data/train.json") |
|
|
|
|
|
save_json({"data": test}, "data/test.json") |
|
|