主题模型用于提取文本信息中的主题。是无监督学习方法。
主题模型主要用于文本聚类,用于对非结构化的本文提取信息和特征。
alpha和beta超参数 – alpha 表示文档-主题密度,beta表示主题-词密度。alpha值越大,表示文档由更多的主题构成,越小,则文档会集中于某几个主题。同理,beta值越大,构成主题的词越多,越小,则主题词越少。
主题数 - 语料中需要提取的主题数。可以使用KL散度来获取合适的主题数。
主题词数 - 如果问题陈述是关于提取主题或概念的,建议选择一个更高的数字,如果问题陈述涉及到提取特性或术语,建议使用一个较低的数字。
迭代/传递的数量——允许LDA算法收敛的最大迭代次数。
doc1 = "Sugar is bad to consume. My sister likes to have sugar, but not my father."
doc2 = "My father spends a lot of time driving my sister around to dance practice."
doc3 = "Doctors suggest that driving may cause increased stress and blood pressure."
doc4 = "Sometimes I feel pressure to perform well at school, but my father never seems to drive my sister to do better."
doc5 = "Health experts say that Sugar is not good for your lifestyle."
# compile documents
doc_complete = [doc1, doc2, doc3, doc4, doc5]
分词
from nltk.corpus import stopwords
from nltk.stem.wordnet import WordNetLemmatizer
import string
stop = set(stopwords.words('english'))
exclude = set(string.punctuation)
lemma = WordNetLemmatizer()
def clean(doc):
stop_free = " ".join([i for i in doc.lower().split() if i not in stop])
punc_free = ''.join(ch for ch in stop_free if ch not in exclude)
normalized = " ".join(lemma.lemmatize(word) for word in punc_free.split())
return normalized
doc_clean = [clean(doc).split() for doc in doc_complete]
将分词结果转化为DT矩阵(TF-IDF)
# Importing Gensim
import gensim
from gensim import corpora
# Creating the term dictionary of our courpus, where every unique term is assigned an index.
dictionary = corpora.Dictionary(doc_clean)
# Converting list of documents (corpus) into Document Term Matrix using dictionary prepared above.
#TF
doc_term_matrix = [dictionary.doc2bow(doc) for doc in doc_clean]
#TF-IDF
corpus_tfidf = models.TfidfModel(corpus)[corpus]
运行LDA模型
# Creating the object for LDA model using gensim library
Lda = gensim.models.ldamodel.LdaModel
# Running and Trainign LDA model on the document term matrix.
ldamodel = Lda(doc_term_matrix, num_topics=3, id2word = dictionary, passes=50)
结果
print(ldamodel.print_topics(num_topics=3, num_words=3))
['0.168*health + 0.083*sugar + 0.072*bad,
'0.061*consume + 0.050*drive + 0.050*sister,
'0.049*pressur + 0.049*father + 0.049*sister]
参考:https://www.analyticsvidhya.com/blog/2016/08/beginners-guide-to-topic-modeling-in-python/