-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.py
More file actions
244 lines (198 loc) · 7.01 KB
/
github.py
File metadata and controls
244 lines (198 loc) · 7.01 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
#!/usr/bin/env python3
import pandas as pd
import numpy as np
import scipy
import scipy.stats
import csv
epsilon = 0.1
maxiter = 1000
S_lim = 3
inputdata = {}
def rows2pd(key, rows):
if rows[0][0] != '':
raise Exception('data error');
colnames = rows[0][1:]
rownames = list(map(lambda x: x[0], rows[1:]))
inputdata[key] = pd.DataFrame(map(lambda x: x[1:], rows[1:]), dtype=float);
inputdata[key].index = rownames;
inputdata[key].columns = colnames;
return
with open('in.csv', 'r', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
inkey = ''
rows = []
for row in reader:
if len(row):
strrowlen = len(''.join(row))
if strrowlen:
if len(row[0]) == strrowlen:
if inkey and rows:
rows2pd(inkey, rows)
inkey = row[0]
rows = []
print('reading', row[0])
else:
rows.append(row)
if inkey and rows:
rows2pd(inkey, rows)
print('input:')
for key, data in inputdata.items():
print(key)
print(data)
print()
print()
print('working...')
print()
df_group_by_sphere_pzk = {}
for tenskey, tens in inputdata.items():
df_group_by_sphere_pzk[tenskey] = {}
for row in tens.index:
for col in tens.columns:
df_group_by_sphere_pzk[tenskey][row + '_' + col] = tens.loc[row,col]
df_group_by_sphere_pzk = pd.DataFrame(df_group_by_sphere_pzk).T
df_sphere_by_group_pzk = {}
df_pzk_by_group_sphere = {}
for col in df_group_by_sphere_pzk.columns:
scol = col.split('_')[0]
pcol = col.split('_')[1]
if scol not in df_sphere_by_group_pzk:
df_sphere_by_group_pzk[scol] = {}
if pcol not in df_pzk_by_group_sphere:
df_pzk_by_group_sphere[pcol] = {}
for row in df_group_by_sphere_pzk.index:
df_sphere_by_group_pzk[scol][row + '_' + pcol] = df_group_by_sphere_pzk.loc[row,col]
df_pzk_by_group_sphere[pcol][row + '_' + scol] = df_group_by_sphere_pzk.loc[row,col]
df_sphere_by_group_pzk = pd.DataFrame(df_sphere_by_group_pzk).T
df_pzk_by_group_sphere = pd.DataFrame(df_pzk_by_group_sphere).T
Y1_K = []
n1 = len(df_group_by_sphere_pzk.columns)
n2 = len(df_group_by_sphere_pzk.index)
for k in range(n2):
for row in range(n1):
msrow = [0] * n2 * n1
msrow[row*n2 + k] = 1
Y1_K.append(msrow)
Y1_K = np.matrix(Y1_K)
Y1_K2 = Y1_K @ Y1_K
Y1_X = np.matrix(df_group_by_sphere_pzk)
Y1_X = Y1_X.reshape((Y1_X.size, 1))
Y1_COL = Y1_K2 @ Y1_X
Y1 = Y1_COL.reshape(len(df_group_by_sphere_pzk.index), len(df_group_by_sphere_pzk.columns))
Y2_K = []
n1 = len(df_group_by_sphere_pzk.index)
n3 = len(df_pzk_by_group_sphere.index) #100
n2 = len(df_sphere_by_group_pzk.index) #5
for k in range(n2):
for row in range(n3):
msrow = [0] * n2 * n3
msrow[row*n2 + k] = 1
Y2_K.append(msrow)
Y2_K = np.matrix(Y2_K)
Y2_I = np.identity(n1)
Y2_K2 = np.kron(Y2_I, Y2_K @ Y2_K)
Y2_X = np.matrix(df_sphere_by_group_pzk)
Y2_X = Y2_X.reshape((Y2_X.size, 1))
Y2_COL = Y2_K2 @ Y2_X
Y2 = Y2_COL.reshape(len(df_sphere_by_group_pzk.index), len(df_sphere_by_group_pzk.columns))
Y2.shape
Y3 = np.matrix(df_pzk_by_group_sphere)
Y3.shape
n1 = len(df_group_by_sphere_pzk.index)
n2 = len(df_sphere_by_group_pzk.index)
n3 = len(df_pzk_by_group_sphere.index)
A1 = np.identity(n1)
A2 = np.identity(n2)
A3 = np.identity(n3)
P_A1 = A1
P_A2 = A2
P_A3 = A3
curiter = maxiter
while curiter>0:
curiter -= 1
Q1_A1 = np.kron(A2,A3)
Q1 = Y1 @ Q1_A1
U, S1, VH = np.linalg.svd(Q1)
A1 = U[:, range(S_lim)]
Q2_A2 = np.kron(A1, A3)
Q2 = Y2 @ Q2_A2
U, S2, VH = np.linalg.svd(Q2)
A2 = U[:, range(S_lim)]
Q3_A3 = np.kron(A1, A2)
Q3 = Y3 @ Q3_A3
U, S3, VH = np.linalg.svd(Q3)
A3 = U[:, range(S_lim)]
gobreak = [False, False, False]
diff = []
A_sqr_sum = []
for i, TMP_A, TMP_PA in ( (0, A1, P_A1), (1, A2, P_A2), (2, A3, P_A3) ):
colmax = max(TMP_A.shape[1],TMP_PA.shape[1])
if TMP_A.shape[1] < colmax:
TMP_A = np.concatenate( (TMP_A, np.zeros( ( TMP_A.shape[0], colmax - TMP_A.shape[1] ) ) ), axis = 1 )
if TMP_PA.shape[1] < colmax:
TMP_PA = np.concatenate( (TMP_PA, np.zeros( ( TMP_PA.shape[0], colmax - TMP_PA.shape[1] ) ) ), axis = 1 )
gobreak[i] = np.sum(np.square(TMP_A-TMP_PA))/TMP_A.size < epsilon
diff.append(np.sum(np.square(TMP_A-TMP_PA))/TMP_A.size)
A_sqr_sum.append(np.sum(np.square(TMP_A)))
if all(gobreak):
break
P_A1 = A1
P_A2 = A2
P_A3 = A3
df_A_dict = {}
for Aname, A, new_index in (('A1', A1, df_group_by_sphere_pzk.index), ('A2', A2, df_sphere_by_group_pzk.index), ('A3', A3, df_pzk_by_group_sphere.index) ):
print()
#Манхеттен-норма'
manhetten = np.sum(np.absolute(A), axis=1)
mrank = A.shape[0] - scipy.stats.rankdata(manhetten, axis=0) + 1
#Евклидова-норма
euclid = np.sqrt(np.sum(np.square(A), axis=1))
erank = A.shape[0] - scipy.stats.rankdata(euclid, axis=0) + 1
#Бесконечность-норма
linf = np.max(np.absolute(A), axis=1)
lrank = A.shape[0] - scipy.stats.rankdata(linf, axis=0) + 1
# Норма Гёльдера
gelder = np.power(np.sum(np.power(A,4), axis=1),0.25)
grank = A.shape[0] - scipy.stats.rankdata(gelder, axis=0) + 1
sumrank = mrank + erank + lrank + grank
finrank = scipy.stats.rankdata(sumrank, axis=0)
dfA = pd.DataFrame(np.concatenate( (A, manhetten, mrank, euclid, erank, linf, lrank, gelder, grank, sumrank, finrank), axis=1 ))
dfA.index = new_index
dfA.columns = list(dfA.columns[:A.shape[1]]) + ['Манхеттен', 'Мранг', 'Евклид', 'Еранг', 'LБесконечность', 'Lранг', 'Гёльдер', 'Гранг', 'Сум ранг', 'Итог ранг']
df_A_dict[Aname] = dfA
print('ouput:')
for key, data in df_A_dict.items():
print(key)
print(data)
print()
C = np.kron(np.kron(A1,A2),A3)
Z = C.T @ Y3.reshape((Y3.size, 1))
df_Z_dict = {}
for i in range(1,S_lim + 1):
df_Z_dict['Z' + str(i)] = pd.DataFrame(Z[range(S_lim*S_lim*(i-1),S_lim*S_lim*i)].reshape((S_lim, S_lim)))
print()
print('tensor')
for key, data in df_Z_dict.items():
print(key)
print(data)
print()
with open('out.csv', 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['ouput'])
for key, data in df_A_dict.items():
rows = data.values.tolist();
for i in range(len(rows)):
rows[i].insert(0, data.index[i])
rows.insert(0, [''] + list(data.columns));
writer.writerow([key])
writer.writerows(rows)
writer.writerow([])
writer.writerow(['tensor'])
for key, data in df_Z_dict.items():
rows = data.values.tolist();
for i in range(len(rows)):
rows[i].insert(0, data.index[i])
rows.insert(0, [''] + list(data.columns));
writer.writerow([key])
writer.writerows(rows)
writer.writerow([])
#