import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.cluster import DBSCAN
df = pd.read_csv('text.csv')
text = df.text.values
tfidf = TfidfVectorizer(stop_words='english')
vec_fit = tfidf.fit(text)
features = vec_fit.transform(text)
# now comes the clustering part, you can use KMeans, DBSCAN at your will
model = DBSCAN().fit(features) # this might take ages as per size of the text and does not require to provide no. of clusters!!!
unseen_features = vec_fit.transform(unseen_text)
y_pred = model.predict(unseen_features)
首先,您需要使用tfidf或word2vec等对文本进行矢量化。请参阅下面的tfidf实现: 我跳过了预处理部分,因为它会根据问题陈述的不同而有所不同
sklean文档中提供了集群评估技术: https://scikit-learn.org/stable/modules/clustering.html#clustering-performance-evaluation
备选方案之一可以是主题建模,例如潜在Dirichlet分配(LDA)模型
最小的
R
示例如下所示:Craigslist数据集的好消息是,它为每个职位都有标签(类别),所以你可以构建一种混乱矩阵,如下所示:
当然,LDA是无监督的,估计的主题不应该与原始类别匹配,但我们观察到,例如
labor
类别和topic_2
之间存在语义交叉您可以使用诸如
gensim.models.word2vec
之类的单词级嵌入对标题进行特征化,然后使用sklearn.cluster.DBSCAN
。如果不查看数据集,很难给出更具体的建议相关问题 更多 >
编程相关推荐