假如我有100篇文章,每篇文章形如:
本发明公开的一种冰箱用的三风门,其包括:一风门本体,所述风门本体上设置有第一风口、第二风口和第三风口;安装在所述风门本体上的电动机构;分别设置在所述第一风口、第二风口和第三风口上的第一风门、第二风门、第三风门,所述第一风门、第二风门、第三风门由所述电动机构驱动按照一定的程序开启和关闭第一风口、第二风口和第三风口。本发明的冰箱用的三风门为整体结构、依靠一套传动机构来驱动三个风门启闭,其能够简化冰箱的结构,降低冰箱的成本。
是一个字符串。
那么我想要使用gensim构造基于TF-IDF的LDA模型模型,首先要构造特征。
思路:构造词袋模型->构造TF-IDF模型->构造LDA模型。
而gensim构造词袋模型的输入必须先对文章进行分词。
1.1 分词
一般使用jieba.lcut(string)
如果有stopwords可以这样
[k for k in jieba.lcut(string,cut_all=False) if k not in stop]
结果是这样:
['本发明', '公开', '一种', '冰箱', '风门', '包括', '风门', '本体', '所述', '风门', '本体', '设置', '第一', '风口', '风口', '第三', '风口', '安装', '所述', '风门', '本体', '电动', '机构', '设置', '所述', '第一', '风口', '风口', '第三', '风口', '第一', '风门', '风门', '第三', '风门', '所述', '第一', '风门', '风门', '第三', '风门', '所述', '电动', '机构', '驱动', '程序', '开启', '关闭', '第一', '风口', '风口', '第三', '风口', '本发明', '冰箱', '风门', '整体', '结构', '一套', '传动', '机构', '驱动', '三个', '风门', '启闭', '简化', '冰箱', '结构', '降低', '冰箱', '成本']
1.2 清洗
如果只想保留汉字,使用:
[i for i in string_list if re.match(r”[\u4e00-\u9fa5]”, i)]
现在已经有一个100string_list的数据。
分别放着100篇文章分词清洗后的词。
假设名为 string_list100*
2.1 词典创建
import gensim.corpora as corpora
id2word = corpora.Dictionary(string_list100)
id2word.filter_extremes(no_below=3, no_above=0.5, keep_n=3000)
id2word.save_as_text("dictionary")
2.2 corpus创建
corpus = [id2word.doc2bow(text) for text in string_list100]
print(corpus[:1])
print([[(id2word[id], freq) for id, freq in cp] for cp in corpus[:1]])
输出形如:
[(0, 2), (1, 2)]
[('一侧', 2), ('一端', 2)]
2.3 词袋转为TF-IDF
from gensim import models
tfidf_model = models.TfidfModel(corpus=corpus, dictionary=id2word)
tfidf_model.save('test_tfidf.model')
tfidf_model = models.TfidfModel.load('test_tfidf.model')
corpus_tfidf = [tfidf_model[doc] for doc in corpus]
print(corpus_tfidf[:1])
print([[(id2word[id], freq) for id, freq in cp] for cp in corpus_tfidf[:1]])
输出形如:
[(0, 0.010196106762123805), (1, 0.0094972374458957)]
[('一侧', 0.010196106762123805), ('一端', 0.0094972374458957)]
lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus_tfidf,
id2word=id2word,
num_topics=20,
random_state=100,
update_every=1,
chunksize=100,
passes=10,
alpha='auto',
per_word_topics=True)
3.1 结果查看
pprint 让输出更美观,num_words指的是每一个主题输出前多少的词。
from pprint import pprint
pprint(lda_model.print_topics(num_words=50))
3.2 评估指标
coherence_model_lda = CoherenceModel(model=lda_model, texts=string_list100, dictionary=id2word, coherence='c_v')
coherence_lda = coherence_model_lda.get_coherence()
print('\nCoherence Score: ', coherence_lda)
4.1 构造特征
def get_features(corpus_one):
new_feature=[0 for k in range(3000)]
for j in corpus_one:
new_feature[j[0]]=j[1]
return new_feature
因为gensim得到的词袋模型以及tf-idf特征是不完整的,需要使用以上函数把每一篇文章的词袋数据转化为3000维的特征。
维度说明:
corpus = corpus_tfidf: 100 * 若干(每篇文章不一样)
对corpus_tfidf每一项get_features得到all_features: 100 * 3000
4.2 KMeans
from sklearn.cluster import KMeans
from sklearn import metrics
y_pred = KMeans(n_clusters=20, random_state=9).fit(all_features)
n_clusters表示分20类
print(y_pred.labels_)
print(y_pred.cluster_centers_)
print(y_pred.predict(new_feature))
4.3 评估指标
如果有分类的标注数据label
print(metrics.adjusted_rand_score(label, y_pred.labels_))
print(metrics.adjusted_mutual_info_score(label, y_pred.labels_))
以上是常用的KMeans评估方法。
参考文章1:文本聚类(一)—— LDA 主题模型
https://blog.csdn.net/weixin_37179744/article/details/108694415#12__94
参考文章2:用scikit-learn学习K-Means聚类
www.cnblogs.com/pinard/p/6169370.html
参考文章3:分别使用sklearn和gensim提取文本的tfidf特征www.jianshu.com/p/c7e2771eccaa
Original: https://blog.csdn.net/weixin_43499457/article/details/121087816
Author: 持续战斗状态
Title: gensim实现TF-IDF和LDA模型、sklearn实现聚类
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/550072/
转载文章受原作者版权保护。转载请注明原作者出处!