【问题标题】:Visualise clusters with k-means使用 k-means 可视化集群
【发布时间】:2020-10-24 21:58:59
【问题描述】:

我有以下数据集:

    Date    Text
0   05/26/2020  è morto all'improvviso jk, aveva...
1   05/26/2020  è morto a 51 anni jk, attore, co...
2   05/26/2020  aveva 51 anni e si trovava in Italia. il rico...
3   05/26/2020  arriva a milano nel 1990 per una serie di conc...
4   05/26/2020  jk, l'attore e comico, e...
5   05/26/2020  spettacolo.it ha appreso che jk, l'...
6   05/26/2020  e' morto all'improvviso jk. cant...
7   05/26/2020  addio a jk . una morte improvvis...
8   05/26/2020  lutto nel mondo della televisione. è morto a 5...
9   05/26/2020  è morto all'età di 51 anni ...
10  05/26/2020  è morto all'età di 51 anni ...
11  05/26/2020  all'improvviso se ne è andato  ...
12  05/26/2020  è andato al supermercato  ...
13  05/26/2020  jk è morto improvvisamente a 51 ...
14  05/26/2020  è morto, a menfi, il 51enne jk...
15  05/26/2020  muore a cinquantuno anni jk, il ...

我想使用聚类(k-mean)来创建标签来对文本进行分类。 我做了如下:

import re
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.cluster import KMeans
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
import nltk
from nltk.corpus import stopwords

stop_words = stopwords.words('italian')

def preprocessing(line):
    line = re.sub(r"[^a-zA-Z]", " ", line.lower())
    words = word_tokenize(line)
    words_lemmed = [WordNetLemmatizer().lemmatize(w) for w in words if w not in stop_words]
    return words_lemmed


vect =TfidfVectorizer(tokenizer=preprocessing)
vectorized_text=vect.fit_transform(df['Text'])
kmeans =KMeans(n_clusters=2).fit(vectorized_text)

然后

import string as st 
from pandas import Series, DataFrame

cl=kmeans.predict(vectorized_text)
df['Cluster']=pd.Series(cl, index=df.index)
df.groupby("Cluster").count()

我想知道如何可视化结果。 我试过如下:

plt.scatter(vectorized_text, cl)
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=300, c='red')
plt.show()

但我有这个错误:

ValueError: x 和 y 的大小必须相同

由于plt.scatter(vectorized_text, cl),所以那里出了点问题。在网上查看可能的解决方案时,我通过使用 PCA 找到了一些东西。我应该考虑吗?

谢谢

更新:收到下面的答案后,我尝试了:

plt.scatter(vectorized_text[:, 0] ,cl)
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1], s=300, c='red')
plt.show()

很遗憾,我仍然收到错误消息:

ValueError: x 和 y 的大小必须相同

【问题讨论】:

  • vectorized_textcl的形状是什么?
  • @Hugolmn:为vectorized_text<209x1245 sparse matrix of type '<class 'numpy.float64'>' with 4718 stored elements in Compressed Sparse Row format>
  • 如何查看 cl 的形状?
  • cl.shape 或 len(cl) 应该可以工作
  • 谢谢。所以cl的形状是(209,)

标签: python pandas cluster-analysis k-means


【解决方案1】:

plt.scatter() 中的 x 参数的形状必须具有维度 (n,),但这里不是这种情况。您只能为散点图选择一列vectorized_text,而不是全部。现在你 x 的尺寸是 209x1245,你的 ydimensions 是 (209,)

如何将vectorized_text转换为一维数组?

剧透:你不能! 您首先需要从中切出一列,然后将其转换为密集矩阵(现在它是稀疏矩阵),然后将其转换为数组。

假设您要绘制从vectorized_text 开始的第一列:您需要提供的xplt.scatterplot 是:

np.asarray(vectorized_text[:, 0].todense())

【讨论】:

  • 感谢您的回答 Hugolmn。我很抱歉,但我没有完全得到你的建议。我应该绘制 x,y,但正确的形状是不同的,因为 x 是矩阵而 y 是数组。
  • y的维度是cl:(209,)的维度。您的 x 维度(vectorized_text) 需要与 y 的维度匹配,因此它也必须是 (209,)。您只需为散点图保留 vectorized_text 的一列
  • 换句话说,您不能使用稀疏矩阵 (vectorized_text) 作为 x 参数,除非您将其切片以仅保留一列。
  • 非常感谢您的澄清。我怎么能选择我的 vectorised_text 的第一个元素(我想)?
  • vectorized_text[:, 0] 应该允许您保留每行的第一列
猜你喜欢
  • 2016-12-15
  • 1970-01-01
  • 2015-07-20
  • 2012-11-01
  • 2012-11-06
  • 2018-10-04
  • 1970-01-01
  • 2012-07-15
  • 2019-12-18
相关资源
最近更新 更多