-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassifiers.py
More file actions
325 lines (197 loc) · 9.29 KB
/
Classifiers.py
File metadata and controls
325 lines (197 loc) · 9.29 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
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
"""File for implementation of different classifiers for multilabel prediction with TabPFN"""
import numpy as np
import pandas as pd
import random
from itertools import permutations
import collections
import warnings
from tabpfn import TabPFNClassifier
from sklearn.base import BaseEstimator, ClassifierMixin
class BinaryRelevance(ClassifierMixin, BaseEstimator):
def __init__(self, estimator, random_state=42, use_labels=False):
# Store parameters
self.random_state = random_state
self.estimator = estimator
self.use_labels = use_labels
# Initialize the underlying classifier with given parameters
#self.clf = self.estimator(**tabpfn_params)
def fit(self, X, Y, sample_weight=None, **fit_params):
self.estimators_ = []
if isinstance(X, np.ndarray):
col_names = ["X_" + str(s) for s in list(range(X.shape[1]))]
df_X = pd.DataFrame(X, columns=col_names)
else:
df_X = X
if isinstance(Y, np.ndarray):
col_names = ["Y_" + str(s) for s in list(range(Y.shape[1]))]
df_Y = pd.DataFrame(Y, columns=col_names)
else:
df_Y = Y
for i in range(Y.shape[1]):
tmp_comb = df_X.join(df_Y)
tmp_comb.dropna(subset=df_Y.columns.values.tolist()[i], inplace=True)
if self.use_labels:
filt_X = tmp_comb.drop(df_Y.columns.values.tolist()[i], axis=1)
filt_Y = tmp_comb[df_Y.columns.values.tolist()[i]]
filt_y = np.asarray(filt_Y)
self.estimators_.append(self.estimator(random_state=self.random_state).fit(filt_X, filt_y[:, 0]))
else:
filt_X = tmp_comb[df_X.columns.values.tolist()]
filt_Y = tmp_comb[df_Y.columns.values.tolist()]
filt_y = np.asarray(filt_Y)
self.estimators_.append(self.estimator(random_state=self.random_state).fit(filt_X, filt_y[:, i]))
self.classes_ = [estimator.classes_ for estimator in self.estimators_]
return self
def predict(self, X, Y=None):
y = []
if self.use_labels:
if Y == None:
raise Exception("Labels not given for prediction")
else:
if isinstance(X, np.ndarray):
col_names = ["X_" + str(s) for s in list(range(X.shape[1]))]
df_X = pd.DataFrame(X, columns=col_names)
else:
df_X = X
if isinstance(Y, np.ndarray):
col_names = ["Y_" + str(s) for s in list(range(Y.shape[1]))]
df_Y = pd.DataFrame(Y, columns=col_names)
else:
df_Y = Y
tmp_comb = df_X.join(df_Y)
for i, e in enumerate(self.estimators_):
tmp_X = tmp_comb.drop(df_Y.columns.values.tolist()[i], axis=1)
y.append(e.predict(tmp_X))
else:
for e in self.estimators_:
y.append(e.predict(X))
return np.asarray(y).T
def predict_proba(self, X, Y=None):
if self.use_labels:
if Y == None:
raise Exception("Labels not given for prediction")
else:
results = []
if isinstance(X, np.ndarray):
col_names = ["X_" + str(s) for s in list(range(X.shape[1]))]
df_X = pd.DataFrame(X, columns=col_names)
else:
df_X = X
if isinstance(Y, np.ndarray):
col_names = ["Y_" + str(s) for s in list(range(Y.shape[1]))]
df_Y = pd.DataFrame(Y, columns=col_names)
else:
df_Y = Y
tmp_comb = df_X.join(df_Y)
for i, e in enumerate(self.estimators_):
tmp_X = tmp_comb.drop(df_Y.columns.values.tolist()[i], axis=1)
results.append(e.predict_proba(tmp_X))
else:
results = [estimator.predict_proba(X) for estimator in self.estimators_]
return results
class ClassifierChains(ClassifierMixin, BaseEstimator):
def __init__(self, estimator, random_state=42, order="random"):
# Store parameters
self.random_state = random_state
self.estimator = estimator
self.order = order
# Initialize the underlying classifier with given parameters
#self.clf = self.estimator(**tabpfn_params)
def fit(self, X, Y, sample_weight=None, **fit_params):
if isinstance(self.order, collections.abc.Sequence) and not isinstance(self.order, str) and len(self.order) == Y.shape[1]:
order = self.order
else:
if self.order != "random":
warnings.warn("Invalid ordering, contunuing with random")
random.seed(self.random_state)
order = list(range(Y.shape[1]))
random.shuffle(order)
self.order = order
#print(order)
self.estimators_ = []
if isinstance(X, np.ndarray):
col_names = ["X_" + str(s) for s in list(range(X.shape[1]))]
df_X = pd.DataFrame(X, columns=col_names)
else:
df_X = X
if isinstance(Y, np.ndarray):
col_names = ["Y_" + str(s) for s in list(range(Y.shape[1]))]
df_Y = pd.DataFrame(Y, columns=col_names)
# currently only works with binary due to cross_val_predict changin Nan to a number higher than number of classes i presume
df_Y.replace(2, np.nan, inplace=True)
else:
df_Y = Y
#currently only works with binary due to cross_val_predict changin Nan to a number higher than number of classes i presume
df_Y.replace(2, np.nan, inplace=True)
for est_num, i in enumerate(self.order):
tmp_X = df_X.copy()
tmp_comb = tmp_X.join(df_Y)
tmp_comb.dropna(subset=df_Y.columns.values.tolist()[i], inplace=True)
filt_X = tmp_comb[df_X.columns.values.tolist()]
filt_Y = tmp_comb[df_Y.columns.values.tolist()]
filt_y = np.asarray(filt_Y)
for j in range(est_num):
filt_X[("Feat_" + str(j))] = filt_y[:, self.order[j]]
self.estimators_.append(self.estimator(random_state=self.random_state).fit(filt_X, filt_y[:, i]))
self.classes_ = [estimator.classes_ for estimator in self.estimators_]
return
def predict(self, X):
if isinstance(X, np.ndarray):
col_names = ["X_" + str(s) for s in list(range(X.shape[1]))]
df_X = pd.DataFrame(X, columns=col_names)
else:
df_X = X
y = np.zeros((len(self.estimators_), X.shape[0]))
tmp_X = df_X.copy()
for est_num, i in enumerate(self.order):
if est_num != 0:
tmp_X[("Feat_" + str(est_num - 1))] = y[self.order[est_num - 1]]
y[i] = self.estimators_[est_num].predict(tmp_X)
return y.T
def predict_proba(self, X):
if isinstance(X, np.ndarray):
col_names = ["X_" + str(s) for s in list(range(X.shape[1]))]
df_X = pd.DataFrame(X, columns=col_names)
else:
df_X = X
tmp_X = df_X.copy()
results = [None] * len(self.estimators_)
for est_num, i in enumerate(self.order):
if est_num != 0:
tmp_X[("Feat_" + str(est_num - 1))] = y_pred_class
results[i] = self.estimators_[est_num].predict_proba(tmp_X)
y_pred_class = np.argmax(results[i], axis=1)
return np.stack(results, axis=1)
class Ensemble(ClassifierMixin, BaseEstimator):
def __init__(self, estimator, n_jobs=5, random_state=42, averaging=False):
# Store parameters
self.random_state = random_state
self.estimator = estimator
self.n_jobs = n_jobs
self.averaging = averaging
# Initialize the underlying classifier with given parameters
#self.clf = self.estimator(**tabpfn_params)
def fit(self, X, Y):
perms = list(permutations(range(Y.shape[1])))
if self.n_jobs > len(perms):
warnings.warn(f"# of jobs greater than # of possible combinations, reduction to # of combinations: {self.n_jobs} > {len(perms)}")
orders = perms
else:
random.seed(self.random_state)
orders = random.sample(perms, self.n_jobs)
self.chains_ = [self.estimator(TabPFNClassifier, random_state=self.random_state, order=order) for order in orders]
for chain in self.chains_:
chain.fit(X, Y)
self.classes_ = [chain.classes_ for chain in self.chains_]
return self
def predict(self, X):
if self.averaging:
return (np.median(np.array([chain.predict(X) for chain in self.chains_]), axis=0) > 0.5) * 1
else:
return np.array([chain.predict(X) for chain in self.chains_])
def predict_proba(self, X):
if self.averaging:
return np.mean(np.array([chain.predict_proba(X) for chain in self.chains_])[...,1], axis=0)
#return np.mean(np.array([chain.predict_proba(X) for chain in self.chains_]), axis=0)
else:
return np.array([chain.predict_proba(X) for chain in self.chains_])