policies / data_preparation.py
vosadcii's picture
policy number only
075d413
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")