-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloading_module.py
More file actions
169 lines (152 loc) · 5.51 KB
/
loading_module.py
File metadata and controls
169 lines (152 loc) · 5.51 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
#!/usr/bin/python3
#
#
import numpy as np
import pandas as pd
class data_module:
def __init__(me, file_name, time_series_column=0):
me.panda = pd.read_csv(file_name)
# this needs to be converted to an explict conversion.
# to_numpy does not fail gracefully.
# me.data = me.panda.to_numpy(dtype="float32")
temp = me.panda.to_numpy()
me.data = np.zeros( temp.data.shape)
for i in range(0, temp.shape[0]):
for j in range(0,temp.shape[1]):
try:
me.data[i,j] = float(temp[i,j])
except:
me.data[i,j] = float(i)
me.new_data = np.zeros( me.data.shape)
me.time_series_column = time_series_column
me.nrows = me.data.shape[0]
me.ncolumns = me.data.shape[1]
me.which = 0
# scale parameters
me.maxes = np.zeros(me.ncolumns)
me.mins = np.zeros(me.ncolumns)
me.means = np.zeros(me.ncolumns)
for i in range(0,me.ncolumns):
me.maxes[i] = max( me.data[0:,i])
me.mins[i] = min( me.data[0:,i])
me.means[i] = me.data[0:,i].mean()
# normalize by column.
for i in range(0,me.ncolumns):
if i == me.time_series_column:
continue
s = 1./(me.maxes[i] - me.mins[i])
m = me.means[i]
for j in range(0, me.nrows):
me.data[j,i] = (me.data[j,i]-m)*s
def reset_data_index(me,where = 0):
me.which = where
def a_data_point(me, window):
if me.which + window +1 >= me.nrows:
me.reset_data_index()
dependent = []
independent = []
for i in range(0, me.ncolumns):
if i == me.time_series_column:
continue
for j in range(me.which, me.which+window):
dependent.append( me.data[j,i])
independent.append(me.data[me.which+window,i])
me.which += 1
return dependent,independent
def a_specific_data_point(me,which, window):
if which + window +1 >= me.nrows:
# return ([],[]) # causes errors
which = me.nrows-1-window
dependent = []
independent = []
for i in range(0, me.ncolumns):
if i == me.time_series_column:
continue
for j in range(which, which+window):
dependent.append( me.data[j,i])
independent.append(me.data[which+window,i])
return (dependent,independent)
def set_up_prediction(me, window):
me.which = 0
# for j in range( 0, window):
# for i in range(0, me.ncolumns):
# me.new_data[j,i] = me.data[j,i]
# for j in range(0, me.nrows):
# me.new_data[j,me.time_series_column] = me.data[j, time_series_column]
np.copyto( me.new_data, me.data)
def set_up_average_prediction(me):
me.average_data = np.zeros( me.data.shape)
me.in_average = 0
def a_prediction_data_point(me, window):
if me.which + window >= me.nrows:
me.reset_data_index()
dependent = []
independent = []
for i in range(0, me.ncolumns):
if i == me.time_series_column:
continue
for j in range(me.which, me.which+window):
dependent.append( me.new_data[j,i])
independent.append(0.)
me.which += 1
return dependent,independent
def add_average_prediction(me):
me.average_data = me.average_data + me.new_data
me.in_average += 1
def add_prediction(me, what, where):
i = 0
for j in range(0,me.ncolumns):
# this skips the time column
if j == me.time_series_column:
continue
me.new_data[where,j] = what[i]
i = i + 1
def get_prediction(me):
# first denormalize the new data
# denormalize by column.
for i in range(0,me.ncolumns):
if i == me.time_series_column:
continue
s = (me.maxes[i] - me.mins[i])
m = me.means[i]
for j in range(0, me.nrows):
me.new_data[j,i] = me.new_data[j,i]*s+m
me.prediction = pd.DataFrame( me.new_data, me.panda.index, me.panda.columns)
return me.prediction
# design decision here. We could pass a file name, but then we'd limit what the user can do with the
# prediction. So we'll return the prediction.
def get_average_prediction(me):
# first denormalize the new data
# denormalize by column.
for i in range(0,me.ncolumns):
if i == me.time_series_column:
continue
s = (me.maxes[i] - me.mins[i])
m = me.means[i]
for j in range(0, me.nrows):
me.average_data[j,i] = (me.average_data[j,i]/me.in_average)*s+m
me.prediction = pd.DataFrame( me.average_data, me.panda.index, me.panda.columns)
return me.prediction
# design decision here. We could pass a file name, but then we'd limit what the user can do with the
# prediction. So we'll return the prediction.
def main():
q = data_module("macrodata.csv")
b = q.a_data_point(8)
print(b)
b = q.a_data_point(8)
print(b)
b = q.a_data_point(8)
print(b)
b = q.a_data_point(8)
print(b)
b = q.a_data_point(8)
print(b)
s = pd.DataFrame( q.data, q.panda.index, q.panda.columns)
print(s)
q.set_up_prediction(8)
x = q.a_prediction_data_point(8)
print( x )
print( len(x[0])/8)
print( len(x[1]) )
if __name__ == "__main__":
main()