I'm a beginner and I'm trying to do a clustering of a multi-sentence text, but my results are horrible. Any tips for me to improve my result?
import pandas
import pprint
import numpy as np
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.metrics import adjusted_rand_score
from sklearn.feature_extraction.text import TfidfVectorizer
dataset = pandas.read_csv('text.csv', encoding = 'utf-8')
comments = dataset['comments']
comments_list = comments.values.tolist()
vetorize = TfidfVectorizer()
X = vetorize.fit_transform(comments_list)
clusters_number = 6
model = KMeans(n_clusters = clusters_number, init = 'k-means++', max_iter = 300, n_init = 1)
model.fit(X)
centers = model.cluster_centers_
labels = model.labels_
clusters = {}
for verbatim, label in zip(verbatim_list, labels):
try:
clusters[str(label)].append(verbatim)
except:
clusters[str(label)] = [verbatim]
pprint.pprint(clusters)
#Top terms for cluster
print("Top termos par cluster:")
ordem_centroides = model.cluster_centers_.argsort()[:, ::-1]
termos = vetorizar.get_feature_names()
for i in range(clusters_number):
print ("Cluster %d:" % i,)
for ind in ordem_centroides[i, :10]:
print (' %s' % termos[ind],)
print()
I have many different themes present in various clusters. I pre-processed my data (stopwords, lowercase, I removed the punct...). But still I have 'like to cancel order' in one cluster and 'love cancel order' in another. When, in fact, the ideal is to join all 'cancel order' in a single cluster.
text.csv
looks like and what kind of information you expect to get from clustering? $\endgroup$6
clusters? if you choose 6 but there in truth are 2, you will see your results split. Do you have a predefined number of clusters that you need to find the items that belong to it? $\endgroup$