-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtworaven_solver_test.py
More file actions
246 lines (234 loc) · 7.87 KB
/
tworaven_solver_test.py
File metadata and controls
246 lines (234 loc) · 7.87 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.pipeline import Pipeline
import tworaven_solver
import pandas
import pandas as pd
import numpy as np
from datetime import datetime
problems = {
'shampoo': {
'pipeline_specification': {
'preprocess': None,
'model': {
# 'strategy': 'SARIMAX'
'strategy': 'AR_NN'
}
},
'train_specification': {
"problem": {
"taskType": "FORECASTING",
"predictors": [],
"targets": ['Sales'],
"forecastingHorizon": {
"column": "Month",
"value": 10
},
"time_format": {
"Month": "%m"
}
},
"input": {
"name": "in-sample",
"resource_uri": "file://" + '/ravens_volume/test_data/TR_TS_shampoo/TRAIN/dataset_TRAIN/tables/learningData.csv'
},
"performanceMetric": "rootMeanSquaredError", # meanSquareError | meanAbsoluteError
}
},
'sunspots': {
'pipeline_specification': {
'preprocess': None,
'model': {
'strategy': 'AR_NN'
}
},
'train_specification': {
"problem": {
"taskType": "FORECASTING",
"predictors": [],
"targets": ['sunspots'],
"forecastingHorizon": {
"column": "year-month",
"value": 10
},
"time_format": {
"Month": "%Y-%m"
}
},
"input": {
"name": "in-sample",
"resource_uri": "file://" + '/ravens_volume/test_data/56_sunspots_monthly/TRAIN/dataset_TRAIN/tables/learningData.csv'
},
"performanceMetric": "meanSquaredError"
}
},
'appliance': {
'pipeline_specification': {
'preprocess': None,
'model': {
'strategy': 'VAR'
}
},
'train_specification': {
"problem": {
"taskType": "FORECASTING",
"predictors": ["T1", "T2"],
"targets": ['Appliances'],
"forecastingHorizon": {
"column": "date",
"value": 10
}
},
"input": {
"name": "in-sample",
"resource_uri": "file://" + '/ravens_volume/test_data/TR_TS_appliance/TRAIN/dataset_TRAIN/tables/learningData.csv'
},
"performanceMetric": {"metric": "meanSquaredError"}
}
},
'baseball': {
'pipeline_specification': {
'preprocess': "standard",
'model': {
'strategy': 'RANDOM_FOREST'
}
},
'train_specification': {
"problem": {
"taskType": "CLASSIFICATION",
"predictors": ["Games_played", "Number_seasons", 'Player'],
"targets": ['Hall_of_Fame'],
"categorical": ['Position', 'Player']
},
"input": {
"name": "in-sample",
"resource_uri": "file://" + "/ravens_volume/test_data/185_baseball/TRAIN/dataset_TRAIN/tables/learningData.csv"
}
}
},
'baseball-regression': {
'pipeline_specification': {
'preprocess': "standard",
'model': {
'strategy': 'ORDINARY_LEAST_SQUARES'
}
},
'train_specification': {
"problem": {
"taskType": "REGRESSION",
"predictors": ["Runs", "Hits", "At_bats"],
"targets": ['Triples'],
"categorical": ['Position', 'Player']
},
"input": {
"name": "in-sample",
"resource_uri": "file://" + "/ravens_volume/test_data/185_baseball/TRAIN/dataset_TRAIN/tables/learningData.csv"
}
}
},
'phem': {
'pipeline_specification': {
'preprocess': "standard",
'model': {
'strategy': 'ORDINARY_LEAST_SQUARES'
}
},
'train_specification': {
"problem": {
"taskType": "REGRESSION",
"predictors": ["Runs", "Hits", "At_bats"],
"targets": ['Triples'],
"categorical": ['Position', 'Player']
},
"input": {
"name": "in-sample",
"resource_uri": "file://" + "/ravens_volume/test_data/185_baseball/TRAIN/dataset_TRAIN/tables/learningData.csv"
}
}
}
}
# Only support for time series forecasting
from tworaven_solver.search import SearchManager
problem = problems['appliance']
pip_spe, train_spe = problem['pipeline_specification'], problem['train_specification']
search_manager = SearchManager(None, train_spe['problem'])
new_pip = search_manager.get_pipeline_specification()
# print(new_pip)
# while new_pip["model"]['strategy'] != "SARIMAX":
new_pip = search_manager.get_pipeline_specification()
model = tworaven_solver.fit_pipeline(new_pip, train_spe)
dataframe = pd.read_csv(problem['train_specification']['input']['resource_uri'].replace('file://', ''))
# res = model.forecast
dataframe = dataframe[['Appliances', 'T1', 'T2', 'date', 'd3mIndex']]
res = model.forecast(dataframe)
print(res)
# end = model.model.model._index[-1]
# start = model.model.model._index[0]
#
# model.model.plot_forecast()
#
# import matplotlib.pyplot as plt
#
# plt.show()
#
# dataframe = pandas.read_csv(data_path)
# dataframe['Month'] = pandas.to_datetime(dataframe['Month'])
# # dataframe = dataframe.set_index('Month')
#
#
# def infer_freq(series):
#
# def approx_seconds(offset):
# offset = pd.tseries.frequencies.to_offset(offset)
# try:
# return offset.nanos / 1E9
# except ValueError:
# pass
#
# date = datetime.now()
# return ((offset.rollback(date) - offset.rollforward(date)) * offset.n).total_seconds()
#
# # infer frequency from every three-pair of records
# candidate_frequencies = set()
# for i in range(len(series) - 3):
# candidate_frequency = pd.infer_freq(series[i:i + 3])
# if candidate_frequency:
# candidate_frequencies.add(candidate_frequency)
#
# # sort inferred frequency by approximate time durations
# return sorted([(i, approx_seconds(i)) for i in candidate_frequencies], key=lambda x: x[1])[-1][0]
#
#
# freq = infer_freq(dataframe['Month'])
#
# dataframe = dataframe.set_index('Month')
# dataframe_temp = dataframe.resample(freq).mean()
# numeric_columns = list(dataframe.select_dtypes(include=[np.number]).columns.values)
# categorical_columns = [i for i in dataframe.columns.values if i not in numeric_columns]
#
# for dropped_column in categorical_columns:
# dataframe_temp[dropped_column] = dataframe[dropped_column]
#
# print(numeric_columns)
# print(categorical_columns)
# dataframe = pd.DataFrame(ColumnTransformer(transformers=[
# ('numeric', SimpleImputer(strategy='median'), numeric_columns),
# ('categorical', SimpleImputer(strategy='most_frequent'), categorical_columns)
# ]).fit_transform(dataframe_temp), index=dataframe_temp.index, columns=dataframe_temp.columns)
#
# print(dataframe)
#
#
# # split_index = int(len(dataframe) * .75)
# # history = dataframe.head(split_index)
# # future = dataframe.head(-split_index)
# #
# # print(model_tworavens.forecast(history, 3))
# # print(future.head(3))
# # train_specification['problem']['targets'].append('Sales')
# #
# # model_tworavens = tworaven_solver.fit_pipeline(
# # pipeline_specification=pipeline_specification,
# # train_specification=train_specification)
# #
# # print(model_tworavens.forecast(3))