主題模型用于提取文本信息中的主題。是無監(jiān)督學(xué)習(xí)方法淑趾。
主題模型主要用于文本聚類呻澜,用于對非結(jié)構(gòu)化的本文提取信息和特征据忘。
alpha和beta超參數(shù) – alpha 表示文檔-主題密度,beta表示主題-詞密度讹语。alpha值越大钙皮,表示文檔由更多的主題構(gòu)成,越小顽决,則文檔會集中于某幾個主題短条。同理,beta值越大才菠,構(gòu)成主題的詞越多茸时,越小,則主題詞越少赋访。
主題數(shù) - 語料中需要提取的主題數(shù)可都』捍可以使用KL散度來獲取合適的主題數(shù)。
主題詞數(shù) - 如果問題陳述是關(guān)于提取主題或概念的渠牲,建議選擇一個更高的數(shù)字旋炒,如果問題陳述涉及到提取特性或術(shù)語,建議使用一個較低的數(shù)字签杈。
迭代/傳遞的數(shù)量——允許LDA算法收斂的最大迭代次數(shù)瘫镇。
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]
將分詞結(jié)果轉(zhuǎn)化為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)
結(jié)果
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/