-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24by24.py
181 lines (98 loc) · 3.64 KB
/
24by24.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
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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import pandas as pd
import numpy as np
import os
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
plt.rcParams["figure.figsize"] = (8, 7)
from keras.models import Model
from keras import layers
# In[ ]:
filelist = os.listdir("./@folderName/")
filelist = filelist[1:]
target = {}
for x, a_file in enumerate(filelist):
target[x] = pd.read_csv("./@folderName/" + a_file, index_col=0)
# In[ ]:
[x[29:35] for x in filelist][87]
# ### pp ML TSNE data
# In[ ]:
training_data = np.array([])
training_data = training_data.reshape(0, 24, 24)
for week_idx in range(32, 61):
answer = target[week_idx]
for i in range(week_idx - 23, week_idx)[::-1]:
answer = pd.merge(target[i], answer, how='right', left_index=True, right_index=True)
answer.fillna(0, inplace=True)
answer = np.fmin(1, answer.div(answer.quantile(0.9, 1) + 1, axis=0))
tri_di_ans = answer.to_numpy().reshape(answer.shape[0], 24, 24)
training_data = np.append(training_data, tri_di_ans, axis = 0)
# In[ ]:
test_data = np.array([])
test_data= test_data.reshape(0, 24, 24)
for week_idx in range(87, 90):
answer = target[week_idx]
for i in range(week_idx - 23, week_idx)[::-1]:
answer = pd.merge(target[i], answer, how='right', left_index=True, right_index=True)
answer.fillna(0, inplace=True)
answer = np.fmin(1, answer.div(answer.quantile(0.9, 1) + 1, axis=0))
tri_di_ans = answer.to_numpy().reshape(answer.shape[0], 24, 24)
test_data = np.append(test_data, tri_di_ans, axis = 0)
# In[ ]:
test_data.shape
# ### 24x24 gray scale image
# In[ ]:
# from matplotlib import pyplot as plt
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (10, 7)
# In[ ]:
plt.imshow(training_data[12], cmap='gray_r')
plt.show()
# ### ML, 576D to 2D to 576D
# In[ ]:
input_target = layers.Input(shape=(24, 24))
flat_input = layers.Flatten()(input_target)
encoding_layer = layers.Dense(2)(flat_input) # None (linear), Relu, sigmoid
embedding_layer = layers.Dense(576)(encoding_layer)
decoding_layer = layers.Reshape((24, 24))(embedding_layer)
model = Model(input=input_target, output=decoding_layer)
encoder = Model(input=input_target, output=encoding_layer)
model.compile(optimizer='rmsprop', loss='mean_squared_error')
# In[ ]:
model.summary()
# In[ ]:
# training case loss
model.fit(training_data, training_data, epochs=10)
# In[ ]:
model.evaluate(test_data, test_data)
# In[ ]:
target_recovered = model.predict(target)
# In[ ]:
fig = plt.figure()
X, Y = np.meshgrid(np.arange(24), np.arange(23, -1, -1))
Z = target[1500]
ax = fig.gca(projection='3d')
ax.view_init(elev=40, azim=280)
ax.plot_surface(X, Y, Z, cmap='coolwarm', linewidth=0, antialiased=False);
# In[ ]:
fig = plt.figure()
X, Y = np.meshgrid(np.arange(24), np.arange(23, -1, -1))
Z = target_recovered[1500]
ax = fig.gca(projection='3d')
ax.view_init(elev=40, azim=280)
ax.plot_surface(X, Y, Z, cmap='coolwarm', linewidth=0, antialiased=False);
# ### TSNE
# In[ ]:
t_model = TSNE()
encoded = encoder.predict(training_data)
tf_for_TSNE = t_model.fit_transform(encoded)
repeated = np.append(np.repeat(0, tri_di_ans_1.shape[0]), np.repeat(1, tri_di_ans_2.shape[0]))
repeated = repeated.reshape(repeated.size, 1)
tf_shuffle = np.append(tf_for_TSNE, repeated, axis=1)
# In[ ]:
np.random.shuffle(tf_shuffle)
plt.scatter(tf_shuffle[:, 0], tf_shuffle[:, 1], c=tf_shuffle[:, 2])
plt.show()