Hi dears I have a problem: i have to do a tweets clustering, but i don't know how i could do it in the right way. My scope is to do a sentiment analysis of the tweets on the back of the hashtag used (if you have a suggestion to improve my work i'll be very happy you will share it with me, thank you). I did all preprocessing work on test, in order to have a clean text to work with. So i extracted the text of my tweets and i trained a word2vec model on them in order to have a vector representation of each word because I would to do a K-Means clustering. Then I asked for the normed vectors obtained from the model of the 1000 most common hashtags, which could give me a label for the tweets. Here is my problem: if I want to give a label to each tweet from the labelled hashtag, how can i do it? Or what I could to to label every tweet from a given hashtag? I post below the code used starting from the word2vec model:
text= df["token_text"]
model = gensim.models.Word2Vec(
window=3, # they are tweet, so I choose a window of 3 elements
min_count=5, #min number of token word in each tweet
workers = 5, #cpu
)
model.build_vocab(text)
model.train(text, total_examples= model.corpus_count, epochs=30)
Then in an other page:
model= gensim.models.Word2Vec.load("../../2/Modelli/model.bin")#model trained by me
corpus= df.hashtag
model.build_vocab(corpus, update=True)
model.train(corpus, total_examples= model.corpus_count, epochs=30)
word_counts = Counter(itertools.chain.from_iterable(corpus))
words_by_freq = (k for k, v in word_counts.most_common()[0:1000])
word_vectors = model.wv.vectors_for_all(words_by_freq)
normed_vectors=word_vectors.get_normed_vectors()
k=5
km_model = KMeans(
n_clusters=k,
init='k-means++',
max_iter=35,
n_init=30,
verbose=True
)
groups = km_model.fit_predict(normed_vectors)
ordered_centroids = km_model.cluster_centers_.argsort()[:, ::-1]
tags = word_vectors.index_to_key
TAGS_IN_CATEGORY= 5
for idx, centroids in enumerate(ordered_centroids):
print("Centroid %s:" % idx)
for centroid_tag in centroids[:TAGS_IN_CATEGORY]:
print("#%s" % tags[centroid_tag])
Finally I try do a PCA with the clusters. I don't know if all is the right way and I don't how now I can give a label to each tweet based on the labeled hashtag. I'll appreciate every suggestions. Thank you for the attention and patience.