-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocLinearR.py
More file actions
153 lines (125 loc) · 4.77 KB
/
Copy pathdocLinearR.py
File metadata and controls
153 lines (125 loc) · 4.77 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
# Authors Alexey Titov and Shir Bentabou
# Version 1.0
# Date 05.2019
# libraries
import gensim
from gensim.test.test_doc2vec import ConcatenatedDoc2Vec
from gensim.models.doc2vec import TaggedDocument
from gensim.models import Doc2Vec
import multiprocessing
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import nltk
from bs4 import BeautifulSoup
import matplotlib.pyplot as plt
import seaborn as sns
import re
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.model_selection import train_test_split
from sklearn import utils
from sklearn.pipeline import Pipeline
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
import csv
import numpy as np
import os
import tempfile
import sys
from importlib import reload
import pandas as pd
from tqdm import tqdm
tqdm.pandas(desc="progress-bar")
# Do this in a separate python interpreter session, since you only have to do it once
# nltk.download('punkt')
# Do this in your ipython notebook or analysis script
cores = multiprocessing.cpu_count()
# fix UnicodeEncodeError
if sys.version[0] == '2':
reload(sys)
sys.setdefaultencoding("utf-8")
# get vector
def get_vectors(model, tagged_docs):
sents = tagged_docs.values
targets, regressors = zip(
*[(doc.tags[0], model.infer_vector(doc.words, steps=20)) for doc in sents])
return targets, regressors
# create vector
def vec_for_learning(model, tagged_docs):
sents = tagged_docs.values
targets, regressors = zip(
*[(doc.tags[0], model.infer_vector(doc.words, steps=20)) for doc in sents])
return targets, regressors
# tokenizer for text
def tokenize_text(text):
tokens = []
for sent in nltk.sent_tokenize(text):
for word in nltk.word_tokenize(sent):
if (len(word) < 2):
continue
tokens.append(word.lower())
return tokens
# this function clean text from no wanted symbols
def cleanText(text):
text = BeautifulSoup(text, "lxml").text
text = re.sub(r'\|\|\|', r' ', text)
text = re.sub(r'http\S+', r'<URL>', text)
text = text.lower()
text = text.replace('x', '')
return text
if __name__ == "__main__":
print("[*] Start read from CSV file")
df = pd.read_csv('pdfFiles_linear.csv')
df = df[['Text', 'Kind']]
df = df[pd.notnull(df['Text'])]
df.rename(columns={'Text': 'Text'}, inplace=True)
df.shape
df.index = range(502)
df['Text'] = df['Text'].apply(cleanText)
train, test = train_test_split(df, test_size=0.25, random_state=42)
train_tagged = train.apply(lambda r: TaggedDocument(
words=tokenize_text(r['Text']), tags=[r.Kind]), axis=1)
test_tagged = test.apply(lambda r: TaggedDocument(
words=tokenize_text(r['Text']), tags=[r.Kind]), axis=1)
train_tagged.values[30]
print("[*] Stop read from CSV file")
# DBOW
model_dbow = Doc2Vec(dm=0, vector_size=300, negative=5,
hs=0, min_count=2, sample=0, workers=cores)
model_dbow.build_vocab([x for x in tqdm(train_tagged.values)])
for epoch in range(30):
model_dbow.train(utils.shuffle([x for x in tqdm(
train_tagged.values)]), total_examples=len(train_tagged.values), epochs=1)
model_dbow.alpha -= 0.002
model_dbow.min_alpha = model_dbow.alpha
# Distributed Memory with Averaging
model_dmm = Doc2Vec(dm=1, dm_mean=1, vector_size=300, window=10,
negative=5, min_count=1, workers=5, alpha=0.065, min_alpha=0.065)
model_dmm.build_vocab([x for x in tqdm(train_tagged.values)])
for epoch in range(30):
model_dmm.train(utils.shuffle([x for x in tqdm(
train_tagged.values)]), total_examples=len(train_tagged.values), epochs=1)
model_dmm.alpha -= 0.002
model_dmm.min_alpha = model_dmm.alpha
# Union
print("Union")
model_dbow.delete_temporary_training_data(
keep_doctags_vectors=True, keep_inference=True)
model_dmm.delete_temporary_training_data(
keep_doctags_vectors=True, keep_inference=True)
new_model = ConcatenatedDoc2Vec([model_dbow, model_dmm])
y_train, X_train = get_vectors(new_model, train_tagged)
y_test, X_test = get_vectors(new_model, test_tagged)
# Linear Regression
# create linear regression object
linreg = Pipeline([('clf', linear_model.LinearRegression()),])
# train the model using the training sets
linreg.fit(X_train, y_train)
# variance score: 1 means perfect prediction
print('Variance score: {}'.format(linreg.score(X_test, y_test)))
# Make predictions using the testing set
diabetes_y_pred = linreg.predict(X_test)
# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % r2_score(y_test, diabetes_y_pred))