{"id":179781,"date":"2024-05-08T20:21:37","date_gmt":"2024-05-08T12:21:37","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/179781.html"},"modified":"2024-05-08T20:21:43","modified_gmt":"2024-05-08T12:21:43","slug":"%e5%88%a9%e7%94%a8python%e5%81%9alda%e6%96%87%e6%9c%ac%e5%88%86%e6%9e%90%ef%bc%8c%e8%af%a5%e4%bb%8e%e5%93%aa%e9%87%8c%e5%85%a5%e6%89%8b%e5%91%a2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/179781.html","title":{"rendered":"\u5229\u7528python\u505aLDA\u6587\u672c\u5206\u6790\uff0c\u8be5\u4ece\u54ea\u91cc\u5165\u624b\u5462"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27064630\/5d778509-9946-4b19-a806-b650c7cebd34.webp\" alt=\"\u5229\u7528python\u505aLDA\u6587\u672c\u5206\u6790\uff0c\u8be5\u4ece\u54ea\u91cc\u5165\u624b\u5462\" \/><\/p>\n<p><p><strong>\u5229\u7528Python\u8fdb\u884cLDA\u6587\u672c\u5206\u6790\uff0c\u5e94\u4ece\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u5f00\u59cb\u3001\u7406\u89e3LDA\u7684\u5de5\u4f5c\u539f\u7406\u3001\u51c6\u5907\u6587\u672c\u6570\u636e\u3001\u6e05\u6d17\u6570\u636e\u3001\u521b\u5efa\u8bcd\u888b\u548cTF-IDF\u6a21\u578b\u3001\u8bad\u7ec3LDA\u6a21\u578b\u4ee5\u53ca\u8bc4\u4f30\u6a21\u578b\u8868\u73b0\u5e76\u53ef\u89c6\u5316\u7ed3\u679c\u3002<\/strong> \u5728\u8fd9\u4e9b\u6b65\u9aa4\u4e2d\uff0c\u7406\u89e3LDA\u7684\u5de5\u4f5c\u539f\u7406\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002LDA\uff0c\u6216\u79f0\u9690\u542b\u72c4\u5229\u514b\u96f7\u5206\u914d\u6a21\u578b\uff0c\u662f\u4e00\u79cd\u6587\u6863\u4e3b\u9898\u751f\u6210\u6a21\u578b\uff0c\u5b83\u5047\u8bbe\u6587\u6863\u5185\u7684\u6bcf\u4e2a\u8bcd\u90fd\u662f\u901a\u8fc7\u4e00\u4e2a\u9690\u542b\u7684\u968f\u673a\u8fc7\u7a0b\u751f\u6210\u7684\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u4e2a\u6587\u6863\u8868\u793a\u4e3a\u4e00\u7cfb\u5217\u4e3b\u9898\u7684\u6df7\u5408\uff0c\u800c\u6bcf\u4e2a\u4e3b\u9898\u5219\u8868\u793a\u4e3a\u4e00\u7cfb\u5217\u8bcd\u7684\u6df7\u5408\u3002LDA\u65e8\u5728\u901a\u8fc7\u540e\u5411\u63a8\u65ad\u8fd9\u4e9b\u6f5c\u5728\u7684\u4e3b\u9898\u7ed3\u6784\u6765\u63ed\u793a\u6587\u6863\u96c6\u5408\u4e2d\u7684\u4e3b\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u5229\u7528Python\u8fdb\u884cLDA\u6587\u672c\u5206\u6790\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u5904\u7406\u6587\u672c\u548c\u6267\u884cLDA\u5206\u6790\u7684\u5e93\u3002\u6700\u5e38\u7528\u7684\u5305\u62ec<code>nltk<\/code>\uff08\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5de5\u5177\u5305\uff09\u3001<code>gensim<\/code>\uff08\u7528\u4e8e\u4e3b\u9898\u5efa\u6a21\u7684\u5e93\uff09\u4ee5\u53ca<code>pyldavis<\/code>\uff08\u7528\u4e8eLDA\u53ef\u89c6\u5316\u7684\u5e93\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">!pip install nltk gensim pyLDAvis<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u51c6\u5907\u7f16\u7a0b\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5b89\u88c5\u4e86\u4e0a\u8ff0\u5e93\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165\u5b83\u4eec\u6765\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import nltk<\/p>\n<p>from gensim import corpora, models<\/p>\n<p>import pyLDAvis.gensim_models as gensimvis<\/p>\n<p>import pyLDAvis<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>nltk\u5e93\u901a\u5e38\u7528\u4e8e\u6587\u672c\u9884\u5904\u7406\uff0c\u5982\u5206\u8bcd\u548c\u53bb\u9664\u505c\u7528\u8bcd\u3002gensim\u5e93\u63d0\u4f9b\u4e86\u7528\u4e8e\u6784\u5efaLDA\u6a21\u578b\u7684\u5b9e\u7528\u5de5\u5177\uff0c\u800cpyLDAvis\u5219\u4f7f\u5f97\u5728Jupyter\u7b14\u8bb0\u672c\u4e2d\u53ef\u89c6\u5316LDA\u6a21\u578b\u6210\u4e3a\u53ef\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7406\u89e3LDA\u6a21\u578b\u539f\u7406<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Python\u8fdb\u884cLDA\u6587\u672c\u5206\u6790\uff0c\u7406\u89e3LDA\u6a21\u578b\u5982\u4f55\u5de5\u4f5c\u4ee5\u53ca\u5176\u539f\u7406\u81f3\u5173\u91cd\u8981\u3002LDA\u662f\u4e00\u79cd\u65e0\u76d1\u7763\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6280\u672f\uff0c\u5b83\u7684\u76ee\u6807\u662f\u53d1\u73b0\u6587\u6863\u96c6\u4e2d\u7684\u4e3b\u9898\u3002<\/p>\n<\/p>\n<p><h4>\u4e3b\u9898\u6a21\u578b\u57fa\u7840<\/h4>\n<\/p>\n<p><p>\u5728LDA\u4e2d\uff0c&quot;\u4e3b\u9898&quot;\u662f\u8bcd\u7684\u96c6\u5408\uff0c\u6bcf\u4e2a\u8bcd\u90fd\u6709\u4e00\u4e2a\u7279\u5b9a\u4e8e\u4e3b\u9898\u7684\u6743\u91cd\u3002\u7b97\u6cd5\u5c1d\u8bd5\u627e\u5230\u4e00\u4e2a\u4e3b\u9898\u5206\u5e03\uff0c\u8fd9\u4e2a\u5206\u5e03\u80fd\u591f\u6700\u597d\u5730\u89e3\u91ca\u89c2\u5bdf\u5230\u7684\u8bcd\u548c\u6587\u6863\u4e4b\u95f4\u7684\u5171\u73b0\u5173\u7cfb\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u6587\u6863\uff0cLDA\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4e3b\u9898\u7684\u6df7\u5408\uff0c\u8fd9\u6837\u6bcf\u4e2a\u6587\u6863\u5c31\u53ef\u4ee5\u8868\u793a\u4e3a\u7531\u591a\u4e2a\u4e3b\u9898\u6309\u4e00\u5b9a\u6bd4\u4f8b\u6df7\u5408\u800c\u6210\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u51c6\u5907\u6587\u672c\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4efb\u4f55\u6587\u672c\u5206\u6790\u4e4b\u524d\uff0c\u6570\u636e\u6536\u96c6\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002\u4f60\u9700\u8981\u4e00\u4e2a\u6587\u672c\u6570\u636e\u96c6\uff0c\u8fd9\u4e9b\u6570\u636e\u53ef\u4ee5\u662f\u6587\u7ae0\u3001\u8bc4\u8bba\u3001\u4e66\u7c4d\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u6536\u96c6\u4e0e\u52a0\u8f7d\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u6536\u96c6\u6570\u636e\u53ef\u80fd\u6d89\u53ca\u4ece\u7f51\u7ad9\u4e0a\u6293\u53d6\u6587\u672c\u6216\u52a0\u8f7d\u73b0\u6709\u7684\u6587\u6863\u96c6\u3002\u52a0\u8f7d\u6570\u636e\u540e\uff0c\u786e\u4fdd\u5c06\u5176\u7ec4\u7ec7\u5728\u53ef\u4f9b\u5206\u6790\u7684\u7ed3\u6784\u4e2d\uff0c\u901a\u5e38\u662f\u4e00\u4e2a\u6587\u6863\u5217\u8868\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6587\u6863\u5217\u8868<\/p>\n<p>documents = [&quot;\u6587\u672c1&quot;, &quot;\u6587\u672c2&quot;, &quot;\u6587\u672c3&quot;, ...]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6e05\u6d17\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5c06\u6587\u672c\u6570\u636e\u7528\u4e8eLDA\u5206\u6790\u4e4b\u524d\uff0c\u9700\u8981\u8fdb\u884c\u9884\u5904\u7406\uff0c\u4ee5\u63d0\u9ad8\u6a21\u578b\u7684\u7cbe\u786e\u5ea6\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>\u6587\u672c\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u4e00\u822c\u7684\u9884\u5904\u7406\u6b65\u9aa4\u5305\u62ec\u5c0f\u5199\u5316\u3001\u53bb\u9664\u6807\u70b9\u548c\u6570\u5b57\u3001\u5206\u8bcd\u3001\u53bb\u9664\u505c\u7528\u8bcd\u548c\u8bcd\u5e72\u63d0\u53d6\u7b49\u3002\u4f7f\u7528nltk\u5e93\u8fdb\u884c\u8fd9\u4e9b\u9884\u5904\u7406\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from nltk.corpus import stopwords<\/p>\n<p>from nltk.stem.wordnet import WordNetLemmatizer<\/p>\n<p>import string<\/p>\n<h2><strong>\u521d\u59cb\u5316\u505c\u7528\u8bcd\u5217\u8868\u3001\u8bcd\u5e72\u63d0\u53d6\u5668\u548c\u8981\u79fb\u9664\u7684\u6807\u70b9<\/strong><\/h2>\n<p>stop = set(stopwords.words(&#039;english&#039;))<\/p>\n<p>exclude = set(string.punctuation)<\/p>\n<p>lemma = WordNetLemmatizer()<\/p>\n<p>def clean(document):<\/p>\n<p>    stop_free = &quot; &quot;.join([word for word in document.lower().split() if word not in stop])<\/p>\n<p>    punc_free = &#039;&#039;.join(ch for ch in stop_free if ch not in exclude)<\/p>\n<p>    normalized = &quot; &quot;.join(lemma.lemmatize(word) for word in punc_free.split())<\/p>\n<p>    return normalized<\/p>\n<p>document_clean = [clean(document).split() for document in documents]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u6e05\u6d17\u51fd\u6570\u4f1a\u5faa\u73af\u904d\u5386\u6240\u6709\u6587\u6863\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\uff0c\u5176\u4e2d\u6bcf\u4e2a\u6587\u6863\u90fd\u662f\u5206\u8bcd\u548c\u6e05\u6d17\u8fc7\u7684\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u521b\u5efa\u8bcd\u888b\u548cTF-IDF\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884cLDA\u5206\u6790\u4e4b\u524d\uff0c\u5fc5\u987b\u5c06\u6587\u672c\u8f6c\u6362\u6210gensim\u53ef\u4ee5\u7406\u89e3\u7684\u683c\u5f0f\u3002\u8fd9\u901a\u5e38\u6d89\u53ca\u5230\u521b\u5efa\u8bcd\u888b\uff08Bag of Words, BoW\uff09\u6a21\u578b\u548c\/\u6216TF-IDF\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><h4>\u6784\u5efa\u8bcd\u888b\u6a21\u578b<\/h4>\n<\/p>\n<p><p>\u8bcd\u888b\u6a21\u578b\u662f\u901a\u8fc7\u8ba1\u6570\u6bcf\u4e2a\u552f\u4e00\u5355\u8bcd\u7684\u51fa\u73b0\u6b21\u6570\u6765\u8868\u793a\u6587\u6863\u3002\u8fd9\u79cd\u8868\u793a\u65b9\u6cd5\u7b80\u5355\u4f46\u5f3a\u5927\uff0c\u7ecf\u5e38\u88ab\u7528\u4f5c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4efb\u52a1\u7684 starting point\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u8bcd\u5178<\/p>\n<p>dictionary = corpora.Dictionary(document_clean)<\/p>\n<h2><strong>\u901a\u8fc7\u8bcd\u5178\u5c06\u6587\u6863\u8f6c\u6362\u4e3a\u8bcd\u888b\u6a21\u578b<\/strong><\/h2>\n<p>doc_term_matrix = [dictionary.doc2bow(doc) for doc in document_clean]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5229\u7528TF-IDF\u6a21\u578b<\/h4>\n<\/p>\n<p><p>TF-IDF\uff08Term Frequency-Inverse Document Frequency\uff0c\u5373\u8bcd\u9891-\u9006\u5411\u6587\u4ef6\u9891\u7387\uff09\u6a21\u578b\u662f\u4e00\u79cd\u7edf\u8ba1\u65b9\u6cd5\uff0c\u7528\u4ee5\u8bc4\u4f30\u4e00\u5b57\u8bcd\u5bf9\u4e8e\u4e00\u4e2a\u6587\u4ef6\u96c6\u6216\u4e00\u4e2a\u8bed\u6599\u5e93\u4e2d\u7684\u5176\u4e2d\u4e00\u4efd\u6587\u4ef6\u7684\u91cd\u8981\u7a0b\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u8bcd\u888b\u6a21\u578b\u6784\u5efaTF-IDF\u6a21\u578b<\/p>\n<p>tfidf = models.TfidfModel(doc_term_matrix)<\/p>\n<p>tfidf_corpus = tfidf[doc_term_matrix]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u8bad\u7ec3LDA\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u62e5\u6709\u4e86\u8bcd\u888b\u6216TF-IDF\u6a21\u578b\u540e\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528gensim\u6765\u6784\u5efaLDA\u6a21\u578b\u4e86\u3002<\/p>\n<\/p>\n<p><h4>LDA\u6a21\u578b\u6784\u5efa<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574LDA\u6a21\u578b\u7684\u53c2\u6570\uff0c\u4f8b\u5982\u4e3b\u9898\u6570\u76ee\u3001\u8fed\u4ee3\u6b21\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528gensim\u6765\u8bad\u7ec3LDA\u6a21\u578b<\/p>\n<p>ldamodel = models.LdaModel(tfidf_corpus, num_topics=5, id2word = dictionary, passes=50)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u8bad\u7ec3\u4e86\u4e00\u4e2a\u67095\u4e2a\u4e3b\u9898\u7684LDA\u6a21\u578b\uff0c\u5e76\u8fd0\u884c\u4e8650\u6b21\u8fed\u4ee3\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u8bc4\u4f30\u6a21\u578b\u8868\u73b0<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u6a21\u578b\u88ab\u8bad\u7ec3\uff0c\u5c31\u9700\u8981\u5bf9\u5176\u6548\u679c\u8fdb\u884c\u8bc4\u4f30\uff0c\u786e\u4fdd\u6a21\u578b\u751f\u6210\u7684\u4e3b\u9898\u662f\u6709\u610f\u4e49\u7684\u3002<\/p>\n<\/p>\n<p><h4>\u68c0\u67e5\u4e3b\u9898\u5173\u952e\u8bcd<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u68c0\u67e5\u6bcf\u4e2a\u4e3b\u9898\u7684\u5173\u952e\u8bcd\uff0c\u6211\u4eec\u53ef\u4ee5\u8bc4\u4f30\u4e3b\u9898\u662f\u5426\u5408\u9002\u548c\u6709\u533a\u5206\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5370\u6bcf\u4e2a\u4e3b\u9898\u7684\u5173\u952e\u8bcd<\/p>\n<p>for topic in ldamodel.print_topics(num_topics=5):<\/p>\n<p>    print(topic)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u5c06\u8f93\u51fa\u6bcf\u4e2a\u4e3b\u9898\u7684\u4e3b\u8981\u5355\u8bcd\u548c\u5b83\u4eec\u5728\u4e3b\u9898\u4e2d\u7684\u6743\u91cd\u3002<\/p>\n<\/p>\n<p><h4>\u8ba1\u7b97\u6a21\u578b\u4e00\u81f4\u6027<\/h4>\n<\/p>\n<p><p>\u6a21\u578b\u7684\u4e00\u81f4\u6027\u5f97\u5206\u53ef\u4ee5\u5e2e\u52a9\u8bc4\u4f30\u4e3b\u9898\u7684\u8d28\u91cf\u3002\u5728gensim\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528CoherenceModel\u6765\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u6a21\u578b\u4e00\u81f4\u6027\u5f97\u5206<\/p>\n<p>coherence_model_lda = models.CoherenceModel(model=ldamodel, texts=document_clean, dictionary=dictionary, coherence=&#039;c_v&#039;)<\/p>\n<p>coherence_lda = coherence_model_lda.get_coherence()<\/p>\n<p>print(&#039;Coherence Score:&#039;, coherence_lda)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u53ef\u89c6\u5316\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u7ed3\u679c\u53ef\u89c6\u5316\u6709\u52a9\u4e8e\u66f4\u52a0\u76f4\u89c2\u5730\u4e86\u89e3LDA\u6a21\u578b\u7684\u8f93\u51fa\u4ee5\u53ca\u6587\u6863\u5982\u4f55\u5206\u5e03\u5728\u4e0d\u540c\u7684\u4e3b\u9898\u4e0a\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528pyLDAvis\u8fdb\u884c\u53ef\u89c6\u5316<\/h4>\n<\/p>\n<p><p>pyLDAvis\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7684\u754c\u9762\uff0c\u53ef\u4ee5\u67e5\u770b\u6bcf\u4e2a\u4e3b\u9898\u4e0e\u5404\u8bcd\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u4ee5\u53ca\u4e3b\u9898\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528pyLDAvis\u53ef\u89c6\u5316\u4e3b\u9898\u6a21\u578b<\/p>\n<p>pyLDAvis.enable_notebook()<\/p>\n<p>vis = gensimvis.prepare(ldamodel, doc_term_matrix, dictionary)<\/p>\n<p>pyLDAvis.display(vis)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd0\u884c\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5229\u7528Python\u8fdb\u884cLDA\u6587\u672c\u5206\u6790\uff0c\u5e76\u5f97\u51fa\u6709\u610f\u4e49\u7684\u7ed3\u8bba\u3002\u786e\u4fdd\u5728\u6bcf\u4e00\u6b65\u90fd\u4ed4\u7ec6\u8c03\u6574\u548c\u8bc4\u4f30\uff0c\u6700\u7ec8\u80fd\u83b7\u5f97\u9ad8\u8d28\u91cf\u7684\u4e3b\u9898\u6a21\u578b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5229\u7528Python\u8fdb\u884cLDA\u6587\u672c\u5206\u6790\u7684\u521d\u6b65\u51c6\u5907\u5de5\u4f5c\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<ol>\n<li>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Python\u548c\u6240\u9700\u7684\u76f8\u5173\u5e93\uff0c\u5982<code>gensim<\/code>\u548c<code>nltk<\/code>\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>pip<\/code>\u547d\u4ee4\u6765\u5b89\u88c5\u8fd9\u4e9b\u5e93\u3002<\/li>\n<li>\u5176\u6b21\uff0c\u4f60\u9700\u8981\u51c6\u5907\u7528\u4e8eLDA\u5206\u6790\u7684\u8bed\u6599\u5e93\u3002\u8fd9\u53ef\u4ee5\u662f\u4e00\u7ec4\u6587\u6863\u6216\u6587\u7ae0\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u662f\u6587\u672c\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u4f60\u9700\u8981\u5bf9\u8bed\u6599\u8fdb\u884c\u9884\u5904\u7406\uff0c\u5305\u62ec\u6587\u672c\u7684\u5206\u8bcd\u3001\u53bb\u9664\u505c\u7528\u8bcd\u548c\u6807\u70b9\u7b26\u53f7\u7b49\u3002<code>nltk<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6709\u7528\u7684\u5de5\u5177\u548c\u51fd\u6570\u6765\u5b8c\u6210\u8fd9\u4e9b\u4efb\u52a1\u3002<\/li>\n<li>\u7136\u540e\uff0c\u4f60\u9700\u8981\u6784\u5efa\u4e00\u4e2a\u8bcd\u888b\u6a21\u578b\uff08bag-of-words model\uff09\uff0c\u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u6570\u503c\u8868\u793a\u3002<code>gensim<\/code>\u5e93\u4e2d\u7684<code>Dictionary<\/code>\u548c<code>Corpus<\/code>\u7c7b\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5b8c\u6210\u8fd9\u4e00\u6b65\u9aa4\u3002<\/li>\n<li>\u6700\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>gensim<\/code>\u5e93\u4e2d\u7684<code>LdaModel<\/code>\u7c7b\u6765\u62df\u5408LDA\u6a21\u578b\uff0c\u5e76\u4f7f\u7528\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u8fdb\u884c\u4e3b\u9898\u63a8\u65ad\u548c\u6587\u6863\u5206\u7c7b\u3002<\/li>\n<\/ol>\n<p><strong>\u5982\u4f55\u8bc4\u4f30LDA\u6587\u672c\u5206\u6790\u7684\u7ed3\u679c\u548c\u6548\u679c\uff1f\u6709\u54ea\u4e9b\u6307\u6807\u53ef\u4ee5\u4f7f\u7528\uff1f<\/strong><\/p>\n<ol>\n<li>\u4e00\u79cd\u5e38\u7528\u7684\u8bc4\u4f30\u65b9\u6cd5\u662f\u5229\u7528\u56f0\u60d1\u5ea6\uff08perplexity\uff09\u6307\u6807\u3002\u56f0\u60d1\u5ea6\u8d8a\u4f4e\uff0c\u8868\u793a\u6a21\u578b\u5bf9\u89c2\u5bdf\u6570\u636e\u7684\u62df\u5408\u7a0b\u5ea6\u8d8a\u597d\u3002\u53ef\u4ee5\u4f7f\u7528<code>gensim<\/code>\u5e93\u4e2d\u7684<code>LogPerplexity<\/code>\u51fd\u6570\u8ba1\u7b97\u56f0\u60d1\u5ea6\u3002<\/li>\n<li>\u53e6\u4e00\u79cd\u8bc4\u4f30\u65b9\u6cd5\u662f\u901a\u8fc7\u4eba\u5de5\u89c2\u5bdf\u548c\u5224\u65ad\u6a21\u578b\u751f\u6210\u7684\u4e3b\u9898\u662f\u5426\u5408\u7406\u548c\u53ef\u89e3\u91ca\u3002\u4f60\u53ef\u4ee5\u9605\u8bfb\u4e00\u4e9b\u4e3b\u9898\u8bcd\u548c\u76f8\u5173\u6587\u6863\uff0c\u5e76\u5224\u65ad\u5b83\u4eec\u662f\u5426\u4e0e\u9884\u671f\u4e00\u81f4\u3002<br \/>\n3.. \u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528\u4e00\u4e9b\u5ea6\u91cf\u6307\u6807\uff0c\u5982\u4e00\u81f4\u6027\uff08coherence\uff09\u548c\u5206\u79bb\u5ea6\uff08segregation\uff09\u6765\u8bc4\u4f30LDA\u6a21\u578b\u7684\u6548\u679c\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>gensim<\/code>\u5e93\u4e2d\u7684\u76f8\u5e94\u51fd\u6570\u6765\u8ba1\u7b97\u8fd9\u4e9b\u6307\u6807\u3002<\/li>\n<\/ol>\n<p><strong>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u6587\u672c\u5206\u6790\u548c\u4e3b\u9898\u5efa\u6a21\uff1f<\/strong><\/p>\n<ol>\n<li>\u5f53\u7136\uff0c\u5728\u6587\u672c\u5206\u6790\u548c\u4e3b\u9898\u5efa\u6a21\u65b9\u9762\uff0cLDA\u53ea\u662f\u4e00\u4e2a\u65b9\u6cd5\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u7c7b\u4f3c\u7684\u4efb\u52a1\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u6f5c\u5728\u8bed\u4e49\u5206\u6790\uff08Latent Semantic Analysis\uff0cLSA\uff09\u6216\u975e\u8d1f\u77e9\u9635\u5206\u89e3\uff08Non-negative Matrix Factorization\uff0cNMF\uff09\u7b49\u65b9\u6cd5\u3002<\/li>\n<li>\u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u57fa\u4e8e\u6df1\u5ea6\u5b66\u4e60\u7684\u65b9\u6cd5\uff0c\u5982\u4e3b\u9898\u6a21\u578b\u7684\u53d8\u79cdLDA2Vec\u6216BERT\uff0c\u4e5f\u88ab\u7528\u4e8e\u6587\u672c\u5206\u6790\u548c\u4e3b\u9898\u5efa\u6a21\u4efb\u52a1\u4e2d\uff0c\u5e76\u53d6\u5f97\u4e86\u4ee4\u4eba\u77a9\u76ee\u7684\u7ed3\u679c\u3002<\/li>\n<li>\u6700\u91cd\u8981\u7684\u662f\uff0c\u5728\u9009\u62e9\u65b9\u6cd5\u4e4b\u524d\uff0c\u5e94\u8be5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u548c\u6570\u636e\u7279\u70b9\u6765\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u6709\u4e0d\u540c\u7684\u4f18\u70b9\u548c\u9002\u7528\u8303\u56f4\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u8981\u8fdb\u884c\u9009\u62e9\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"\u5229\u7528Python\u8fdb\u884cLDA\u6587\u672c\u5206\u6790\uff0c\u5e94\u4ece\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\u5f00\u59cb\u3001\u7406\u89e3LDA\u7684\u5de5\u4f5c\u539f\u7406\u3001\u51c6\u5907\u6587\u672c\u6570\u636e\u3001\u6e05\u6d17\u6570\u636e\u3001\u521b\u5efa\u8bcd [&hellip;]","protected":false},"author":3,"featured_media":179793,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/179781"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=179781"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/179781\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/179793"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=179781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=179781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=179781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}