-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc2vec_3author.py
189 lines (152 loc) · 6.11 KB
/
doc2vec_3author.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
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 21 15:07:06 2018
@author: mgungor
Doc2vec for 3 author datasets
#To import xgboost properly use the following code
dir = r'C:\Program Files\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev0\mingw64\bin'
import os
os.environ['PATH'].count(dir)
os.environ['PATH'].find(dir)
os.environ['PATH'] = dir + ';' + os.environ['PATH']
"""
from gensim.models import doc2vec
from collections import namedtuple
import pandas as pd
import numpy as np
import xgboost as xgb
from tqdm import tqdm
from sklearn.metrics import f1_score, accuracy_score
import scipy.io as sc
from sklearn.linear_model import LogisticRegression
import multiprocessing
import gensim
cores = multiprocessing.cpu_count()
assert gensim.models.doc2vec.FAST_VERSION
# Load data
train = pd.read_csv('train.csv')
test = pd.read_csv('test.csv')
ytrain = sc.loadmat("ytrain.mat")["ytrain"]
test_author = sc.loadmat("yvalid.mat")["yvalid"]
ytrain = ytrain.transpose()
test_author = test_author.transpose()
# Transform data (you can add more data preprocessing steps)
train_docs = []
analyzedDocument = namedtuple('AnalyzedDocument', 'words tags')
for i, text in tqdm(enumerate(train.text.values)):
words = text.lower().split()
tags = [i]
train_docs.append(analyzedDocument(words, tags))
# Train model (set min_count = 1, if you want the model to work with the provided example data set)
model_train = doc2vec.Doc2Vec(train_docs, size = 300, window = 300, min_count = 1, workers = cores)
test_docs = []
analyzedDocument = namedtuple('AnalyzedDocument', 'words tags')
for i, text in tqdm(enumerate(test.text.values)):
words = text.lower().split()
tags = [i]
test_docs.append(analyzedDocument(words, tags))
# Train model (set min_count = 1, if you want the model to work with the provided example data set)
model_test = doc2vec.Doc2Vec(test_docs, size = 300, window = 300, min_count = 1, workers = cores)
xtrain = np.zeros((len(train.text.values), 300))
for i in range(len(train.text.values)):
xtrain[i,:] = model_train.docvecs[i]
# Get the vectors
xtest = np.zeros((len(test.text.values), 300))
for i in range(len(test.text.values)):
xtest[i,:] = model_test.docvecs[i]
# Fitting a simple xgboost
clf = xgb.XGBClassifier(nthread=10, silent=False)
clf.fit(xtrain, ytrain)
predictions = clf.predict(xtest)
print("f1 Score")
print(f1_score(test_author, predictions, average='macro'))
print("Accuracy")
print(accuracy_score(test_author, predictions))
# Fitting a simple xgboost on glove features
clf = xgb.XGBClassifier(max_depth=7, n_estimators=200, colsample_bytree=0.8,
subsample=0.8, nthread=10, learning_rate=0.1, silent=False)
clf.fit(xtrain, ytrain)
predictions = clf.predict(xtest)
print("f1 Score")
print(f1_score(test_author, predictions, average='macro'))
print("Accuracy")
print(accuracy_score(test_author, predictions))
# Fitting a simple Logistic Regression on word2vec
clf = LogisticRegression(C=1.0)
clf.fit(xtrain, ytrain)
predictions = clf.predict(xtest)
print("Logistic Regression")
print("f1 Score")
print(f1_score(test_author, predictions, average='macro'))
print("Accuracy")
print(accuracy_score(test_author, predictions))
###############################################################################
#Second method
"""
Doc2Vec function contains alpha and min_alpha parameters, but that means that
the learning rate decays during one epoch from alpha to min_alpha.
Train several epochs, set the learning rate manually
"""
import random
alpha_val = 0.025 # Initial learning rate
min_alpha_val = 1e-4 # Minimum for linear learning rate decay
passes = 15 # Number of passes of one document during training
alpha_delta = (alpha_val - min_alpha_val) / (passes - 1)
model_train = doc2vec.Doc2Vec( size = 300 # Model initialization
, window = 300
, min_count = 1
, workers = cores)
model_train.build_vocab(train_docs) # Building vocabulary
model_test = doc2vec.Doc2Vec( size = 300 # Model initialization
, window = 300
, min_count = 1
, workers = cores)
model_test.build_vocab(test_docs) # Building vocabulary
for epoch in tqdm(range(passes)):
# Shuffling gets better results
random.shuffle(train_docs)
random.shuffle(test_docs)
# Train
model_train.alpha, model_train.min_alpha = alpha_val, alpha_val
model_train.train(train_docs,total_examples=model_train.corpus_count,epochs=model_train.iter)
#Test
model_test.alpha, model_test.min_alpha = alpha_val, alpha_val
model_test.train(test_docs,total_examples=model_test.corpus_count,epochs=model_test.iter)
# Logs
print('Completed pass %i at alpha %f' % (epoch + 1, alpha_val))
# Next run alpha
alpha_val -= alpha_delta
# Get the vectors
xtrain = np.zeros((len(train.text.values), 300))
for i in range(len(train.text.values)):
xtrain[i,:] = model_train.docvecs[i]
# Get the vectors
xtest = np.zeros((len(test.text.values), 300))
for i in range(len(test.text.values)):
xtest[i,:] = model_test.docvecs[i]
# Fitting a simple xgboost on glove features
clf = xgb.XGBClassifier(nthread=10, silent=False)
clf.fit(xtrain, ytrain)
predictions = clf.predict(xtest)
print("f1 Score")
print(f1_score(test_author, predictions, average='macro'))
print("Accuracy")
print(accuracy_score(test_author, predictions))
# Fitting a simple xgboost
clf = xgb.XGBClassifier(max_depth=7, n_estimators=200, colsample_bytree=0.8,
subsample=0.8, nthread=10, learning_rate=0.1, silent=False)
clf.fit(xtrain, ytrain)
predictions = clf.predict(xtest)
print("f1 Score")
print(f1_score(test_author, predictions, average='macro'))
print("Accuracy")
print(accuracy_score(test_author, predictions))
# Fitting a simple Logistic Regression on word2vec
clf = LogisticRegression(C=1.0)
clf.fit(xtrain, ytrain)
predictions = clf.predict(xtest)
print("Logistic Regression")
print("f1 Score")
print(f1_score(test_author, predictions, average='macro'))
print("Accuracy")
print(accuracy_score(test_author, predictions))