-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstacking.py
More file actions
52 lines (37 loc) · 1.46 KB
/
stacking.py
File metadata and controls
52 lines (37 loc) · 1.46 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
import numpy as np
from logistic_regression import LogisticRegression
class Stacking:
def __init__(self, *arg):
self.models = [i[0] for i in arg[0]]
self.freq = [i[1] for i in arg[0]]
self.trained_models = list()
self.stacked_model = None
def stacking_predict(self, models, row):
stacked_row = list()
for i in models:
prediction = i.predict(models[i], row)
stacked_row.append(prediction)
stacked_row.append(row[-1])
return row[0:len(row) - 1] + stacked_row
def train(self, X, y):
for clf, freq in zip(self.models, self.freq):
for i in range(freq):
self.trained_models.append(clf.train(X, y))
stacked_preds = []
for model in self.trained_models:
preds = model.predict(X)
stacked_preds.append(preds)
stacked_preds = list(map(list, zip(*stacked_preds)))
stacked_preds = np.asarray(stacked_preds)
self.stacked_model = LogisticRegression()
self.stacked_model.train(stacked_preds, y)
return self
def predict(self, X):
stacked_preds = []
for model in self.trained_models:
preds = model.predict(X)
stacked_preds.append(preds)
stacked_preds = list(map(list, zip(*stacked_preds)))
stacked_preds = np.asarray(stacked_preds)
predictions = self.stacked_model.predict(stacked_preds)
return predictions