{"id":1053209,"date":"2024-12-31T14:31:56","date_gmt":"2024-12-31T06:31:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1053209.html"},"modified":"2024-12-31T14:32:00","modified_gmt":"2024-12-31T06:32:00","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97%e6%96%87%e6%a1%a3%e4%b8%ad%e7%9a%84%e8%af%8d%e9%a2%91%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1053209.html","title":{"rendered":"python\u5982\u4f55\u8ba1\u7b97\u6587\u6863\u4e2d\u7684\u8bcd\u9891\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/f47bf0cc-2586-433a-8cc5-f775d1bf3374.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u8ba1\u7b97\u6587\u6863\u4e2d\u7684\u8bcd\u9891\u77e9\u9635\" \/><\/p>\n<p><p> <strong>Python\u8ba1\u7b97\u6587\u6863\u4e2d\u7684\u8bcd\u9891\u77e9\u9635\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u5e93\u3001\u5916\u90e8\u5e93\u7b49\u3002\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528CountVectorizer\u3001TfidfVectorizer\u3001\u624b\u52a8\u7f16\u5199\u4ee3\u7801\u5b9e\u73b0\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c<strong>CountVectorizer<\/strong> \u662f\u6700\u5e38\u89c1\u548c\u65b9\u4fbf\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u8ba1\u7b97\u6587\u6863\u4e2d\u7684\u8bcd\u9891\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528CountVectorizer\u8ba1\u7b97\u8bcd\u9891\u77e9\u9635<\/h3>\n<\/p>\n<p><p><strong>CountVectorizer<\/strong> \u662fScikit-learn\u5e93\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e00\u7ec4\u6587\u6863\u8f6c\u6362\u4e3a\u8bcd\u9891\u77e9\u9635\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8be6\u7ec6\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Scikit-learn\u5e93\uff0c\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scikit-learn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u4f60\u7684Python\u4ee3\u7801\u4e2d\uff0c\u5bfc\u5165CountVectorizer\u548c\u5176\u4ed6\u5fc5\u8981\u7684\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.feature_extraction.text import CountVectorizer<\/p>\n<p>import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u51c6\u5907\u6587\u6863\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6587\u6863\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">documents = [<\/p>\n<p>    &quot;This is the first document.&quot;,<\/p>\n<p>    &quot;This document is the second document.&quot;,<\/p>\n<p>    &quot;And this is the third one.&quot;,<\/p>\n<p>    &quot;Is this the first document?&quot;<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u4f7f\u7528CountVectorizer\u8f6c\u6362\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aCountVectorizer\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528fit_transform\u65b9\u6cd5\u5c06\u6587\u6863\u8f6c\u6362\u4e3a\u8bcd\u9891\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">vectorizer = CountVectorizer()<\/p>\n<p>X = vectorizer.fit_transform(documents)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5\u3001\u67e5\u770b\u8bcd\u9891\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u5c06\u8bcd\u9891\u77e9\u9635\u8f6c\u6362\u4e3aDataFrame\u4ee5\u4fbf\u66f4\u597d\u5730\u67e5\u770b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.DataFrame(X.toarray(), columns=vectorizer.get_feature_names_out())<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u6b65\u9aa4\u5c06\u8f93\u51fa\u4e00\u4e2aDataFrame\uff0c\u5176\u4e2d\u6bcf\u4e00\u884c\u8868\u793a\u4e00\u4e2a\u6587\u6863\uff0c\u6bcf\u4e00\u5217\u8868\u793a\u4e00\u4e2a\u8bcd\uff0c\u5355\u5143\u683c\u4e2d\u7684\u503c\u8868\u793a\u8be5\u8bcd\u5728\u5bf9\u5e94\u6587\u6863\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528TfidfVectorizer\u8ba1\u7b97TF-IDF\u77e9\u9635<\/h3>\n<\/p>\n<p><p>TF-IDF\uff08Term Frequency-Inverse Document Frequency\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6587\u672c\u8868\u793a\u65b9\u6cd5\uff0c\u5b83\u4e0d\u4ec5\u8003\u8651\u8bcd\u7684\u9891\u7387\uff0c\u8fd8\u8003\u8651\u8bcd\u5728\u6240\u6709\u6587\u6863\u4e2d\u7684\u91cd\u8981\u6027\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Scikit-learn\u5e93\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.feature_extraction.text import TfidfVectorizer<\/p>\n<p>import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u51c6\u5907\u6587\u6863\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6587\u6863\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">documents = [<\/p>\n<p>    &quot;This is the first document.&quot;,<\/p>\n<p>    &quot;This document is the second document.&quot;,<\/p>\n<p>    &quot;And this is the third one.&quot;,<\/p>\n<p>    &quot;Is this the first document?&quot;<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u4f7f\u7528TfidfVectorizer\u8f6c\u6362\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2aTfidfVectorizer\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528fit_transform\u65b9\u6cd5\u5c06\u6587\u6863\u8f6c\u6362\u4e3aTF-IDF\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">vectorizer = TfidfVectorizer()<\/p>\n<p>X = vectorizer.fit_transform(documents)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5\u3001\u67e5\u770bTF-IDF\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u5c06TF-IDF\u77e9\u9635\u8f6c\u6362\u4e3aDataFrame\u4ee5\u4fbf\u66f4\u597d\u5730\u67e5\u770b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.DataFrame(X.toarray(), columns=vectorizer.get_feature_names_out())<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u624b\u52a8\u8ba1\u7b97\u8bcd\u9891\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u60f3\u4f9d\u8d56\u5916\u90e8\u5e93\uff0c\u53ef\u4ee5\u624b\u52a8\u7f16\u5199\u4ee3\u7801\u6765\u8ba1\u7b97\u8bcd\u9891\u77e9\u9635\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from collections import Counter<\/p>\n<p>import pandas as pd<\/p>\n<p>import re<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u51c6\u5907\u6587\u6863\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u6587\u6863\u7684\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">documents = [<\/p>\n<p>    &quot;This is the first document.&quot;,<\/p>\n<p>    &quot;This document is the second document.&quot;,<\/p>\n<p>    &quot;And this is the third one.&quot;,<\/p>\n<p>    &quot;Is this the first document?&quot;<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u9884\u5904\u7406\u6587\u6863<\/h4>\n<\/p>\n<p><p>\u5c06\u6240\u6709\u6587\u6863\u8f6c\u6362\u4e3a\u5c0f\u5199\uff0c\u5e76\u79fb\u9664\u6807\u70b9\u7b26\u53f7\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">processed_docs = [re.sub(r&#39;\\W+&#39;, &#39; &#39;, doc.lower()) for doc in documents]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u8ba1\u7b97\u8bcd\u9891<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Counter\u8ba1\u7b97\u6bcf\u4e2a\u6587\u6863\u4e2d\u7684\u8bcd\u9891\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">word_counts = [Counter(doc.split()) for doc in processed_docs]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5\u3001\u521b\u5efa\u8bcd\u9891\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u83b7\u53d6\u6240\u6709\u552f\u4e00\u8bcd\uff0c\u5e76\u521b\u5efa\u4e00\u4e2aDataFrame\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">unique_words = list(set(word for doc in word_counts for word in doc))<\/p>\n<p>df = pd.DataFrame(columns=unique_words)<\/p>\n<p>for i, word_count in enumerate(word_counts):<\/p>\n<p>    df.loc[i] = {word: word_count.get(word, 0) for word in unique_words}<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u8ba1\u7b97\u6587\u6863\u4e2d\u7684\u8bcd\u9891\u77e9\u9635\u662f\u6587\u672c\u5206\u6790\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4e2d\u7684\u57fa\u7840\u6b65\u9aa4\u3002<strong>CountVectorizer<\/strong> \u548c <strong>TfidfVectorizer<\/strong> \u63d0\u4f9b\u4e86\u65b9\u4fbf\u5feb\u6377\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\uff0c\u800c\u624b\u52a8\u8ba1\u7b97\u8bcd\u9891\u77e9\u9635\u5219\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u5176\u80cc\u540e\u7684\u539f\u7406\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u5e94\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u8bcd\u9891\u77e9\u9635\u7684\u8ba1\u7b97\u3002\u65e0\u8bba\u662f\u4f7f\u7528\u5e93\u8fd8\u662f\u624b\u52a8\u5b9e\u73b0\uff0c\u90fd\u9700\u8981\u6ce8\u610f\u6570\u636e\u7684\u9884\u5904\u7406\u6b65\u9aa4\uff0c\u4ee5\u786e\u4fdd\u5f97\u5230\u51c6\u786e\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8ba1\u7b97\u6587\u6863\u4e2d\u7684\u8bcd\u9891\u77e9\u9635\uff1f<\/strong><\/p>\n<p>\u8981\u8ba1\u7b97\u6587\u6863\u4e2d\u7684\u8bcd\u9891\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684\u591a\u4e2a\u5e93\uff0c\u5982<code>pandas<\/code>\u3001<code>sklearn<\/code>\u6216<code>nltk<\/code>\u3002\u9996\u5148\uff0c\u9700\u5c06\u6587\u6863\u8bfb\u53d6\u4e3a\u6587\u672c\u6570\u636e\uff0c\u7136\u540e\u8fdb\u884c\u5206\u8bcd\u3001\u53bb\u9664\u505c\u7528\u8bcd\u7b49\u9884\u5904\u7406\u6b65\u9aa4\u3002\u63a5\u7740\uff0c\u5229\u7528<code>CountVectorizer<\/code>\u6216<code>TfidfVectorizer<\/code>\u7b49\u5de5\u5177\u53ef\u4ee5\u751f\u6210\u8bcd\u9891\u77e9\u9635\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>pandas<\/code>\u5c06\u7ed3\u679c\u8f6c\u5316\u4e3a\u6613\u4e8e\u5206\u6790\u7684DataFrame\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5728\u8ba1\u7b97\u8bcd\u9891\u77e9\u9635\u65f6\uff0c\u5982\u4f55\u5904\u7406\u505c\u7528\u8bcd\uff1f<\/strong><\/p>\n<p>\u5728\u5904\u7406\u6587\u672c\u6570\u636e\u65f6\uff0c\u505c\u7528\u8bcd\u662f\u6307\u5728\u5206\u6790\u4e2d\u4e0d\u63d0\u4f9b\u6709\u610f\u4e49\u4fe1\u606f\u7684\u5e38\u7528\u8bcd\uff0c\u4f8b\u5982\u201c\u662f\u201d\u3001\u201c\u7684\u201d\u3001\u201c\u5728\u201d\u7b49\u3002\u4f7f\u7528Python\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>sklearn<\/code>\u4e2d\u7684<code>CountVectorizer<\/code>\u8bbe\u7f6e<code>stop_words=&#39;english&#39;<\/code>\u6765\u81ea\u52a8\u6392\u9664\u82f1\u8bed\u4e2d\u7684\u505c\u7528\u8bcd\u3002\u6b64\u5916\uff0c\u7528\u6237\u4e5f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u505c\u7528\u8bcd\u5217\u8868\uff0c\u901a\u8fc7<code>stop_words<\/code>\u53c2\u6570\u4f20\u5165\u81ea\u5b9a\u4e49\u7684\u8bcd\u8868\uff0c\u4ece\u800c\u66f4\u597d\u5730\u9002\u5e94\u7279\u5b9a\u5e94\u7528\u573a\u666f\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5de5\u5177\u6216\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u8ba1\u7b97\u8bcd\u9891\u77e9\u9635\uff1f<\/strong><\/p>\n<p>\u8ba1\u7b97\u8bcd\u9891\u77e9\u9635\u7684\u5de5\u5177\u548c\u5e93\u6709\u5f88\u591a\u3002\u5e38\u89c1\u7684\u6709<code>sklearn<\/code>\u4e2d\u7684<code>CountVectorizer<\/code>\u548c<code>TfidfVectorizer<\/code>\uff0c\u5b83\u4eec\u90fd\u80fd\u6709\u6548\u751f\u6210\u8bcd\u9891\u77e9\u9635\u3002<code>nltk<\/code>\u5e93\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u9002\u5408\u8fdb\u884c\u66f4\u7ec6\u81f4\u7684\u6587\u672c\u9884\u5904\u7406\u3002\u6b64\u5916\uff0c<code>pandas<\/code>\u5e93\u5bf9\u4e8e\u6570\u636e\u5904\u7406\u548c\u7ed3\u679c\u53ef\u89c6\u5316\u4e5f\u6781\u4e3a\u4fbf\u5229\uff0c\u53ef\u4ee5\u5c06\u8ba1\u7b97\u51fa\u7684\u8bcd\u9891\u77e9\u9635\u8f6c\u5316\u4e3aDataFrame\u8fdb\u884c\u8fdb\u4e00\u6b65\u5206\u6790\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8ba1\u7b97\u6587\u6863\u4e2d\u7684\u8bcd\u9891\u77e9\u9635\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u5e93\u3001\u5916\u90e8\u5e93\u7b49\u3002\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528CountVecto [&hellip;]","protected":false},"author":3,"featured_media":1053230,"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\/1053209"}],"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=1053209"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053209\/revisions"}],"predecessor-version":[{"id":1053234,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1053209\/revisions\/1053234"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1053230"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1053209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1053209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1053209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}