-
Notifications
You must be signed in to change notification settings - Fork 1
/
Clustering_BOW_EM.py
275 lines (232 loc) · 8.45 KB
/
Clustering_BOW_EM.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 27 17:34:06 2019
@author: Preeti
"""
#importing libraries
import nltk
import re
from urllib import request
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
stemmer = PorterStemmer()
stop_words = set(stopwords.words('english'))
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
#Data preparation and preprocess
def custom_preprocessor(text):
text = re.sub(r'\W+|\d+|_', ' ', text) #removing numbers and punctuations
text = re.sub(r'\s+',' ',text) #remove multiple spaces into a single space
text = re.sub(r"\s+[a-zA-Z]\s+",' ',text) #remove a single character
text = text.lower()
text = nltk.word_tokenize(text) #tokenizing
text = [word for word in text if not word in stop_words] #English Stopwords
#text = [lemmatizer.lemmatize(word) for word in text] #Lemmatising
return text
filepath_dict = {'Book1': 'https://www.gutenberg.org/files/58764/58764-0.txt',
'Book2': 'https://www.gutenberg.org/files/58751/58751-0.txt',
'Book3': 'http://www.gutenberg.org/cache/epub/345/pg345.txt'}
for key, value in filepath_dict.items():
if (key == "Book1"):
bookLoc = filepath_dict[key]
response = request.urlopen(bookLoc)
raw = response.read().decode('utf-8')
len(raw)
first_book = custom_preprocessor(raw)
elif (key == "Book2"):
bookLoc = filepath_dict[key]
response = request.urlopen(bookLoc)
raw = response.read().decode('utf-8')
len(raw)
second_book = custom_preprocessor(raw)
elif (key == "Book3"):
bookLoc = filepath_dict[key]
response = request.urlopen(bookLoc)
raw = response.read().decode('utf-8')
len(raw)
third_book = custom_preprocessor(raw)
else:
pass
#Building First Book
first_book_text = ' '.join(first_book)
fileLoc = '/Users/sfuhaid/Desktop/EBC7100Assign2-Group7/firstbook/a.txt'
with open(fileLoc, 'a') as fout:
fout.write(first_book_text)
fout.close()
#Building Second Book
second_book_text = ' '.join(second_book)
fileLoc = '/Users/sfuhaid/Desktop/EBC7100Assign2-Group7/secondbook/b.txt'
with open(fileLoc, 'a') as fout:
fout.write(second_book_text)
fout.close()
#Building Third Book
third_book_text = ' '.join(third_book)
fileLoc = '/Users/sfuhaid/Desktop/EBC7100Assign2-Group7/thirdbook/c.txt'
with open(fileLoc, 'a') as fout:
fout.write(third_book_text)
fout.close()
# labeling
# Cretaing tuple
# aBooklist = []
def readAtxtfile(bookText, docs, labels):
x = 0
i = 0
n = 150
while x < 200:
temp = ""
words = bookText.split(" ")[i:n]
for word in words:
temp = word + " " + temp
docs.append(temp)
labels.append(0)
i += 150
n += 150
x += 1
return docs, labels
# Cretaing tuple
# bBooklist = []
def readBtxtfile(bookText, docs, labels):
x = 0
i = 0
n = 150
while x < 184:
temp = ""
words = bookText.split(" ")[i:n]
for word in words:
temp = word + " " + temp
docs.append(temp)
labels.append(1)
i += 150
n += 150
x += 1
return docs, labels
# Cretaing tuple
# cBooklist = []
def readCtxtfile(bookText, docs, labels):
x = 0
i = 0
n = 150
while x < 200:
temp = ""
words = bookText.split(" ")[i:n]
for word in words:
temp = word + " " + temp
docs.append(temp)
labels.append(2)
i += 150
n += 150
x += 1
return docs, labels
docs = []
labels = []
docs, labels = readAtxtfile(first_book_text, docs, labels)
# print(aBooklist)
docs, labels = readBtxtfile(second_book_text, docs, labels)
# print(bBooklist)
docs, labels = readCtxtfile(third_book_text, docs, labels)
# print(cBooklist)
#print(len(docs))
#print(docs)
#print(labels)
#print(len(labels))
#***********************collocation********************************************
import nltk
from nltk.collocations import *
bigram_measures = nltk.collocations.BigramAssocMeasures()
trigram_measures = nltk.collocations.TrigramAssocMeasures()
# change this to read in your data
finder = BigramCollocationFinder.from_words(first_book+second_book+third_book)
# only bigrams that appear 3+ times
finder.apply_freq_filter(3)
# return the 10 n-grams with the highest PMI
book_collocation = finder.nbest(bigram_measures.pmi, 10)
#print('collocation : ',book_collocation)
# Data transformation BOW
# Creating the BOW model
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer(max_features=2000, min_df=3, max_df=0.6)
X = vectorizer.fit_transform(docs)
X.toarray()
#**********************Expectation Maximization********************************
def bow_EM(X):
from sklearn.mixture import GaussianMixture
EM_X = X.toarray()
gmm = GaussianMixture(n_components=3, random_state=0)
gmm = gmm.fit(EM_X)
EM_labels = gmm.predict(EM_X)
return EM_labels
#*****************************calculation**************************************
from scipy.stats import spearmanr
from time import time
from sklearn import metrics
name = 'EM-BOW'
t0 = time()
EM_label = bow_EM(X)
print(82 * '_')
print('init\t\ttime\thomo\tcompl\tv-meas\tARI\tAMI\tkappa\tcorr\tsilh_Clus\tsilh_HMN')
print('%-9s\t%.2fs\t%i\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%-9s\t%.3f\t%.3f'
% (name, (time() - t0),
metrics.homogeneity_score(labels, EM_label),
metrics.completeness_score(labels, EM_label),
metrics.v_measure_score(labels, EM_label),
metrics.adjusted_rand_score(labels, EM_label),
metrics.adjusted_mutual_info_score(labels, EM_label),
metrics.cohen_kappa_score(labels, EM_label,weights='linear'),
str(spearmanr(labels,EM_label)),
metrics.silhouette_score(X, EM_label,
metric='euclidean'),
metrics.silhouette_score(X, labels,
metric='euclidean'),
))
#**************************error analysis**************************************
from sklearn.metrics.cluster import contingency_matrix
x = labels #actual labels
y = EM_label #predicted labels
error_analysis = contingency_matrix(x, y)
#***************************plot***********************************************
from sklearn.metrics.pairwise import cosine_similarity
dist = 1 - cosine_similarity(X)
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.manifold import MDS
MDS()
# convert two components as we're plotting points in a two-dimensional plane
# "precomputed" because we provide a distance matrix
# we will also specify `random_state` so the plot is reproducible.
mds = MDS(n_components=2, dissimilarity="precomputed", random_state=1)
pos = mds.fit_transform(dist) # shape (n_components, n_samples)
xs, ys = pos[:, 0], pos[:, 1]
#set up colors per clusters using a dict
cluster_colors = {0: '#1b9e77', 1: '#d95f02', 2: '#7570b3'}
#set up cluster names using a dict
cluster_names = {0: 'first book',
1: 'second book',
2: 'third book'}
#create data frame that has the result of the MDS plus the cluster numbers and titles
df = pd.DataFrame(dict(x=xs, y=ys, label=EM_label))
#group by cluster
groups = df.groupby('label')
# set up plot
fig, ax = plt.subplots(figsize=(8, 5)) # set size
ax.margins(0.05) # Optional, just adds 5% padding to the autoscaling
#iterate through groups to layer the plot
for name, group in groups:
ax.plot(group.x, group.y, marker='o', linestyle='', ms=12,
label=cluster_names[name], color=cluster_colors[name],
mec='none')
ax.set_aspect('auto')
ax.tick_params(\
axis= 'x', # changes apply to the x-axis
which='both', # both major and minor ticks are affected
bottom='off', # ticks along the bottom edge are off
top='off', # ticks along the top edge are off
labelbottom='off')
ax.tick_params(\
axis= 'y', # changes apply to the y-axis
which='both', # both major and minor ticks are affected
left='off', # ticks along the bottom edge are off
top='off', # ticks along the top edge are off
labelleft='off')
ax.legend(numpoints=1) #show legend with only 1 point
plt.show() #show the plot