forked from TeamLIR/SigmaLaw-PBSA-Final
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ensemble.py
73 lines (58 loc) · 2.16 KB
/
ensemble.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
# stacked generalization with linear meta model on blobs dataset
import pickle
import pandas as pd
from sklearn.datasets import make_blobs
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from numpy import dstack
import numpy as np
import os
from sklearn.metrics import f1_score
from ensemblePredict import get_model
from predict import main
# create stacked model input dataset as outputs from the ensemble
def stacked_dataset(members):
stackX = None
yhat = get_model(members)
for i in yhat:
ypred=i
# stack predictions into [rows, members, probabilities]
if stackX is None:
stackX = i
else:
stackX = dstack((stackX, i))
# flatten predictions to [rows, members x probabilities]
print(type(stackX))
stackX = stackX.reshape((stackX.shape[0], stackX.shape[1] * stackX.shape[2]))
return stackX
# fit a model based on the outputs from the ensemble members
def fit_stacked_model(members, inputy):
# create dataset using ensemble
stackedX = stacked_dataset(members)
# fit standalone model
model = LogisticRegression()
model.fit(stackedX, inputy)
return model
# make a prediction with the stacked model
def stacked_prediction(members, model):
# create dataset using ensemble
stackedX = stacked_dataset(members)
# make a prediction
yhat = model.predict(stackedX)
filename = 'finalized_model.sav'
pickle.dump(model, open(filename, 'wb'))
return yhat
if __name__ == '__main__':
train_data = pd.read_csv('./datasets/semeval14/test.csv', header=0, index_col=None)
models = [ "bert_spc",'bert_atae_lstm',"gcn_bert","ram_bert","lcf_bert"]
a=train_data['sentiment'].values.tolist()
train_data['sentiment'] += 1
b = train_data['sentiment'].values.tolist()
testy = np.array(train_data['sentiment'].values.tolist())
model = fit_stacked_model(models, testy)
# evaluate model on test set
yhat = stacked_prediction(models, model)
acc = accuracy_score(testy, yhat)
print('Stacked Test Accuracy: %.3f' % acc)
f1 = f1_score(testy, yhat, average='macro')
print('Stacked f1 score: %.3f' % f1)