-
Notifications
You must be signed in to change notification settings - Fork 1
/
all_kinds_of_test.py
215 lines (165 loc) · 8.06 KB
/
all_kinds_of_test.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
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
from origin_gcca import *
from spare_gcca import *
from cca import *
from deep_cca import *
from wgcca import *
from dgcca_format import *
import numpy as np
def t_std_gene_data(clf_, n = 10):
# read some data
data = data_generate()
name = ['Srbct', 'Leukemia', 'Lymphoma', 'Prostate', 'Brain', 'Colon']
for i in range(6):
train_corr = []
test_corr = []
train_acc = []
test_acc = []
spare = []
print()
print("finish reading data: ",name[i])
print()
for j in range(n):
# split data using different random_state
data.generate_genes_data(num=i, random_state=np.random.randint(1, 100, 1)[0])
# train gcca model
clf = clf_(ds=data, m_rank=2)
clf.solve()
# calculate all kind of metric
v1_test, v2_test = clf.transform(data.test_data)
train_corr.append(np.mean(clf.cal_correlation(clf.list_projection)))
test_corr.append(np.mean(clf.cal_correlation([v1_test, v2_test])))
train_acc.append(clf.cal_acc(clf.list_projection))
test_acc.append(clf.cal_acc([v1_test, v2_test]))
spare.append(clf.cal_spare()[0])
print("std of train correlation is: ", np.std(train_corr), ' mean is ', np.mean(train_corr))
print("std of test correlation is: ", np.std(test_corr),' mean is ', np.mean(test_corr))
print("std of train acc is: ", np.std(train_acc), ' mean is ',np.mean(train_acc))
print("std of test acc is: ", np.std(test_acc),' mean is ', np.mean(test_acc))
print("std of spare is: ", np.std(spare), np.mean(spare))
# print("| %.3f(%.3f) | %.3f(%.3f) | %.3f(%.3f) | %.3f(%.3f) | %.3f(%.3f) |"%(np.mean(test_corr),
# np.std(test_corr),
# np.mean(spare),
# np.std(spare),
# np.mean(train_acc),
# np.std(train_acc),
# np.mean(test_acc),
# np.std(test_acc),
# np.mean(train_corr),
# np.std(train_corr)))
print()
print()
def t_result_gene_data(clf_):
# read some data
data = data_generate()
name = ['Srbct', 'Leukemia', 'Lymphoma', 'Prostate', 'Brain', 'Colon']
for i in range(6):
data.generate_genes_data(num=i)
print()
print("finish reading data: ", name[i])
print()
# train gcca model
clf = clf_(ds=data, m_rank=2)
clf.solve()
# calculate all kind of metric
v1_test, v2_test = clf.transform(data.test_data)
print("total correlation in training data is: ", np.mean(clf.cal_correlation(clf.list_projection)))
print("total correlation in testing data is: ", np.mean(clf.cal_correlation([v1_test, v2_test])))
print("training data ACC is: ", clf.cal_acc(clf.list_projection))
print("testing data ACC is: ", clf.cal_acc([v1_test, v2_test]))
print("each view's spare of U is ", clf.cal_spare())
print("total sqare is: ", clf.cal_spare()[0])
print()
print()
def t_if_normalize_or_not(clf_):
# read some data
data = data_generate()
name = ['Srbct', 'Leukemia', 'Lymphoma', 'Prostate', 'Brain', 'Colon']
for c in [True, False]:
print("$$$$$$$$$$$normalize is: ", c)
for i in range(6):
data.generate_genes_data(num=i, normalize=c)
print()
print("finish reading data: ", name[i])
print()
# train model
clf = clf_(ds=data, m_rank=2)
clf.solve()
# calculate all kind of metric
v1_test, v2_test = clf.transform(data.test_data)
print("total correlation in training data is: ", np.mean(clf.cal_correlation(clf.list_projection)))
print("total correlation in testing data is: ", np.mean(clf.cal_correlation([v1_test, v2_test])))
print("training data ACC is: ", clf.cal_acc(clf.list_projection))
print("testing data ACC is: ", clf.cal_acc([v1_test, v2_test]))
print("each view's spare of U is ", clf.cal_spare())
print("total sqare is: ", clf.cal_spare()[0])
print()
print()
def t_three_view(clf_):
data = data_generate()
data.generate_three_view_tfidf_dataset()
clf = clf_(ds=data, m_rank=20)
clf.solve()
# calculate all kind of metric
print("reconstruction error of G in training is: ", clf.cal_G_error(data.train_data, test=False))
print("reconstruction error of G in testing is: ", clf.cal_G_error(data.test_data, test=True))
print("each view's spare of U is ", clf.cal_spare())
print("total sqare is: ", np.mean(clf.cal_spare()))
print()
print()
def t_synthetic_data(clf_):
data = data_generate()
data.generate_synthetic_dataset()
clf = clf_(ds=data, m_rank=2)
clf.solve()
# calculate all kind of metric
print("reconstruction error of G in training is: ", clf.cal_G_error(data.train_data, test=False))
print("reconstruction error of G in testing is: ", clf.cal_G_error(data.test_data, test=True))
print("each view's spare of U is ", clf.cal_spare())
print("total sqare is: ", np.mean(clf.cal_spare()))
print()
print()
return clf.cal_G_error(data.test_data, test=True)
if __name__ == "__main__":
# change the model below you like
# preserve all model in one list
print("Which model do you want to choose?\n0.gcca,\n1.spare_gcca,\n2.cca,\n3.deepcca,\n4.WeightedGCCA,\n5.dgcca_")
choose = int(input(">>>"))
clf_list = [gcca, spare_gcca, cca, deepcca, WeightedGCCA, dgcca_]
# choose which model you want to use
clf_ = clf_list[choose]
if choose in {0, 1, 2, 3}:
print("Which result do you want to test?\n0.test in gene data,\n1.test in gene data for std,\n2.test in gene data whether normalize or not,\n4. nothing")
choose1 = int(input(">>>"))
if choose1 == 0:
print ("################### start testing result #####################")
print()
t_result_gene_data(clf_)
print("################### finish testing result #####################")
print()
if choose1 == 1:
print("################### start testing std #####################")
print()
t_std_gene_data(clf_)
print("################### finish testing std #####################")
print()
if choose1 == 2:
print("################### start testing normalize or not #####################")
print()
t_if_normalize_or_not(clf_)
print("################### finish testing normalize or not #####################")
print()
if choose in {0, 1, 4, 5}:
print("Which result do you want to test?\n0.three view for language data,\n1.synthetic data\n2.nothing")
choose1 = int(input(">>>"))
if choose1 == 0:
print("################### start testing three views of language data #####################")
print()
t_three_view(clf_)
print("################### finish testing three views of language data #####################")
print()
if choose1 == 1:
print("###################start testing synthetic data #####################")
print()
t_synthetic_data(clf_)
print("###################finish testing synthetic data #####################")
print()