{"id":1114582,"date":"2025-01-08T17:58:51","date_gmt":"2025-01-08T09:58:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1114582.html"},"modified":"2025-01-08T17:58:53","modified_gmt":"2025-01-08T09:58:53","slug":"python3%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8lda%e4%b8%bb%e9%a2%98%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1114582.html","title":{"rendered":"python3\u4e2d\u5982\u4f55\u4f7f\u7528LDA\u4e3b\u9898\u6a21\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075709\/d64232e5-e7d1-4cac-94ac-468c999b6bf8.webp\" alt=\"python3\u4e2d\u5982\u4f55\u4f7f\u7528LDA\u4e3b\u9898\u6a21\u578b\" \/><\/p>\n<p><p> <strong>\u5728Python3\u4e2d\uff0c\u4f7f\u7528LDA\u4e3b\u9898\u6a21\u578b\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u51c6\u5907\u6570\u636e\u3001\u6587\u672c\u9884\u5904\u7406\u3001\u521b\u5efa\u5b57\u5178\u548c\u8bed\u6599\u5e93\u3001\u8bad\u7ec3LDA\u6a21\u578b\u3001\u5206\u6790\u548c\u89e3\u91ca\u7ed3\u679c\u3002<\/strong> \u5176\u4e2d\uff0c\u6587\u672c\u9884\u5904\u7406\u662f\u6700\u5173\u952e\u7684\u4e00\u6b65\uff0c\u5b83\u76f4\u63a5\u5f71\u54cdLDA\u6a21\u578b\u7684\u6548\u679c\u3002\u6587\u672c\u9884\u5904\u7406\u5305\u62ec\u53bb\u9664\u505c\u7528\u8bcd\u3001\u6807\u70b9\u7b26\u53f7\u3001\u63d0\u53d6\u8bcd\u5e72\u7b49\u64cd\u4f5c\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u80fd\u5e2e\u52a9\u6a21\u578b\u66f4\u597d\u5730\u8bc6\u522b\u6587\u672c\u4e2d\u7684\u4e3b\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u51c6\u5907\u6570\u636e<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u51c6\u5907\u8981\u8fdb\u884c\u4e3b\u9898\u5efa\u6a21\u7684\u6587\u672c\u6570\u636e\u3002\u6570\u636e\u53ef\u4ee5\u662f\u4efb\u610f\u5f62\u5f0f\u7684\u6587\u672c\u6587\u6863\uff0c\u6bd4\u5982\u6587\u7ae0\u3001\u8bba\u6587\u3001\u65b0\u95fb\u7b49\u3002\u6211\u4eec\u9700\u8981\u5c06\u8fd9\u4e9b\u6587\u672c\u6587\u6863\u8bfb\u53d6\u5230Python\u4e2d\uff0c\u901a\u5e38\u4f7f\u7528pandas\u5e93\u6765\u5904\u7406\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;path_to_your_file.csv&#39;)<\/p>\n<p>texts = data[&#39;text_column&#39;].tolist()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u6587\u672c\u9884\u5904\u7406<\/p>\n<\/p>\n<p><p>\u6587\u672c\u9884\u5904\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u6b65\uff0c\u5b83\u51b3\u5b9a\u4e86\u540e\u7eedLDA\u6a21\u578b\u6548\u679c\u7684\u597d\u574f\u3002\u901a\u5e38\uff0c\u6587\u672c\u9884\u5904\u7406\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u53bb\u9664\u505c\u7528\u8bcd\u548c\u6807\u70b9\u7b26\u53f7<\/strong><\/li>\n<li><strong>\u5206\u8bcd<\/strong><\/li>\n<li><strong>\u63d0\u53d6\u8bcd\u5e72<\/strong><\/li>\n<li><strong>\u53bb\u9664\u4f4e\u9891\u8bcd<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>from nltk.corpus import stopwords<\/p>\n<p>from nltk.stem import WordNetLemmatizer<\/p>\n<p>from nltk.tokenize import word_tokenize<\/p>\n<h2><strong>\u4e0b\u8f7d\u5fc5\u8981\u7684NLTK\u8d44\u6e90<\/strong><\/h2>\n<p>import nltk<\/p>\n<p>nltk.download(&#39;punkt&#39;)<\/p>\n<p>nltk.download(&#39;stopwords&#39;)<\/p>\n<p>nltk.download(&#39;wordnet&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u6587\u672c\u9884\u5904\u7406\u51fd\u6570<\/strong><\/h2>\n<p>def preprocess(text):<\/p>\n<p>    # \u8f6c\u4e3a\u5c0f\u5199<\/p>\n<p>    text = text.lower()<\/p>\n<p>    # \u53bb\u9664\u6807\u70b9\u7b26\u53f7<\/p>\n<p>    text = re.sub(r&#39;\\W&#39;, &#39; &#39;, text)<\/p>\n<p>    # \u5206\u8bcd<\/p>\n<p>    words = word_tokenize(text)<\/p>\n<p>    # \u53bb\u9664\u505c\u7528\u8bcd<\/p>\n<p>    words = [word for word in words if word not in stopwords.words(&#39;english&#39;)]<\/p>\n<p>    # \u63d0\u53d6\u8bcd\u5e72<\/p>\n<p>    lemmatizer = WordNetLemmatizer()<\/p>\n<p>    words = [lemmatizer.lemmatize(word) for word in words]<\/p>\n<p>    return words<\/p>\n<h2><strong>\u5e94\u7528\u9884\u5904\u7406\u51fd\u6570<\/strong><\/h2>\n<p>processed_texts = [preprocess(text) for text in texts]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u521b\u5efa\u5b57\u5178\u548c\u8bed\u6599\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u9884\u5904\u7406\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u9700\u8981\u5c06\u6587\u672c\u6570\u636e\u8f6c\u5316\u4e3aLDA\u6a21\u578b\u53ef\u4ee5\u63a5\u53d7\u7684\u683c\u5f0f\uff0c\u5373\u5b57\u5178\u548c\u8bed\u6599\u5e93\u3002\u5b57\u5178\u662f\u4e00\u4e2a\u5305\u542b\u6240\u6709\u8bcd\u6c47\u7684\u5217\u8868\uff0c\u800c\u8bed\u6599\u5e93\u662f\u6bcf\u4e2a\u6587\u6863\u7684\u8bcd\u9891\u8868\u793a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from gensim import corpora<\/p>\n<h2><strong>\u521b\u5efa\u5b57\u5178<\/strong><\/h2>\n<p>dictionary = corpora.Dictionary(processed_texts)<\/p>\n<h2><strong>\u521b\u5efa\u8bed\u6599\u5e93<\/strong><\/h2>\n<p>corpus = [dictionary.doc2bow(text) for text in processed_texts]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u8bad\u7ec3LDA\u6a21\u578b<\/p>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u597d\u5b57\u5178\u548c\u8bed\u6599\u5e93\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528gensim\u5e93\u6765\u8bad\u7ec3LDA\u6a21\u578b\u3002\u6211\u4eec\u9700\u8981\u6307\u5b9a\u4e3b\u9898\u6570\u548c\u5176\u4ed6\u53c2\u6570\u6765\u8bad\u7ec3\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from gensim.models import LdaModel<\/p>\n<h2><strong>\u8bad\u7ec3LDA\u6a21\u578b<\/strong><\/h2>\n<p>num_topics = 10  # \u8bbe\u5b9a\u4e3b\u9898\u6570<\/p>\n<p>lda_model = LdaModel(corpus, num_topics=num_topics, id2word=dictionary, passes=15)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5206\u6790\u548c\u89e3\u91ca\u7ed3\u679c<\/p>\n<\/p>\n<p><p>\u8bad\u7ec3\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u67e5\u770b\u6bcf\u4e2a\u4e3b\u9898\u7684\u5173\u952e\u8bcd\u4ee5\u53ca\u6587\u6863\u7684\u4e3b\u9898\u5206\u5e03\u3002LDA\u6a21\u578b\u7684\u7ed3\u679c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u6587\u672c\u6570\u636e\u4e2d\u7684\u9690\u85cf\u4e3b\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u67e5\u770b\u6bcf\u4e2a\u4e3b\u9898\u7684\u5173\u952e\u8bcd<\/p>\n<p>topics = lda_model.print_topics(num_words=10)<\/p>\n<p>for topic in topics:<\/p>\n<p>    print(topic)<\/p>\n<h2><strong>\u67e5\u770b\u6bcf\u4e2a\u6587\u6863\u7684\u4e3b\u9898\u5206\u5e03<\/strong><\/h2>\n<p>doc_topics = [lda_model.get_document_topics(doc) for doc in corpus]<\/p>\n<p>for i, doc_topic in enumerate(doc_topics):<\/p>\n<p>    print(f&quot;Document {i+1}: {doc_topic}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u53ef\u89c6\u5316\u4e3b\u9898\u6a21\u578b<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3LDA\u6a21\u578b\u7684\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528pyLDAvis\u5e93\u6765\u53ef\u89c6\u5316\u4e3b\u9898\u6a21\u578b\u3002pyLDAvis\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4ea4\u4e92\u5f0f\u7684\u53ef\u89c6\u5316\u754c\u9762\uff0c\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u548c\u89e3\u91ca\u4e3b\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyLDAvis<\/p>\n<p>import pyLDAvis.gensim_models as gensimvis<\/p>\n<h2><strong>\u53ef\u89c6\u5316LDA\u6a21\u578b<\/strong><\/h2>\n<p>lda_vis = gensimvis.prepare(lda_model, corpus, dictionary)<\/p>\n<p>pyLDAvis.show(lda_vis)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u8c03\u6574\u548c\u4f18\u5316\u6a21\u578b<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u83b7\u5f97\u66f4\u597d\u7684\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u6574LDA\u6a21\u578b\u7684\u53c2\u6570\uff0c\u4f8b\u5982\u4e3b\u9898\u6570\u3001\u8bad\u7ec3\u8f6e\u6570\u7b49\u3002\u6b64\u5916\uff0c\u6587\u672c\u9884\u5904\u7406\u7684\u8d28\u91cf\u4e5f\u4f1a\u5f71\u54cd\u6a21\u578b\u6548\u679c\uff0c\u56e0\u6b64\u53ef\u4ee5\u5c1d\u8bd5\u4e0d\u540c\u7684\u9884\u5904\u7406\u65b9\u6cd5\u6765\u4f18\u5316\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8c03\u6574\u4e3b\u9898\u6570<\/p>\n<p>num_topics = 20  # \u589e\u52a0\u4e3b\u9898\u6570<\/p>\n<p>lda_model = LdaModel(corpus, num_topics=num_topics, id2word=dictionary, passes=15)<\/p>\n<h2><strong>\u518d\u6b21\u67e5\u770b\u4e3b\u9898\u5173\u952e\u8bcd<\/strong><\/h2>\n<p>topics = lda_model.print_topics(num_words=10)<\/p>\n<p>for topic in topics:<\/p>\n<p>    print(topic)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u4f7f\u7528LDA\u4e3b\u9898\u6a21\u578b\u8fdb\u884c\u6587\u672c\u5206\u6790\u662f\u4e00\u9879\u590d\u6742\u7684\u4efb\u52a1\uff0c\u9700\u8981\u4ed4\u7ec6\u7684\u6587\u672c\u9884\u5904\u7406\u548c\u53c2\u6570\u8c03\u6574\u3002\u901a\u8fc7Python\u4e2d\u7684gensim\u5e93\u548cpyLDAvis\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u6784\u5efa\u548c\u53ef\u89c6\u5316LDA\u6a21\u578b\uff0c\u4ece\u800c\u63ed\u793a\u6587\u672c\u6570\u636e\u4e2d\u7684\u9690\u85cf\u4e3b\u9898\u3002\u8bb0\u4f4f\uff0c\u6587\u672c\u9884\u5904\u7406\u7684\u8d28\u91cf\u76f4\u63a5\u5f71\u54cdLDA\u6a21\u578b\u7684\u6548\u679c\uff0c\u56e0\u6b64\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u9002\u5f53\u8c03\u6574\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>LDA\u4e3b\u9898\u6a21\u578b\u7684\u57fa\u672c\u6982\u5ff5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>LDA\uff08Latent Dirichlet Allocation\uff09\u662f\u4e00\u79cd\u751f\u6210\u6a21\u578b\uff0c\u7528\u4e8e\u53d1\u73b0\u6587\u6863\u96c6\u5408\u4e2d\u7684\u4e3b\u9898\u3002\u5b83\u5047\u8bbe\u6bcf\u4e2a\u6587\u6863\u90fd\u662f\u7531\u591a\u4e2a\u4e3b\u9898\u6df7\u5408\u800c\u6210\uff0c\u800c\u6bcf\u4e2a\u4e3b\u9898\u53c8\u662f\u7531\u591a\u4e2a\u8bcd\u6c47\u7ec4\u6210\u7684\u3002\u901a\u8fc7LDA\u6a21\u578b\uff0c\u53ef\u4ee5\u4ece\u5927\u91cf\u6587\u672c\u4e2d\u63d0\u53d6\u51fa\u6f5c\u5728\u7684\u4e3b\u9898\uff0c\u5e2e\u52a9\u5206\u6790\u548c\u7406\u89e3\u6587\u672c\u6570\u636e\u3002<\/p>\n<p><strong>\u5728Python3\u4e2d\u5982\u4f55\u5b89\u88c5LDA\u6240\u9700\u7684\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python3\u4e2d\u4f7f\u7528LDA\u4e3b\u9898\u6a21\u578b\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>gensim<\/code>\u5e93\uff0c\u8fd9\u662f\u4e00\u4e2a\u975e\u5e38\u6d41\u884c\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c<code>pip install gensim<\/code>\u6765\u5b89\u88c5\u5b83\u3002\u6b64\u5916\uff0c\u4e3a\u4e86\u9884\u5904\u7406\u6587\u672c\u6570\u636e\uff0c\u60a8\u53ef\u80fd\u8fd8\u9700\u8981\u5b89\u88c5<code>nltk<\/code>\u6216<code>spaCy<\/code>\u7b49\u5e93\u8fdb\u884c\u5206\u8bcd\u548c\u53bb\u9664\u505c\u7528\u8bcd\u3002<\/p>\n<p><strong>\u5982\u4f55\u51c6\u5907\u6570\u636e\u4ee5\u9002\u5e94LDA\u6a21\u578b\u7684\u8f93\u5165\u683c\u5f0f\uff1f<\/strong><br \/>\u5728\u4f7f\u7528LDA\u6a21\u578b\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u5bf9\u6587\u672c\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3002\u901a\u5e38\uff0c\u8fd9\u5305\u62ec\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u6587\u672c\u6e05\u6d17\uff08\u53bb\u9664\u6807\u70b9\u7b26\u53f7\u548c\u6570\u5b57\uff09\u3001\u5206\u8bcd\uff08\u5c06\u53e5\u5b50\u62c6\u5206\u6210\u5355\u8bcd\uff09\u3001\u53bb\u9664\u505c\u7528\u8bcd\uff08\u5982\u201c\u7684\u201d\u3001\u201c\u662f\u201d\u7b49\u5e38\u89c1\u8bcd\u6c47\uff09\u3001\u8bcd\u5e72\u63d0\u53d6\u6216\u8bcd\u5f62\u8fd8\u539f\u3002\u5904\u7406\u540e\u7684\u6587\u672c\u9700\u8981\u8f6c\u6362\u4e3a\u8bcd\u888b\u6a21\u578b\u6216TF-IDF\u683c\u5f0f\uff0c\u4ee5\u4fbfLDA\u80fd\u591f\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bc4\u4f30LDA\u6a21\u578b\u7684\u6548\u679c\uff1f<\/strong><br \/>\u8bc4\u4f30LDA\u6a21\u578b\u7684\u6548\u679c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u3002\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4e3b\u9898\u4e00\u81f4\u6027\u8bc4\u5206\uff08\u5982UMass\u6216C_V\u6307\u6807\uff09\uff0c\u8fd9\u4e9b\u6307\u6807\u53ef\u4ee5\u5e2e\u52a9\u5224\u65ad\u63d0\u53d6\u7684\u4e3b\u9898\u662f\u5426\u5408\u7406\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u901a\u8fc7\u53ef\u89c6\u5316\u5de5\u5177\uff08\u5982pyLDAvis\uff09\u6765\u89c2\u5bdf\u4e3b\u9898\u4e4b\u95f4\u7684\u5173\u7cfb\u4ee5\u53ca\u4e3b\u9898\u4e0b\u7684\u8bcd\u6c47\u5206\u5e03\uff0c\u4ece\u800c\u66f4\u597d\u5730\u7406\u89e3\u6a21\u578b\u7684\u8868\u73b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python3\u4e2d\uff0c\u4f7f\u7528LDA\u4e3b\u9898\u6a21\u578b\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u51c6\u5907\u6570\u636e\u3001\u6587\u672c\u9884\u5904\u7406\u3001\u521b\u5efa\u5b57\u5178\u548c\u8bed\u6599\u5e93\u3001\u8bad\u7ec3LDA\u6a21\u578b\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1114589,"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\/1114582"}],"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=1114582"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114582\/revisions"}],"predecessor-version":[{"id":1114592,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1114582\/revisions\/1114592"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1114589"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1114582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1114582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1114582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}