如何对 r 中充满字符串变量的数据集进行 K 均值聚类

How to do K-means clustering on a dataset full of string variables in r

现在我有一个充满字符串变量的数据集,但我想在其上做一个聚类项目。我对所有变量应用 as.factor() 后, nbclust() 仍然无法工作,我该怎么办?

K-means 通常使用欧氏距离(参见 https://stats.stackexchange.com/questions/81481/why-does-k-means-clustering-algorithm-use-only-euclidean-distance-metric),因此您不能直接 "cluster on words".

如果您想根据单词对观察结果进行聚类,则必须生成数字(例如 ) For example if you were trying to cluster customer profiles to do segmentation, you could count up words representing their interests in their profiles, and then have one column per interest, and count the number of times that word or n-gram appeared in the profile, then cluster on that matrix of numbers. Or in clustering documents, generate a term-document matrix (or document-term matrix, or term-term occurrence like k-means clustering on term-term co-ocurrence matrix)并使用这些数字进行聚类。

不要对此类数据使用 k-means。

您无法仅通过 "trial and error" 获得有意义的统计分析。因为有很多方法可以得到一个看起来不错但完全没有根据的结果。

在使用这些方法中的任何一种之前,您需要了解它的作用。在 k-means 的情况下,它最小化最小二乘法,这显然只对 continuous 变量有意义。他们还需要线性表现。如果您有多个变量,它们也需要具有相同的大小。

这不是黑盒方法。用的不好,就是垃圾。