python - 使用 countvectorizer 和 tfidfvectorizer 作为特征向量使用 KMeans 进行文本聚类是否有意义?

标签 python machine-learning scipy scikit-learn feature-extraction

我正在尝试从包含大约 1000 条评论的 csv 文件构建特征向量。我的特征向量之一是使用 scikit learn 的 tfidf vectorizer 的 tfidf。将计数也用作特征向量是否有意义,或者我应该使用更好的特征向量吗?

如果我最终同时使用 Countvectorizer 和 tfidfvectorizer 作为我的特征,我应该如何将它们都放入我的 Kmeans 模型(特别是 km.fit() 部分)?现在我只能将 tfidf 特征向量拟合到模型中。

这是我的代码:

vectorizer=TfidfVectorizer(min_df=1, max_df=0.9, stop_words='english', decode_error='ignore')
vectorized=vectorizer.fit_transform(sentence_list)

#count_vectorizer=CountVectorizer(min_df=1, max_df=0.9, stop_words='english', decode_error='ignore')
#count_vectorized=count_vectorizerfit_transform(sentence_list)

km=KMeans(n_clusters=num_clusters, init='k-means++',n_init=10, verbose=1)
km.fit(vectorized)

最佳答案

本质上,您所做的是找到文本文档的数字表示(特征工程)。在某些问题中,计数效果更好,而在其他一些问题中,tfidf 表示是最佳选择。你真的应该尝试他们两个。虽然这两种表示非常相似,因此携带的信息大致相同,但使用完整的特征集 (tfidf+counts) 可能会获得更好的精度。通过在这个特征空间中搜索,可能会更接近真实模型。

这是水平堆叠特征的方式:

import scipy.sparse

X = scipy.sparse.hstack([vectorized, count_vectorized])

然后你可以这样做:

model.fit(X, y)  # y is optional in some models

关于python - 使用 countvectorizer 和 tfidfvectorizer 作为特征向量使用 KMeans 进行文本聚类是否有意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27496014/

相关文章:

python - 递归神经网络中的时期与迭代

python - 给定一个词,我们可以使用 Spacy 获得所有可能的引理吗?

python - 以 3D 张量时间序列作为输入的二元分类 Keras 神经网络模型

python - python 中两个路由字符串相互附加

python - 如何计算聚类的基尼系数

python - 如何计算(正态)分布上的点的 p 值?

python - SciPy 插值 ValueError : x and y arrays must be equal in length along interpolation axis

image - 使用 openCV 处理 matplotlib.figure.Figure

python - 如何创建任意长度字符串的numpy数组?

Python 重复循环文件以将记录与用户输入进行匹配