-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHyperparameter_Token_Classification_SVM.py
174 lines (152 loc) · 6.03 KB
/
Hyperparameter_Token_Classification_SVM.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
from pathlib import Path
import os
import numpy as np
import pandas as pd
from sklearn.metrics import f1_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import accuracy_score
import fasttext.util
from sklearn.svm import SVC
from sklearn.utils import compute_class_weight
# A function that flattens a 2-D list
# into a 1-D list. The source of the function
# is: https://github.com/charles9n/bert-sklearn
# Passing Parameters:
# l: a 2-D list
# Return values:
# A 1-D list
def flatten(l):
return [item for sublist in l for item in sublist]
# A function that computes the class weights
# The source of the function is:
# https://github.com/junwang4/causal-language-use-in-science
# Passing parameters:
# labels: A list of labels
# Return values:
# class_weight: The weights for each class
def get_class_weight(labels):
class_weight = [x for x in compute_class_weight("balanced", np.unique(labels), labels)]
print('- auto-computed class weight:', class_weight)
return class_weight
# A function the creates K separate train/test splits for
# k-fold cross validation
# Passing Parameters:
# dataset: the dataset as a pandas dataframe
# k: the number of folds
# Return Values:
# A list of k dictionaries
# with each dictionary holding the training
# and test set for that fold.
def make_K_Folds(dataset,k):
dataset_local = dataset.copy(deep=True)
folds = []
splits = []
for i in range(k,0,-1):
split = dataset_local.sample(frac=float(1/i), random_state=7)
splits.append(split)
dataset_local = dataset_local.drop(split.index)
for split in splits:
test = split.copy(deep=True)
train = dataset.drop(split.index)
folds.append({'train':train,'test':test})
return folds
cwd = os.getcwd()
# Intitialize the paths to the data folder
# and the outputs folder
data_folder = Path(cwd + "/Data/")
output_folder = Path(cwd + "/outputs/")
if not os.path.exists(output_folder):
os.mkdir(output_folder)
# Read the data
f = open(data_folder / "EntityRecognition.txt", "r")
data = f.readlines()
tokens = []
labels = []
for i in range(0, len(data) - 1, 2):
tokens.append(data[i].strip().split(" "))
labels.append(data[i + 1].strip().split(" "))
f.close()
# Convert the data to a dictionary
data = {'tokens': tokens, 'labels': labels}
# convert the tokens and labels to 1-d lists
tokens = flatten(tokens)
labels = flatten(labels)
# get ehe unique labels
label_list = np.unique(labels)
label_list = list(label_list)
# load the fastText model
fasttext.util.download_model('en', if_exists='ignore')
ft = fasttext.load_model('cc.en.300.bin')
print("Loaded FastText model.")
# convert the data to a dataframe
dataset = pd.DataFrame(data=data)
# get the train/test splits for the 5 folds
folds = make_K_Folds(dataset, 5)
# The c parameters to test
C = [5]#0.5,1,2,5,10
# the kernals to test
kernels = ['rbf']# , 'linear'
# Open the results file for the experiment
results_file = open(output_folder / ("SVM" + "_"+"ALL" + "_NER.txt"), "a")
# write the csv headers
results_file.write("Label;Accuracy;Precision;Recall;F1;Parameters\n")
# for each C value and kernel combination
for c_param in C:
for kernel in kernels:
accuracy = []
precision = []
recall = []
f1 = []
# For each train/test split in the 5 folds
for fold in folds:
train = fold['train']
test = fold['test']
# flatten the 2-D lists
X_train, y_train = flatten(train.tokens), flatten(train.labels)
X_test, y_test = flatten(test.tokens), flatten(test.labels)
# vectorize the words
X_train = [ft.get_word_vector(x).tolist() for x in X_train]
X_test = [ft.get_word_vector(x).tolist() for x in X_test]
# Initialize the SVM model
model = SVC(C=c_param, kernel=kernel,class_weight='balanced')
# Fit the model to the train data
model.fit(X_train, y_train)
# Get the metrics for the fold for all labels
y_pred = model.predict(X_test)
accuracy.append(accuracy_score(y_test,y_pred))
precision.append(precision_score(y_test,y_pred, labels=label_list, average=None))
recall.append(recall_score(y_test,y_pred,labels=label_list, average=None))
f1.append(f1_score(y_test,y_pred, labels=label_list, average=None))
mean_acc = np.mean(accuracy)
std_acc = np.std(accuracy)
# format the results and print them to the results file
parameters = {"C": c_param, "kernel": kernel}
# for each label calculate the average
# precision, recall and F1-score over the
# 5 folds along with the standard deviation
# and print them in the results file
print("Label |Accuracy | Precision | Recall | F1 | Parameters")
for i in range(len(label_list)):
precision_temp = []
recall_temp = []
f1_temp = []
for j in range(len(recall)):
precision_temp.append(precision[j][i])
recall_temp.append(recall[j][i])
f1_temp.append(f1[j][i])
mean_pre = np.mean(precision_temp)
std_pre = np.std(precision_temp)
mean_rec = np.mean(recall_temp)
std_rec = np.std(recall_temp)
mean_f1 = np.mean(f1_temp)
std_f1 = np.std(f1_temp)
results_file.write(label_list[i] + ";")
results_file.write("%0.3f (+/-%0.03f); %0.3f (+/-%0.03f); %0.3f (+/-%0.03f); %0.3f (+/-%0.03f); for %r\n"
% (mean_acc, std_acc, mean_pre, std_pre, mean_rec, std_rec
, mean_f1, std_f1, parameters))
print(str(label_list[
i]) + ": " + "%0.3f (+/-%0.03f); %0.3f (+/-%0.03f); %0.3f (+/-%0.03f); %0.3f (+/-%0.03f); for %r\n"
% (mean_acc, std_acc, mean_pre, std_pre, mean_rec, std_rec
, mean_f1, std_f1, parameters))
results_file.close()