-
Notifications
You must be signed in to change notification settings - Fork 0
/
sarcoidosis_experiment_3_altered_fuzzy_gp.py
193 lines (149 loc) · 6.75 KB
/
sarcoidosis_experiment_3_altered_fuzzy_gp.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
import time
import pandas as pd
from util import evaluate_grid_model
from plot_roc import plotroc
from scipy.io import loadmat
import functools # flatten list of list
import operator # flatten List of list
from sklearn.feature_selection import RFE
from sklearn.svm import LinearSVC
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn import metrics
from gplearn.genetic import SymbolicClassifier
from gplearn.fitness import _Fitness
import numpy as np
if __name__ == '__main__':
start = time.time()
Run = 1
filename = "sarcoidosis_experiment_3_altered_fuzzy_gp"
filename_dataset = "sa.csv"
filename_crossval = "sa_cvi.mat"
filename_matlab = "Exp_sarcoidose_sa.csv"
filename_matlab_cc = "cc_" + filename_matlab
filename_matlab_p = "p_" + filename_matlab
crossval_field = 'SA_CrossValIndex'
filename_csv = filename + ".csv"
filename_roc = "roc_" + filename_csv
filename_auc = "auc_" + filename_csv
filename_class = "class_" + filename_csv
filename_result = "result_" + filename_csv
filename_cc = "cc_" + filename_csv
filename_params = "params_" + filename_csv
filename_fig = filename + ".png"
cell_mat = loadmat(filename_crossval) # load crossval index
print(cell_mat.keys())
crossval_index = cell_mat[crossval_field]
print(type(crossval_index))
print(crossval_index.shape[0])
train_index = crossval_index[0][0]
valid_index = crossval_index[0][1]
test_index = crossval_index[0][2]
train_index = train_index.flatten()
valid_index = valid_index.flatten()
test_index = test_index.flatten()
print(train_index)
print(valid_index)
print(test_index)
# Obtain the result for best FOT parameter from matlab
matlab_df = pd.read_csv(filename_matlab, delimiter=',', encoding="utf-8-sig")
#
print(matlab_df.head())
print(matlab_df.columns)
bfp = matlab_df['BFP']
# Copy the cc values from Matlab
matlab_cc_df = pd.read_csv(filename_matlab_cc, delimiter=',', encoding="utf-8-sig")
bfp_cc = matlab_cc_df['BFP']
# Obtain the dataset
df = pd.read_csv(filename_dataset)
cols = list(df.columns)
cols.pop()
models = [
{
'label': 'GP',
'model': 'GP',
}
]
mfs = LinearSVC(C=1, penalty="l1", dual=False, max_iter=5000)
feat_selection = RFE(mfs, step=1)
if Run == 1:
seed = 7
num_folds = 10
num_class = 2
n_iter = 30
scoring = 'roc_auc'
print("seed: %f" % (seed))
print("n_iter: %d" % (n_iter))
print("scoring: %s" % (scoring))
# ===================
# Genetic Programming
# ===================
print('-- GP --')
def _roc_auc(y, y_pred, w):
"""Calculate the AUC."""
proba = np.vstack([1 - y_pred, y_pred]).T
fpr, tpr, threshold = metrics.roc_curve(y, proba[:, 1])
return metrics.auc(fpr, tpr)
roc_auc = _Fitness(function=_roc_auc, greater_is_better=True)
param_grid = {
'fs__n_features_to_select': [12, 24, 36],
'GP__init_depth': [(2, 2), (2, 6)],
'GP__population_size': [100, 300, 500, 1000, 3000],
'GP__tournament_size': [2, 7, 20],
'GP__generations': [20, 50, 100, 200],
'GP__p_crossover': [0.8]
}
# create pipeline
estimators = []
estimators.append(('standardize', StandardScaler()))
estimators.append(('fs', feat_selection))
estimators.append(('GP', SymbolicClassifier(random_state=seed, #feature_names=cols,
metric=roc_auc,
stopping_criteria=0.01, n_jobs=-1,
p_subtree_mutation=0.01,
p_point_replace=0.05,
function_set=('add', 'sub', 'mul', 'div'),
init_method='half and half',
const_range=(-1.,1.),
p_hoist_mutation=0.01, #combate bloat 0.05
p_point_mutation=0.01,
max_samples=1, verbose=0,
parsimony_coefficient=0.001, low_memory=True)))
model = Pipeline(estimators)
options = {'Importance', 'GP', 'fuzzy'}
gp_grid_result, gp_class, gp_auc, gp_probs, gp_preds, gp_score, gp_params, gp_features, gp_cc = evaluate_grid_model(
df, crossval_index, model, param_grid, scoring, num_folds, seed, options, num_class)
classes = functools.reduce(operator.iconcat, gp_class, [])
y_probs = functools.reduce(operator.iconcat, gp_probs, []) # flatten the list of lists
fpr, tpr, thresholds = metrics.roc_curve(classes, y_probs, drop_intermediate=False)
print(f'AUC = {metrics.auc(fpr, tpr)}')
# Store the result in dataframe
class_df = pd.DataFrame(columns=['BFP', 'GP'])
class_df['BFP'] = matlab_df['class'] # comes from matlab
class_df['GP'] = functools.reduce(operator.iconcat, gp_class, [])
class_df = pd.DataFrame(columns=['BFP'])
class_df['BFP'] = matlab_df['class'] # comes from matlab
class_df['GP'] = functools.reduce(operator.iconcat, gp_class, [])
class_df.to_csv(filename_class, index=False)
params_df = pd.DataFrame(columns=['GP'])
params_df['GP'] = pd.Series(gp_params) # Transform to series
params_df.to_csv(filename_params, index=False)
cc_df = pd.DataFrame(columns=['GP'])
cc_df['GP'] = functools.reduce(operator.iconcat, gp_cc, []) # Transform to series
cc_df.to_csv(filename_cc, index=False)
# Put the results togpther in dataframe
result_df = pd.DataFrame(columns=['BFP', 'GP', 'class'])
result_df['BFP'] = bfp # comes from matlab
result_df['GP'] = functools.reduce(operator.iconcat, gp_probs, []) # flatten the list of lists
result_df['class'] = functools.reduce(operator.iconcat, gp_class, []) # flatten the list of lists
result_df.to_csv(filename_result, index=False)
else:
print('Obtained results from files')
result_df = pd.read_csv(filename_result)
# Prepare to show the results
roc_df, fig = plotroc(result_df, models, [0.90, 0.75])
fig.savefig(filename_fig, format='png', dpi=300)
roc_df.to_csv(filename_roc, index=True)
print(roc_df)
end = time.time()
print("Elapsed time: ", end - start)