-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
168 lines (128 loc) · 4.27 KB
/
main.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
import random as rd
import re
import math
import matplotlib.pyplot as plt
def pre_process_tweets(url):
f = open(url)
all_tweets = list(f)
list_of_tweets = []
for i in range(len(all_tweets)):
all_tweets[i] = re.sub(r'@\S*.', "", all_tweets[i])
all_tweets[i] = re.sub(r'#', "", all_tweets[i])
all_tweets[i] = re.sub(r'www\S*.', "", all_tweets[i])
all_tweets[i] = re.sub(r'http\S*.', "", all_tweets[i])
all_tweets[i] = all_tweets[i][50:]
all_tweets[i] = all_tweets[i].lower()
f.close()
return all_tweets
"""***** jaccard ******"""
def jaccard(tweet1, tweet2):
union = set().union(tweet1, tweet2)
intersection = set(tweet1).intersection(tweet2)
dis = 1 - (len(intersection) / len(union))
return dis
""" **** kmeans ***** """
def k_means(k, max_itiration, tweets):
prv_centroid = []
itiration = 0
centroids = []
count = 0
chek_random = []
while count < k:
random_tweet_idx = rd.randint(0, len(tweets) - 1)
if random_tweet_idx not in chek_random:
count += 1
chek_random.append(random_tweet_idx)
centroids.append(tweets[random_tweet_idx])
while itiration < max_itiration:
if convarge(prv_centroid, centroids) == True:
break
clusters = assign(centroids, tweets)
prv_centroid = centroids
centroids = update(clusters)
"""SSE"""
sse = 0
for i in range(len(clusters)):
for j in range(len(clusters[i])): #num of tweets of cluster
sse = sse + (clusters[i][j][1] * clusters[i][j][1])
itiration += 1
return sse, clusters
"""***cluster**"""
def assign(centroids, listoftweets):
clusters=dict()
indx =60
for i in range(len(listoftweets)):
min_dis=math.inf
cluster_indx=-1
for j in range(len(centroids)):
dis=jaccard(centroids[j],listoftweets[i])
if dis<min_dis:
min_dis=dis
cluster_indx=j
if min_dis==1:
cluster_indx=indx
indx+=80
clusters.setdefault(cluster_indx,[]).append([listoftweets[i]])
#sse
last_tweet_idx = len(clusters.setdefault(cluster_indx, [])) - 1
clusters.setdefault(cluster_indx, [])[last_tweet_idx].append(min_dis)
return clusters
"""** mean**"""
def update(clusters):
new_centeroid = []
for i in range(len(clusters)):
redandant_dis = []
min_dis = math.inf
indx = -1
for j in range(len(clusters[i])):
redandant_dis.append([])
dis_sum = 0
for q in range(len(clusters[i])):
if q < j:
dis = redandant_dis[q][j]
elif q == j:
dis = 0
else:
dis = jaccard(clusters[i][j][0], clusters[i][q][0])
redandant_dis[j].append(dis)
dis_sum += dis
if dis_sum < min_dis:
min_dis = dis_sum
indx = j
new_centeroid.append(clusters[i][indx][0])
return new_centeroid
"""* convarge ***"""
def convarge(prv_centroid,new_centroid ):
if len(prv_centroid)!=len(new_centroid):
return False
for i in range(len(new_centroid)):
if new_centroid[i]!=prv_centroid[i]:
return False
return True
""" main """
if __name__ == '__main__':
data_url = 'Health-Tweets/bbchealth.txt'
tweets = pre_process_tweets(data_url)
# default number of experiments to be performed
experiments = 5
# default value of K for K-means
k = 3
sse_list = []
k_list =[]
# for every experiment 'e', run K-means
for e in range(experiments):
print("------ Running K means for experiment no. " + str((e + 1)) + " for k = " + str(k))
sse1,clusters = k_means(k,5,tweets)
#for every cluster 'c', print size of each cluster
for c in range(len(clusters)):
print(str(c + 1) + ": ", str(len(clusters[c])) + " tweets")
sse_list.append(sse1)
print("--> SSE : " + str(sse1))
print('\n')
k_list.append(k)
# increment k after every experiment
k = k + 1
plt.xlabel("k")
plt.ylabel("SSE")
plt.plot(k_list, sse_list)
plt.show()