-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_experiment.py
More file actions
194 lines (181 loc) · 7.41 KB
/
run_experiment.py
File metadata and controls
194 lines (181 loc) · 7.41 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
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import argparse
import json
import os
from model.utils import *
import math
# model name list
tree_based_models = [
'LightGBM',
'XGBoost',
'CatBoost'
]
deep_learning_models = [
'danets',
'mlp',
'node',
'resnet',
'switchtab',
'tabcaps',
'tabnet',
'tangos',
'autoint',
'dcn2',
'ftt',
'grownet',
'saint',
'snn',
'tabtransformer',
'tabr',
'modernNCA',
'TabPFN'
]
llms = [
'Llama3-8B'
]
tabularllms = [
'TabLLM',
'UniPredict'
]
def pearson(df, ascending):
correlation = df.corr(method='pearson')
last_column_correlation = correlation.iloc[:, -1]
last_column_correlation = last_column_correlation.drop(last_column_correlation.index[last_column_correlation == 1])
sorted_correlation = last_column_correlation.abs().sort_values(ascending=ascending)
sorted_columns = sorted_correlation.index
return sorted_columns
def split_dataset(dataset, task, degree):
filename = './dataset/' + dataset + '/' + dataset + '.csv'
df = pd.read_csv(filename)
train_set, test_set = train_test_split(df, test_size=0.2, random_state=42)
all_test_sets = []
all_test_sets.append(test_set) # In Distribution
if task == 'single':
data = df.copy()
data[data.columns[data.dtypes == 'object']] = data.select_dtypes(['object']).apply(lambda x: pd.Categorical(x).codes)
sorted_columns = pearson(data, ascending=True)
for num in range(1, len(test_set.columns)):
test = test_set.copy()
if test[sorted_columns[num-1]].dtype == 'object':
mode_value = train_set[sorted_columns[num-1]].mode()[0]
test[sorted_columns[num-1]] = mode_value
else:
column_means = train_set[sorted_columns[num-1]].mean()
test[sorted_columns[num-1]] = column_means
all_test_sets.append(test)
elif task == 'least':
data = df.copy()
data[data.columns[data.dtypes == 'object']] = data.select_dtypes(['object']).apply(
lambda x: pd.Categorical(x).codes)
sorted_columns = pearson(data, ascending=True)
test = test_set.copy()
for num in range(1, len(test.columns)):
for i in range(0, num):
if test[sorted_columns[i]].dtype == 'object':
mode_value = train_set[sorted_columns[i]].mode()[0]
test[sorted_columns[i]] = mode_value
else:
column_means = train_set[sorted_columns[i]].mean()
test[sorted_columns[i]] = column_means
all_test_sets.append(test)
elif task == 'most':
data = df.copy()
data[data.columns[data.dtypes == 'object']] = data.select_dtypes(['object']).apply(
lambda x: pd.Categorical(x).codes)
sorted_columns = pearson(data, ascending=False)
test = test_set.copy()
for num in range(1, len(test.columns)):
for i in range(0, num):
if test[sorted_columns[i]].dtype == 'object':
mode_value = train_set[sorted_columns[i]].mode()[0] # mode() 返回一个序列,取第一个值
test[sorted_columns[i]] = mode_value
else:
column_means = train_set[sorted_columns[i]].mean()
test[sorted_columns[i]] = column_means
all_test_sets.append(test)
elif task == 'random':
if degree != 'all':
degree = float(degree)
num = math.floor(degree * len(train_set.columns))
combinations = list(itertools.combinations(df.columns[:-1], num))
print("combinations are: ", combinations)
part_test_sets = []
for combination in combinations:
test = test_set.copy()
column_list = train_set.columns.tolist()
for i in combination:
index = column_list.index(i)
if test.iloc[:, index].dtype == 'object':
mode_value = train_set.iloc[:, index].mode()[0]
test.iloc[:, index] = mode_value
else:
column_means = train_set.iloc[:, index].mean()
test.iloc[:, index] = column_means
test = test[df.columns]
part_test_sets.append(test)
part_test_sets = pd.concat(part_test_sets, ignore_index=True)
all_test_sets.append(part_test_sets)
else:
for num in range(1, len(df.columns)):
part_test_sets = []
combinations = list(itertools.combinations(df.columns[:-1], num))
print("combinations are: ", combinations)
for combination in combinations:
test = test_set.copy()
column_list = train_set.columns.tolist()
for i in combination:
index = column_list.index(i)
if test.iloc[:, index].dtype == 'object':
mode_value = train_set.iloc[:, index].mode()[0]
test.iloc[:, index] = mode_value
else:
column_means = train_set.iloc[:, index].mean()
test.iloc[:, index] = column_means
test = test[df.columns]
part_test_sets.append(test)
part_test_sets = pd.concat(part_test_sets, ignore_index=True)
all_test_sets.append(part_test_sets)
return train_set, all_test_sets
def evaluate_model(dataset, model, train_set, test_sets):
if model in tabularllms:
tabular_llm(dataset, model, train_set, test_sets)
elif model in llms:
llm(dataset, model, train_set, test_sets)
elif model in deep_learning_models:
deep_learning(dataset, model, train_set, test_sets)
else:
tree_model(dataset, model, train_set, test_sets)
def main(dataset, model, task, degree, export_dataset):
# 1. get train and test set
train_set, test_sets = split_dataset(dataset, task, degree)
# 2. whether to export the dataset or not
if export_dataset:
train_set.to_csv('train.csv', index=False)
for index, test_set in enumerate(test_sets):
test_set.to_csv('test_' + str(index) + '.csv', index=False)
# 3. evaluate model
evaluate_model(dataset, model, train_set, test_sets)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, required=True,
help="Dataset Name")
parser.add_argument('--model', type=str, required=True,
help="Model Name")
parser.add_argument('--task', type=str, required=True,
help="Task Name")
parser.add_argument('--degree', type=str, default="all",
help="Feature Shift Degree")
parser.add_argument('--export_dataset', type=bool, default=False,
help="whether to export the dataset or not")
args = parser.parse_args()
dataset = args.dataset
model = args.model
task = args.task
degree = args.degree
export_dataset = args.export_dataset
if task == 'random' and degree == None:
print("Please specify a degree for random")
else:
main(dataset, model, task, degree, export_dataset)