【发布时间】:2017-05-19 14:33:31
【问题描述】:
我正在使用 k-means 对文章进行聚类,它运行良好。现在我想定义初始中心以获得更合理的结果。
我的 Python 代码:
tfidf_matrix = tfidf_vectorizer.fit_transform(articles)
X = np.array([[-19.67480000, -8.546],
[22.010807000,-10.9737],
[11.959700000,19.2701],
[12.254700000, 11.2381],
[16.649700000,-15.2251],
[19.859700000, 13.2601]] , np.float64)
km = KMeans(n_clusters=6,init=X, n_init=1).fit(tfidf_matrix)
当我尝试定义初始质心时,出现以下错误:
ValueError: The number of features of the initial centers 2 does not match the number of features of the data 4602.
从错误中我得到尺寸不相等的想法。如何转换我的初始中心以满足稀疏矩阵的维度?
【问题讨论】:
-
tfidf_matrix的形状是什么? -
每次运行都会改变。但是你可以根据最后一个来帮助我:(1111, 8262)
标签: python numpy scikit-learn k-means tf-idf