-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWrapperComparisonJob.py
More file actions
238 lines (198 loc) · 9.34 KB
/
Copy pathWrapperComparisonJob.py
File metadata and controls
238 lines (198 loc) · 9.34 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
import os
import sys
import glob
import pandas as pd
from scipy import stats
import copy
def job(experiment_path):
# Get dataset paths
datasets = os.listdir(experiment_path)
datasets.remove('logs')
datasets.remove('jobs')
datasets.remove('jobsCompleted')
datasets.remove('metadata.csv')
dataset_directory_paths = []
for dataset in datasets:
full_path = experiment_path + "/" + dataset
dataset_directory_paths.append(full_path)
# Get algorithms
algorithms = []
name_to_abbrev = {'logistic_regression': 'LR', 'decision_tree': 'DT', 'random_forest': 'RF', 'naive_bayes': 'NB',
'XGB': 'XGB', 'LGB': 'LGB', 'ANN': 'ANN', 'SVM': 'SVM', 'ExSTraCS': 'ExSTraCS', 'eLCS': 'eLCS',
'XCS': 'XCS','gradient_boosting':'GB','k_neighbors':'KN'}
abbrev_to_name = dict([(value, key) for key, value in name_to_abbrev.items()])
for filepath in glob.glob(dataset_directory_paths[0] + '/training/pickledModels/*'):
algo_name = abbrev_to_name[filepath.split('/')[-1].split('_')[0]]
if not algo_name in algorithms:
algorithms.append(algo_name)
# Get Metrics
data = pd.read_csv(dataset_directory_paths[0] + '/training/results/Summary_performance_mean.csv', sep=',')
metrics = data.columns.values.tolist()[1:]
# Significance Cutoff
sig_cutoff = 0.05
# Create new directory
if not os.path.exists(experiment_path+'/DatasetComparisons'):
os.mkdir(experiment_path+'/DatasetComparisons')
# Kruscall Wallis (ANOVA-like) comparison between datasets
label = ['statistic', 'pvalue', 'sig']
for dataset in datasets:
label.append('mean_' + dataset)
label.append('std_' + dataset)
for algorithm in algorithms:
kruskal_summary = pd.DataFrame(index=metrics, columns=label)
for metric in metrics:
tempArray = []
aveList = []
sdList = []
for dataset_path in dataset_directory_paths:
filename = dataset_path+'/training/results/'+name_to_abbrev[algorithm]+'_performance.csv'
td = pd.read_csv(filename)
tempArray.append(td[metric])
aveList.append(td[metric].mean())
sdList.append(td[metric].std())
try:
result = stats.kruskal(*tempArray)
except:
result = [tempArray[0][0],1]
kruskal_summary.at[metric, 'statistic'] = str(round(result[0], 6))
kruskal_summary.at[metric, 'pvalue'] = str(round(result[1], 6))
if result[1] < sig_cutoff:
kruskal_summary.at[metric, 'sig'] = str('*')
else:
kruskal_summary.at[metric, 'sig'] = str('')
for j in range(len(aveList)):
kruskal_summary.at[metric, 'mean_' + datasets[j]] = str(round(aveList[j], 6))
kruskal_summary.at[metric, 'std_' + datasets[j]] = str(round(sdList[j], 6))
kruskal_summary.to_csv(experiment_path+'/DatasetComparisons/'+algorithm+'_KruskalWallis.csv')
# Mann-Whitney U test (Pairwise Comparisons)
label = ['metric', 'dataset1', 'dataset2', 'statistic', 'pvalue', 'sig']
for i in range(2):
label.append('mean_dataset' + str(i))
label.append('std_dataset' + str(i))
for algorithm in algorithms:
master_list = []
for metric in metrics:
for x in range(0,len(dataset_directory_paths)-1):
for y in range(x+1,len(dataset_directory_paths)):
tempList = []
file1 = dataset_directory_paths[x]+'/training/results/'+name_to_abbrev[algorithm]+'_performance.csv'
td1 = pd.read_csv(file1)
set1 = td1[metric]
ave1 = td1[metric].mean()
sd1 = td1[metric].std()
file2 = dataset_directory_paths[y] + '/training/results/' + name_to_abbrev[algorithm] + '_performance.csv'
td2 = pd.read_csv(file2)
set2 = td2[metric]
ave2 = td2[metric].mean()
sd2 = td2[metric].std()
combined = list(copy.deepcopy(set1))
combined.extend(list(set2))
if all(x == combined[0] for x in combined): # Check if all nums are equal in sets
result = [combined[0], 1]
else:
result = stats.mannwhitneyu(set1, set2)
tempList.append(str(metric))
tempList.append(str(datasets[x]))
tempList.append(str(datasets[y]))
tempList.append(str(round(result[0], 6)))
tempList.append(str(round(result[1], 6)))
if result[1] < sig_cutoff:
tempList.append(str('*'))
else:
tempList.append(str(''))
tempList.append(str(round(ave1, 6)))
tempList.append(str(round(sd1, 6)))
tempList.append(str(round(ave2, 6)))
tempList.append(str(round(sd2, 6)))
master_list.append(tempList)
df = pd.DataFrame(master_list)
df.columns = label
df.to_csv(experiment_path+'/DatasetComparisons/' +algorithm+ '_MannWhitney.csv')
#Best Kruskal Wallis results comparison
label = ['statistic', 'pvalue', 'sig']
for dataset in datasets:
label.append('best_alg_' + dataset)
label.append('mean_' + dataset)
label.append('std_' + dataset)
kruskal_summary = pd.DataFrame(index=metrics, columns=label)
global_data = []
for metric in metrics:
best_list = []
best_data = []
for dataset_path in dataset_directory_paths:
alg_ave = []
alg_st = []
alg_data = []
for algorithm in algorithms:
filename = dataset_path+'/training/results/'+name_to_abbrev[algorithm]+'_performance.csv'
td = pd.read_csv(filename)
alg_ave.append(td[metric].mean())
alg_st.append(td[metric].std())
alg_data.append(td[metric])
# Find best algorithm for given metric based on average
best_ave = max(alg_ave)
best_index = alg_ave.index(best_ave)
best_sd = alg_st[best_index]
best_alg = algorithms[best_index]
best_data.append(alg_data[best_index])
best_list.append([best_alg, best_ave, best_sd])
global_data.append([best_data, best_list])
result = stats.kruskal(*best_data)
kruskal_summary.at[metric, 'statistic'] = str(round(result[0], 6))
kruskal_summary.at[metric, 'pvalue'] = str(round(result[1], 6))
if result[1] < sig_cutoff:
kruskal_summary.at[metric, 'sig'] = str('*')
else:
kruskal_summary.at[metric, 'sig'] = str('')
for j in range(len(best_list)):
kruskal_summary.at[metric, 'best_alg_' + datasets[j]] = str(best_list[j][0])
kruskal_summary.at[metric, 'mean_' + datasets[j]] = str(round(best_list[j][1], 6))
kruskal_summary.at[metric, 'std_' + datasets[j]] = str(round(best_list[j][2], 6))
kruskal_summary.to_csv(experiment_path + '/DatasetComparisons/BestCompare_KruskalWallis.csv')
# Best Mann Whitney (Pairwise comparisons)
label = ['metric', 'dataset1', 'dataset2', 'statistic', 'pvalue', 'sig']
for i in range(2):
label.append('best_alg' + str(i))
label.append('mean_dataset' + str(i))
label.append('std_dataset' + str(i))
master_list = []
j = 0
for metric in metrics:
for x in range(0, len(datasets) - 1):
for y in range(x + 1, len(datasets)):
tempList = []
set1 = global_data[j][0][x]
ave1 = global_data[j][1][x][1]
sd1 = global_data[j][1][x][2]
set2 = global_data[j][0][y]
ave2 = global_data[j][1][y][1]
sd2 = global_data[j][1][y][2]
combined = list(copy.deepcopy(set1))
combined.extend(list(set2))
if all(x == combined[0] for x in combined): # Check if all nums are equal in sets
result = [combined[0], 1]
else:
result = stats.mannwhitneyu(set1, set2)
tempList.append(str(metric))
tempList.append(str(datasets[x]))
tempList.append(str(datasets[y]))
tempList.append(str(round(result[0], 6)))
tempList.append(str(round(result[1], 6)))
if result[1] < sig_cutoff:
tempList.append(str('*'))
else:
tempList.append(str(''))
tempList.append(global_data[j][1][x][0])
tempList.append(str(round(ave1, 6)))
tempList.append(str(round(sd1, 6)))
tempList.append(global_data[j][1][y][0])
tempList.append(str(round(ave2, 6)))
tempList.append(str(round(sd2, 6)))
master_list.append(tempList)
j += 1
df = pd.DataFrame(master_list)
df.columns = label
df.to_csv(experiment_path + '/DatasetComparisons/BestCompare_MannWhitney.csv')
print("Phase 6 complete")
if __name__ == '__main__':
job(sys.argv[1])