-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAR_script.py
68 lines (58 loc) · 2.62 KB
/
AR_script.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Import the library
from pmdarima import auto_arima
# Importing required libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sys
import json
import datetime
# Ignore harmless warnings
import warnings
warnings.filterwarnings("ignore")
import os
directory = os.path.join(os.curdir, "sine_data")
seq_len = 200
dims = [1, 2, 4, 6, 9]
for dim in dims:
file = f'sine_123_1000_{dim}.csv'
data = np.loadtxt(os.path.join(directory, file), delimiter = ",",skiprows = 1)
# dim = file.split('_')[3][0]
print(f'dim is {dim}')
data = pd.DataFrame(data)
if dim == '1':
data = data[1]
best_model = auto_arima(data, start_p = 1, start_q = 1,
max_p = 3, max_q = 3,
start_P = 0, seasonal = True,
d = None, D = 1, trace = True,
error_action ='ignore', # we don't want to know if an order does not work
suppress_warnings = True, # we don't want convergence warnings
stepwise = True) # set to stepwise
forecast = pd.DataFrame(best_model.predict(n_periods = seq_len))
gen_list = forecast.values.tolist()
x = datetime.datetime.now()
timestamp = x.strftime("%d%m%y_%Hh%M")
filepath = f'synthetic_sines/arima_sine_{dim}_{seq_len}_{timestamp}.json'
with open(filepath, 'w') as file:
json.dump(gen_list, file)
else:
# Fit auto_arima function to dataset
generated_sines = []
for feature in data.columns:
feat = data[feature]
best_model = auto_arima(feat, start_p = 1, start_q = 1,
max_p = 3, max_q = 3,
start_P = 0, seasonal = True,
d = None, D = 1, trace = True,
error_action ='ignore', # we don't want to know if an order does not work
suppress_warnings = True, # we don't want convergence warnings
stepwise = True) # set to stepwise
forecast = pd.DataFrame(best_model.predict(n_periods = seq_len))
generated_sines.append(forecast.values.tolist())
# Save generated data to csv
x = datetime.datetime.now()
timestamp = x.strftime("%d%m%y_%Hh%M")
filepath = f'synthetic_sines/arima_sine_{dim}_{seq_len}_{timestamp}.json'
with open(filepath, 'w') as file:
json.dump(generated_sines, file)