-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGIT_milp_multiclass_11_65.py
340 lines (287 loc) · 13.6 KB
/
GIT_milp_multiclass_11_65.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# -*- coding: utf-8 -*-
"""
Prediction of representative phenotypes using multi-output subset selection
Author: [email protected]
If you find it useful, please cite our paper as follows:
@phdthesis{wang2020data,
title={Data analytics and optimization methods in biomedical systems: from microbes to humans},
author={Wang, Taiyao},
year={2020}
}
In the python code, I use microF1 as metrics for RF/LR. One interesting fact: the micro average for binary classification (not for >=3 classes) is equal for Precision, Recall and F1 score
because micro averaging these metrics results in overall Accuracy.
"""
from sklearn.metrics import f1_score, accuracy_score
import warnings
from iterstrat.ml_stratifiers import MultilabelStratifiedKFold
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegressionCV
from gurobipy import *
import time
import numpy as np
import pandas as pd
import seaborn as sns
sns.set()
warnings.filterwarnings("ignore")
def MMRP_classify2021(X, r, z_start=None, z_greedy=None, n_greedy=None, OutputFlag=0, Ncpu=30, LP=False, lamda=0, include=None, exclude=None, MIPFocus=0): # ,M=100
n, d = X.shape
# assert n == len(y)
m = Model()
# addVars
B = m.addVars(d, d, lb=-GRB.INFINITY)
Babs = m.addVars(d, d, lb=0)
b0 = m.addVars(d, lb=-GRB.INFINITY) # intercept
z = m.addVars(d, vtype=GRB.BINARY)
zz = m.addVars(d, vtype=GRB.BINARY)
# Y = m.addVars(n, d, lb = -GRB.INFINITY) #t_ij
KSI = m.addVars(n, d, lb=0)
# Y_X = m.addVars(n, d, lb = -GRB.INFINITY)
# addConstr
# In an SOS constraint of type 1 (an SOS1 constraint), at most one variable in the specified list is allowed to take a non-zero value.
for j in range(d):
m.addConstr(zz[j] == 1-z[j])
m.addConstr(quicksum(z[j] for j in range(d)) <= r)
for i in range(n):
for j in range(d):
# overloaded form
m.addConstr((z[j] == 0) >> (
X[i, j]*(b0[j]+quicksum([B[k, j]*X[i, k] for k in range(d)])) >= 1-KSI[i, j]))
for k in range(d):
for j in range(d):
m.addSOS(GRB.SOS_TYPE1, [B[j, k], zz[j]], [1, 2])
m.addConstr(Babs[j, k] == abs_(B[j, k]), name="absconstr")
if z_start is not None:
for j in range(d):
z[j].start = z_start[j]
zz[j].start = 1-z_start[j]
if z_greedy is not None:
m.addConstr(quicksum(z_greedy[j]*z[j] for j in range(d)) >= r-n_greedy)
if LP:
for j in range(d):
m.addConstr(z[j] == z_start[j])
m.addConstr(zz[j] == 1-z_start[j])
if include is not None:
for j in include:
m.addConstr(z[j] == 1)
if exclude is not None:
for j in exclude:
m.addConstr(z[j] == 0)
# m.Params.method = 2
# m.params.DisplayInterval = 300
m.Params.Threads = Ncpu
m.Params.OutputFlag = OutputFlag # 1#0#
if MIPFocus != 0:
m.Params.MIPFocus = MIPFocus
# m.setParam("TimeLimit", TimeLimit)
obj2 = quicksum(KSI[i, j] for i in range(n) for j in range(
d))/n + lamda*quicksum(Babs[j, k] for k in range(d) for j in range(d))
m.setObjective(obj2, GRB.MINIMIZE)
# solve model
m.optimize()
bestB = np.zeros((d, d))
for i in range(d):
for j in range(d):
bestB[i, j] = B[i, j].X
bestKSI = np.zeros((n, d))
for i in range(n):
for j in range(d):
bestKSI[i, j] = KSI[i, j].X
bestb0 = np.zeros((d,))
bestz = np.zeros((d,))
for j in range(d):
bestb0[j] = b0[j].X
bestz[j] = z[j].X
return bestB, bestb0, bestz, bestKSI, m.getObjective().getValue()
def get_score(df_z_all, f1_micro_all, names_y_all, col_score='f1_score', clf='classify_RF'):
output = f'../result/MIP_{clf}_{col_score}.csv'
f1_scores = []
for i in range(len(f1_micro_all)):
for nam, j in zip(names_y_all[i], f1_micro_all[i]):
f1_scores.append((df_z_all.shape[0]-i, nam, j))
f1_scores = pd.DataFrame.from_records(
f1_scores, columns=['# of predictors', 'response', col_score])
f1_scores.to_csv(output, index=False)
return f1_scores
if __name__ == '__main__':
# M = 1000
# Ncpu = 16
# TimeLimit = 1500
# Snitkin_RefinedExperimentalData.csv
df = pd.read_csv('../data/growth_profiles_binary.csv',
index_col='strain') # ,index_col='Mutant'
df_new = df.astype(float)
X = df_new.values
n, d = X.shape
"""## only for classify, 0 -->-1 """
X[X == 0] = -1
"""# MMRP_classify
['D-Glucose_1.0', 'D-Glucose_2.0', 'Inulin_1.0', 'Inulin_2.0',
'myo-Inositol_1.0', 'myo-Inositol_2.0', 'Methanol_1.0',
'Methanol_2.0']
"""
my_range = range(d-1, 0, -1) # range(d-2,0,-2)#
my_l = len(my_range) # d-1#37
myseed = 2020
rand = np.random.RandomState([myseed])
z_all = []
B_all = []
obj_all = []
for r in my_range:
bestB, bestb0, bestz, bestKSI, obj = MMRP_classify2021(X, r)
z_all.append(bestz)
B_all.append(bestB)
obj_all.append(obj)
plt.figure(figsize=(20, 8))
sns.heatmap(np.array(z_all).astype(int),
xticklabels=df_new.columns.values,
yticklabels=my_range, vmin=0, vmax=1)
df_z_all = pd.DataFrame.from_records(z_all, columns=df_new.columns)
df_z_all.to_csv('../result/MIP_classify_z_all_11.csv', index=False)
"""## obj_all increase when we have fewer features and more responses"""
pd.Series(obj_all).plot()
plt.figure(figsize=(20, 18))
sns.heatmap(np.array(z_all).T.astype(int),
xticklabels=my_range, # str_times,
yticklabels=df_new.columns.values, vmin=0, vmax=1)
df3sum = df_z_all.sum()
df3sum.plot()
"""# times of selected"""
df_z_all = pd.read_csv('../result/MIP_classify_z_all_11.csv')
dfsum = df.sum().to_frame('sum of variables')
dfsum['sum of selected times'] = df_z_all.sum().values
dfsum.sort_values('sum of variables').plot(
x='sum of variables', y='sum of selected times')
a4_dims = (6.7, 4.27)
fig, ax = plt.subplots(figsize=a4_dims)
sns.scatterplot(ax=ax, x="sum of variables", y="sum of selected times",
data=dfsum.sort_values('sum of variables'))
dfstd = df.std().to_frame('std of variables')
dfstd['sum of selected times'] = df_z_all.sum().values
a4_dims = (6.7, 4.27)
fig, ax = plt.subplots(figsize=a4_dims)
sns.scatterplot(ax=ax, x="std of variables", y="sum of selected times",
data=dfstd.sort_values('std of variables'))
"""# predict setting--MultilabelStratifiedKFold
iterative-stratification is a project that provides scikit-learn compatible cross validators with stratification for multilabel data.
Presently scikit-learn provides several cross validators with stratification. However, these cross validators do not offer the ability to stratify multilabel data. This iterative-stratification project offers implementations of MultilabelStratifiedKFold, MultilabelRepeatedStratifiedKFold, and MultilabelStratifiedShuffleSplit with a base algorithm for stratifying multilabel data described in the following paper:
Sechidis K., Tsoumakas G., Vlahavas I. (2011) On the Stratification of Multi-Label Data. In: Gunopulos D., Hofmann T., Malerba D., Vazirgiannis M. (eds) Machine Learning and Knowledge Discovery in Databases. ECML PKDD 2011. Lecture Notes in Computer Science, vol 6913. Springer, Berlin, Heidelberg.
"""
# dfc=pd.read_csv('../data/growth_profiles_continuous.csv',index_col='strain') #,index_col='Mutant'
# from sklearn.preprocessing import KBinsDiscretizer
# est = KBinsDiscretizer(n_bins=10, encode='ordinal', strategy='quantile')
# y_=est.fit_transform(dfc['HMBoligo'].values.reshape(-1, 1))
# pd.Series(np.array(y_).ravel()).value_counts()
X = df.values
names_raw = df.columns.values
n, d = X.shape
z_last = df_z_all.iloc[-1, :].values
y_last = X[:, z_last == 0]
myseed = 2020
cvFolds = 5
r_ = np.random.RandomState([myseed])
"""# LogisticRegressionCV"""
acc_all = []
f1_micro_all = []
for i in range(0, df_z_all.shape[0]):
z = df_z_all.iloc[i, :].values
y_split = X[:, z == 0]
if i == 0:
folds = StratifiedKFold(
n_splits=cvFolds, shuffle=True, random_state=myseed)
else:
folds = MultilabelStratifiedKFold(
n_splits=cvFolds, shuffle=True, random_state=myseed)
name_i = names_raw[z == 0]
for fold_, (trn_idx, test_idx) in enumerate(folds.split(X, y_split)):
print('r, and fold_==', d-i-1, fold_, end='; ')
train_X, test_X = X[trn_idx][:, z == 1], X[test_idx][:, z == 1]
train_y, test_y = X[trn_idx][:, z == 0], X[test_idx][:, z == 0]
if fold_ == 0:
print('name_i,train_y.shape, train_y.mean, test_y.mean:',
name_i, train_y.shape, train_y.mean(), test_y.mean())
for j in range(train_y.shape[1]):
if (train_y[:, j] == 0).sum() > cvFolds:
clf = LogisticRegressionCV(Cs=[10**-2, 10**-1, 10**0], class_weight='balanced',
tol=0.001, cv=cvFolds, random_state=myseed).fit(train_X, train_y[:, j])
else: # not cv due to ValueError: Class label 0 not present.
clf = LogisticRegression(
C=10**-1, class_weight='balanced', tol=0.001, random_state=myseed).fit(train_X, train_y[:, j])
y_te_pre = clf.predict(test_X)
acc_all.append(accuracy_score(test_y[:, j], y_te_pre))
f1_micro_all.append([df_z_all.shape[0]-i, fold_, name_i[j],
f1_score(test_y[:, j], y_te_pre, average='micro')])
f1_scores = get_score(
f1_micro_all, col_score='f1_score', clf='classify_LR')
f1_scores.tail()
"""# RandomForestClassifier"""
acc_all = []
f1_micro_all = []
for i in range(0, df_z_all.shape[0]):
z = df_z_all.iloc[i, :].values
y_split = X[:, z == 0]
if i == 0:
folds = StratifiedKFold(
n_splits=cvFolds, shuffle=True, random_state=myseed)
else:
folds = MultilabelStratifiedKFold(
n_splits=cvFolds, shuffle=True, random_state=myseed)
name_i = names_raw[z == 0]
for fold_, (trn_idx, test_idx) in enumerate(folds.split(X, y_split)):
print('r, and fold_==', d-i-1, fold_, end='; ')
train_X, test_X = X[trn_idx][:, z == 1], X[test_idx][:, z == 1]
train_y, test_y = X[trn_idx][:, z == 0], X[test_idx][:, z == 0]
if fold_ == 0:
print('name_i,train_y.shape, train_y.mean, test_y.mean:',
name_i, train_y.shape, train_y.mean(), test_y.mean())
rfc = RandomForestClassifier(
n_estimators=10, random_state=myseed, n_jobs=5, min_samples_split=4, class_weight="balanced")
for j in range(train_y.shape[1]):
if (train_y[:, j] == 0).sum() > cvFolds:
rfc.fit(train_X, train_y[:, j])
y_te_pre = rfc.predict(test_X)
else: # not cv due to ValueError: Class label 0 not present.
CV_rfc = GridSearchCV(
estimator=rfc, param_grid=param_grid, cv=cvFolds, n_jobs=9)
CV_rfc.fit(train_X, train_y[:, j])
y_te_pre = CV_rfc.predict(test_X)
acc_all.append(accuracy_score(test_y[:, j], y_te_pre))
f1_micro_all.append([df_z_all.shape[0]-i, fold_, name_i[j],
f1_score(test_y[:, j], y_te_pre, average='micro')])
f1_scores = get_score(
f1_micro_all, col_score='f1_score', clf='classify_RF')
f1_scores.tail()
"""# compare with enumerate when 1 response"""
acc_all = []
f1_micro_all = []
for i in range(0, X.shape[1]):
X_split = X[:, [i_ for i_ in range(X.shape[1]) if i_ != i]]
y_split = X[:, i].reshape(-1, 1)
folds = StratifiedKFold(
n_splits=cvFolds, shuffle=True, random_state=myseed)
name_i = names_raw[i]
for fold_, (trn_idx, test_idx) in enumerate(folds.split(X, y_split)):
print('r, and fold_==', d-i-1, fold_, end='; ')
train_X, test_X = X_split[trn_idx], X_split[test_idx]
train_y, test_y = y_split[trn_idx], y_split[test_idx]
if fold_ == 0:
print('name_i,train_y.shape, train_y.mean, test_y.mean:',
name_i, train_y.shape, train_y.mean(), test_y.mean())
rfc = RandomForestClassifier(
n_estimators=10, random_state=myseed, n_jobs=5, min_samples_split=4, class_weight="balanced")
for j in range(train_y.shape[1]):
if (train_y[:, j] == 0).sum() > cvFolds:
rfc.fit(train_X, train_y[:, j])
y_te_pre = rfc.predict(test_X)
else: # not cv due to ValueError: Class label 0 not present.
CV_rfc = GridSearchCV(
estimator=rfc, param_grid=param_grid, cv=cvFolds, n_jobs=9)
CV_rfc.fit(train_X, train_y[:, j])
y_te_pre = CV_rfc.predict(test_X)
acc_all.append(accuracy_score(test_y[:, j], y_te_pre))
f1_micro_all.append([i, fold_, name_i, f1_score(
test_y[:, j], y_te_pre, average='micro')])
f1_scores11 = get_score(
f1_micro_all, col_score='f1_score', clf='classify_RF')
f1_scores11.plot(kind='bar', x='response', y='mean')