{"id":9426,"date":"2025-06-11T12:46:02","date_gmt":"2025-06-11T04:46:02","guid":{"rendered":"\/?p=9426"},"modified":"2025-06-11T12:46:46","modified_gmt":"2025-06-11T04:46:46","slug":"%e4%b8%89%e7%a7%8d%e8%af%8d%e8%a2%8b%e6%a8%a1%e5%9e%8bcountvectorizertfidfhashvectorizer","status":"publish","type":"post","link":"\/?p=9426","title":{"rendered":"\u4e09\u79cd\u8bcd\u888b\u6a21\u578bCountVectorizerTFIDFHashVectorizer"},"content":{"rendered":"<h3><strong>1. CountVectorizer<\/strong><\/h3>\n<p><code>CountVectorizer<\/code>\u7c7b\u4f1a\u5c06\u6587\u672c\u4e2d\u7684\u8bcd\u8bed\u8f6c\u6362\u4e3a\u8bcd\u9891\u77e9\u9635\u3002 \u4f8b\u5982\u77e9\u9635\u4e2d\u5305\u542b\u4e00\u4e2a\u5143\u7d20a[i][j]\uff0c\u5b83\u8868\u793aj\u8bcd\u5728i\u7c7b\u6587\u672c\u4e0b\u7684\u8bcd\u9891\u3002\u5b83\u901a\u8fc7<code>fit_transform<\/code>\u51fd\u6570\u8ba1\u7b97\u5404\u4e2a\u8bcd\u8bed\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u901a\u8fc7<code>get_feature_names()<\/code>\u53ef\u83b7\u53d6\u8bcd\u888b\u4e2d\u6240\u6709\u6587\u672c\u7684\u5173\u952e\u5b57\uff0c\u901a\u8fc7<code>toarray()<\/code>\u53ef\u770b\u5230\u8bcd\u9891\u77e9\u9635\u7684\u7ed3\u679c\u3002<\/p>\n<pre><code class=\"language-python3\">from sklearn.feature_extraction.text import CountVectorizer\n#\u8bed\u6599\ncorpus = [\n    &#039;This is the first document.&#039;,\n    &#039;This is the this second second document.&#039;,\n    &#039;And the third one.&#039;,\n    &#039;Is this the first document?&#039;\n]\n#\u5c06\u6587\u672c\u4e2d\u7684\u8bcd\u8f6c\u6362\u6210\u8bcd\u9891\u77e9\u9635\nvectorizer = CountVectorizer()\nprint(vectorizer)\n#\u8ba1\u7b97\u67d0\u4e2a\u8bcd\u51fa\u73b0\u7684\u6b21\u6570\nX = vectorizer.fit_transform(corpus)\nprint(type(X),X)\n#\u83b7\u53d6\u8bcd\u888b\u4e2d\u6240\u6709\u6587\u672c\u5173\u952e\u8bcd\nword = vectorizer.get_feature_names()\nprint(word)\n#\u67e5\u770b\u8bcd\u9891\u7ed3\u679c\nprint(X.toarray())<\/code><\/pre>\n<p>\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-text\">&lt;class &#039;scipy.sparse._csr.csr_matrix&#039;&gt;   (np.int32(0), np.int32(8)) 1\n  (np.int32(0), np.int32(3))    1\n  (np.int32(0), np.int32(6))    1\n  (np.int32(0), np.int32(2))    1\n  (np.int32(0), np.int32(1))    1\n  (np.int32(1), np.int32(8))    2\n  (np.int32(1), np.int32(3))    1\n  (np.int32(1), np.int32(6))    1\n  (np.int32(1), np.int32(1))    1\n  (np.int32(1), np.int32(5))    2\n  (np.int32(2), np.int32(6))    1\n  (np.int32(2), np.int32(0))    1\n  (np.int32(2), np.int32(7))    1\n  (np.int32(2), np.int32(4))    1\n  (np.int32(3), np.int32(8))    1\n  (np.int32(3), np.int32(3))    1\n  (np.int32(3), np.int32(6))    1\n  (np.int32(3), np.int32(2))    1\n  (np.int32(3), np.int32(1))    1\n[&#039;and&#039;, &#039;document&#039;, &#039;first&#039;, &#039;is&#039;, &#039;one&#039;, &#039;second&#039;, &#039;the&#039;, &#039;third&#039;, &#039;this&#039;]\n[[0 1 1 1 0 0 1 0 1]\n [0 1 0 1 0 2 1 0 2]\n [1 0 0 0 1 0 1 1 0]\n [0 1 1 1 0 0 1 0 1]]<\/code><\/pre>\n<p>\u6211\u4eec\u770b\u4e0b\u8bcd\u7684\u5206\u5e03\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">and<\/th>\n<th style=\"text-align: center;\">document<\/th>\n<th style=\"text-align: center;\">first<\/th>\n<th style=\"text-align: center;\">is<\/th>\n<th style=\"text-align: center;\">one<\/th>\n<th style=\"text-align: center;\">second<\/th>\n<th style=\"text-align: center;\">the<\/th>\n<th style=\"text-align: center;\">third<\/th>\n<th style=\"text-align: center;\">this<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">2<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">2<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u518d\u770b\u4e0b\u539f\u6bb5\u843d\uff0c\u53ef\u4ee5\u9a8c\u8bc1\u4ee5\u4e0b\u5206\u5e03\u662f\u5426\u6b63\u786e(<u><em>a[i][j]\uff0c\u5b83\u8868\u793aj\u8bcd\u5728i\u7c7b\u6587\u672c\u4e0b\u7684\u8bcd\u9891<\/em><\/u>)\uff1a<\/p>\n<pre><code>1:    &#039;This is the first document.&#039;,\n2:    &#039;This is the this second second document.&#039;,\n3:    &#039;And the third one.&#039;,\n4:    &#039;Is this the first document?&#039;<\/code><\/pre>\n<h4>\u5173\u4e8e\u7ed3\u679c X\uff1a<\/h4>\n<p><code>X = vectorizer.fit_transform(corpus)<\/code>:<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20250611113043782.png\" alt=\"image-20250611113043782\" \/><\/p>\n<p><strong>\u6587\u6863\u8bcd\u9891\u77e9\u9635\uff08\u7a20\u5bc6\u5f62\u5f0f\uff09<\/strong>\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u6587\u6863<\/th>\n<th style=\"text-align: center;\">and<\/th>\n<th style=\"text-align: center;\">document<\/th>\n<th style=\"text-align: center;\">first<\/th>\n<th style=\"text-align: center;\">is<\/th>\n<th style=\"text-align: center;\">one<\/th>\n<th style=\"text-align: center;\">second<\/th>\n<th style=\"text-align: center;\">the<\/th>\n<th style=\"text-align: center;\">third<\/th>\n<th style=\"text-align: center;\">this<\/th>\n<th style=\"text-align: center;\"><strong>\u975e\u96f6\u503c\u6570\u91cf<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\"><strong>5\u4e2a<\/strong> (1+1+1+1+1)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">2<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">2<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">2<\/td>\n<td style=\"text-align: center;\"><strong>6\u4e2a<\/strong> (1+1+2+1+2)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">3<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\"><strong>4\u4e2a<\/strong> (1+1+1+1)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">4<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\">0<\/td>\n<td style=\"text-align: center;\">1<\/td>\n<td style=\"text-align: center;\"><strong>4\u4e2a<\/strong> (1+1+1+1+1)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><strong>\u603b\u8ba1<\/strong><\/td>\n<td style=\"text-align: center;\">-<\/td>\n<td style=\"text-align: center;\">-<\/td>\n<td style=\"text-align: center;\">-<\/td>\n<td style=\"text-align: center;\">-<\/td>\n<td style=\"text-align: center;\">-<\/td>\n<td style=\"text-align: center;\">-<\/td>\n<td style=\"text-align: center;\">-<\/td>\n<td style=\"text-align: center;\">-<\/td>\n<td style=\"text-align: center;\">-<\/td>\n<td style=\"text-align: center;\"><strong>19\u4e2a\u975e\u96f6\u503c<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u77e9\u9635\u7ef4\u5ea6\u662f4(\u6587\u6863\u6570)\u00d79(\u8bcd\u6c47\u91cf)=36\uff0c\u4f46\u5b9e\u9645\u975e\u96f6\u5143\u7d20\u53ea\u670919\u4e2a\uff08\u89c1X.data\u6570\u7ec4\u957f\u5ea6\uff09\uff0c\u56e0\u6b64X.size=19\u53cd\u6620\u7684\u662f\u6240\u6709\u6587\u6863\u4e2d\u5b9e\u9645\u51fa\u73b0\u7684\u8bcd\u6c47\u8ba1\u6570\u603b\u548c<\/p>\n<h4>\u5173\u4e8e\u8bcd\u6c47\u8868\u987a\u5e8f\uff1a<\/h4>\n<p><code>vectorizer.get_feature_names()<\/code> \u8fd4\u56de\u7684\u8bcd\u6c47\u5217\u8868\u6309 <strong>\u5b57\u6bcd\u5347\u5e8f<\/strong> \u6392\u5217\uff08\u4ece a \u5230 z\uff09\uff0c\u8fd9\u662f <code>CountVectorizer<\/code> \u7684\u9ed8\u8ba4\u884c\u4e3a\u3002<br \/>\n\u5728\u7528\u6237\u4ee3\u7801\u7684\u8bed\u6599\u4e2d\uff0c\u8bcd\u6c47\u987a\u5e8f\u4e3a\uff1a<br \/>\n<code>[&#039;and&#039;, &#039;document&#039;, &#039;first&#039;, &#039;is&#039;, &#039;one&#039;, &#039;second&#039;, &#039;the&#039;, &#039;third&#039;, &#039;this&#039;]<\/code><\/p>\n<ul>\n<li><strong>\u9a8c\u8bc1<\/strong>\uff1a<code>and<\/code>\uff08a\u5f00\u5934\uff09\u6392\u6700\u524d\uff0c<code>this<\/code>\uff08t\u5f00\u5934\uff09\u6392\u6700\u540e\u3002<\/li>\n<\/ul>\n<p><strong>\ud83d\udccc \u987a\u5e8f\u8c03\u6574\u65b9\u6cd5<\/strong><\/p>\n<p>\u82e5\u9700\u81ea\u5b9a\u4e49\u987a\u5e8f\uff0c\u53ef\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\"># \u6309\u8bcd\u6c47\u9996\u6b21\u51fa\u73b0\u7684\u987a\u5e8f\u6392\u5e8f\uff08\u975e\u5b57\u6bcd\u5e8f\uff09\nvectorizer = CountVectorizer(vocabulary=sorted(set(&quot; &quot;.join(corpus).split())))<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u9ed8\u8ba4\u5b57\u6bcd\u5e8f\u53ef\u786e\u4fdd\u7ed3\u679c\u53ef\u590d\u73b0\uff0c\u4e0d\u53d7\u8bed\u6599\u8f93\u5165\u987a\u5e8f\u5f71\u54cd<\/p>\n<h3><strong>2. TF-IDF<\/strong><\/h3>\n<p><strong>TF-IDF<\/strong>(term frequency-inverse document frequency)\u662f\u6587\u672c\u52a0\u6743\u65b9\u6cd5\uff0c\u91c7\u7528\u7edf\u8ba1\u601d\u60f3\uff0c\u5373\u6587\u672c\u51fa\u73b0\u7684\u6b21\u6570\u548c\u6574\u4e2a\u8bed\u6599\u4e2d\u6587\u6863\u9891\u7387\u6765\u8ba1\u7b97\u5b57\u8bcd\u7684\u91cd\u8981\u5ea6\uff0c\u662f\u4e00\u79cd\u7528\u4e8e\u8bc4\u4f30\u8bcd\u5728\u6587\u6863\u4e2d\u91cd\u8981\u6027\u7684\u7edf\u8ba1\u65b9\u6cd5\uff0c\u5176\u6838\u5fc3\u601d\u60f3\u662f\uff1a<strong>\u8bcd\u7684\u91cd\u8981\u6027\u968f\u5176\u5728\u6587\u6863\u4e2d\u7684\u51fa\u73b0\u9891\u7387\u6b63\u6bd4\u589e\u52a0\uff0c\u4f46\u968f\u5176\u5728\u8bed\u6599\u5e93\u4e2d\u7684\u666e\u904d\u6027\u53cd\u6bd4\u4e0b\u964d<\/strong>\u3002<\/p>\n<p><strong>\u4ec0\u4e48\u662fTF-IDF<\/strong><\/p>\n<p>TF-IDF\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6587\u672c\u5904\u7406\u6280\u672f\uff0c\u7528\u4ee5\u8bc4\u4f30\u4e00\u4e2a\u8bcd\u5bf9\u4e8e\u4e00\u7bc7\u6587\u7ae0\u6216\u8bed\u6599\u5e93\u4e2d\u4e00\u7bc7\u6587\u7ae0\u7684\u91cd\u8981\u6027\u3002TF\u4ee3\u8868\u8bcd\u9891(Term Frequency)\uff0cIDF\u4ee3\u8868\u9006\u6587\u6863\u9891\u7387(Inverse Document Frequency)\u3002\u5b57\u8bcd\u7684\u91cd\u8981\u6027\u968f\u7740\u5b83\u5728\u6587\u4ef6\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u6210\u6b63\u6bd4\u589e\u52a0\uff0c\u4f46\u540c\u65f6\u4f1a\u968f\u7740\u5b83\u5728\u8bed\u6599\u5e93\u4e2d\u51fa\u73b0\u7684\u9891\u7387\u6210\u53cd\u6bd4\u4e0b\u964d\u3002<\/p>\n<p><strong>TF-IDF\u7684\u4f7f\u7528\u573a\u666f<\/strong><\/p>\n<p>TF-IDF\u5e38\u88ab\u7528\u4e8e\u6587\u672c\u5206\u7c7b\u3001\u4fe1\u606f\u68c0\u7d22\u3001\u5173\u952e\u8bcd\u63d0\u53d6\u7b49\u9886\u57df\u3002\u5728\u6587\u672c\u5206\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u6839\u636eTF-IDF\u503c\u6765\u8ba1\u7b97\u6587\u672c\u4e0e\u67d0\u4e2a\u7c7b\u522b\u7684\u76f8\u5173\u7a0b\u5ea6\uff1b\u5728\u4fe1\u606f\u68c0\u7d22\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u7528\u6237\u8f93\u5165\u7684\u5173\u952e\u8bcd\u7684TF-IDF\u503c\u6765\u6392\u5e8f\u641c\u7d22\u7ed3\u679c\uff1b\u5728\u5173\u952e\u8bcd\u63d0\u53d6\u4e2d\uff0c\u53ef\u4ee5\u6839\u636eTF-IDF\u503c\u6765\u786e\u5b9a\u6587\u672c\u4e2d\u7684\u5173\u952e\u8bcd\u3002<\/p>\n<p><strong>TF-IDF\u539f\u7406<\/strong><\/p>\n<p><strong>TF<\/strong>\uff08\u5168\u79f0TermFrequency\uff09\u6307\u7684\u662f\u67d0\u4e2a\u8bcd\u5728\u6587\u672c\u4e2d\u51fa\u73b0\u7684\u9891\u7387\u3002\u5982\u679c\u4e00\u4e2a\u8bcd\u5728\u6587\u672c\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u8d8a\u591a\uff0c\u90a3\u4e48\u5b83\u7684TF\u503c\u5c31\u8d8a\u9ad8\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u7bc7\u6587\u7ae0\u4e2d\uff0c\u8bcd\u8bed\u201capple\u201d\u51fa\u73b0\u4e865\u6b21\uff0c\u800c\u603b\u8bcd\u6570\u4e3a1000\u4e2a\uff0c\u90a3\u4e48\u5b83\u7684TF\u503c\u4e3a0.005\u3002<\/p>\n<p>\u8fd9\u5176\u4e2d\u8fd8\u6709\u4e00\u4e2a\u6f0f\u6d1e\uff0c\u5c31\u662f \u201d\u7684\u201c \u201d\u662f\u201c \u201d\u554a\u201c \u7b49\u7c7b\u4f3c\u7684\u8bcd\u5728\u6587\u7ae0\u4e2d\u51fa\u73b0\u7684\u6b64\u65f6\u662f\u975e\u5e38\u591a\u7684\uff0c\u4f46\u662f\u8fd9\u4e9b\u5927\u591a\u90fd\u662f\u6ca1\u6709\u610f\u4e49\u8bcd\uff0c\u5bf9\u4e8e\u5224\u65ad\u6587\u7ae0\u7684\u5173\u952e\u8bcd\u51e0\u4e4e\u6ca1\u6709\u4ec0\u4e48\u7528\u5904\uff0c\u6211\u4eec\u79f0\u8fd9\u4e9b\u8bcd\u4e3a\u201d\u505c\u7528\u8bcd\u201c\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728\u5ea6\u91cf\u76f8\u5173\u6027\u7684\u65f6\u5019\u4e0d\u5e94\u8be5\u8003\u8651\u8fd9\u4e9b\u8bcd\u7684\u9891\u7387\u3002<\/p>\n<p><strong>IDF<\/strong>\uff08\u5168\u79f0InverseDocumentFrequency\uff09\u6307\u7684\u662f\u4e00\u4e2a\u8bcd\u5728\u6587\u672c\u96c6\u5408\u4e2d\u7684\u91cd\u8981\u7a0b\u5ea6\u3002\u5982\u679c\u4e00\u4e2a\u8bcd\u5728\u6574\u4e2a\u6587\u672c\u96c6\u5408\u4e2d\u51fa\u73b0\u7684\u6587\u6863\u6570\u8d8a\u5c11\uff0c\u90a3\u4e48\u5b83\u7684IDF\u503c\u5c31\u8d8a\u9ad8\uff0c\u8bf4\u660e\u8fd9\u4e2a\u8bcd\u5728\u6587\u672c\u4e2d\u7684\u91cd\u8981\u7a0b\u5ea6\u8d8a\u9ad8\u3002\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u75311000\u7bc7\u6587\u7ae0\u7ec4\u6210\u7684\u6587\u672c\u96c6\u5408\u4e2d\uff0c\u8bcd\u8bed\u201capple\u201d\u53ea\u51fa\u73b0\u572810\u7bc7\u6587\u7ae0\u4e2d\uff0c\u90a3\u4e48\u5b83\u7684IDF\u503c\u4e3alog(1000\/10) = 2\u3002<\/p>\n<p><strong>TF-IDF<\/strong> \u503c\u5c31\u662f\u5c06TF\u548cIDF\u76f8\u4e58\u5f97\u5230\u7684\u7ed3\u679c\u3002\u5b83\u53cd\u6620\u4e86\u4e00\u4e2a\u8bcd\u5728\u6587\u672c\u4e2d\u7684\u91cd\u8981\u6027\u3002\u5982\u679c\u4e00\u4e2a\u8bcd\u5728\u6587\u672c\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u8d8a\u591a\uff0c\u540c\u65f6\u5728\u6574\u4e2a\u6587\u672c\u96c6\u5408\u4e2d\u51fa\u73b0\u7684\u6587\u6863\u6570\u8d8a\u5c11\uff0c\u90a3\u4e48\u5b83\u7684TF-IDF\u503c\u5c31\u8d8a\u9ad8\uff0c\u8bf4\u660e\u8fd9\u4e2a\u8bcd\u5728\u6587\u672c\u4e2d\u7684\u91cd\u8981\u7a0b\u5ea6\u8d8a\u9ad8\u3002<\/p>\n<p><strong>TF-IDF\u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a<\/strong>\uff1a<\/p>\n<p><strong>\u4f18\u70b9<\/strong>\uff1a\u8fc7\u6ee4\u4e00\u4e9b\u5e38\u89c1\u4f46\u662f\u65e0\u5173\u7d27\u8981\u7684\u5b57\u8bcd\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20250611114013531.png\" alt=\"image-20250611114013531\" \/><\/p>\n<p><strong>TF<\/strong>\uff08Term Frequency\uff09\u8868\u793a\u67d0\u4e2a\u5173\u952e\u8bcd\u5728\u6574\u7bc7\u6587\u7ae0\u4e2d\u51fa\u73b0\u7684\u9891\u7387\u3002\uff08\u67d0\u4e2a\u8bcd\u5728\u6587\u7ae0\u4e2d\u51fa\u73b0\u7684\u603b\u6b21\u6570\/\u6587\u7ae0\u7684\u603b\u8bcd\u6570\uff09;<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20250611114025223.png\" alt=\"image-20250611114025223\" \/><\/p>\n<p><strong>IDF<\/strong>\uff08Inverse Document Frequency\uff09\u8868\u793a\u8ba1\u7b97\u5012\u6587\u672c\u9891\u7387\u3002\u6587\u672c\u9891\u7387\u662f\u6307\u67d0\u4e2a\u5173\u952e\u8bcd\u5728\u6574\u4e2a\u8bed\u6599\u6240\u6709\u6587\u7ae0\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002\u5012\u6587\u6863\u9891\u7387\u53c8\u79f0\u4e3a\u9006\u6587\u6863\u9891\u7387\uff0c\u5b83\u662f\u6587\u6863\u9891\u7387\u7684\u5012\u6570\uff0c\u4e3b\u8981\u7528\u4e8e\u964d\u4f4e\u6240\u6709\u6587\u6863\u4e2d\u4e00\u4e9b\u5e38\u89c1\u5374\u5bf9\u6587\u6863\u5f71\u54cd\u4e0d\u5927\u7684\u8bcd\u8bed\u7684\u4f5c\u7528\u3002<\/p>\n<p><strong><a href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.feature_extraction.text.TfidfTransformer.html%23sklearn.feature_extraction.text.TfidfTransformer\">Transformer \u5b98\u65b9\u6587\u6863<\/a><\/strong><\/p>\n<p><strong>\u9ed8\u8ba4\uff1a<\/strong><\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20250611114040635.png\" alt=\"image-20250611114040635\" \/><\/p>\n<p>\u8fd9\u91cc\uff0c <strong><em>N<\/em><\/strong>\u4e3a\u603b\u7684\u6587\u6863\u6570\uff0c<strong><em>N<\/em><\/strong>(<em>x<\/em>)\u4e3a\u5305\u542b\u8fd9\u4e2a\u8bcd<em>x<\/em>\u7684\u6587\u6863\u6570\u3002<\/p>\n<p><strong>\u6559\u79d1\u4e66\u6807\u51c6\u7684idf\u5b9a\u4e49\uff1a<\/strong><\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20250611114153410.png\" alt=\"image-20250611114153410\" \/><\/p>\n<p>\u5176\u4e2d\uff0c <strong><em>N<\/em><\/strong>\u4ee3\u8868\u8bed\u6599\u5e93\u7684\u6587\u6863\u603b\u6570\uff1b <strong><em>N<\/em><\/strong>(<em>x<\/em>)\u4ee3\u8868\u5305\u542b\u8be5\u8bcd<em>x<\/em>\u7684\u6587\u6863\u6570.<\/p>\n<p>Tfidf \u5b9e\u73b0\uff0c\u4e00\u822c\u662f\u5148\u901a\u8fc7<code>countVectorizer<\/code>, \u7136\u540e\u518d\u901a\u8fc7 <code>tfidfTransformer<\/code>\uff0c \u8f6c\u6362\u6210 <code>tfidf<\/code> \u5411\u91cf; \u4e5f\u6709\u73b0\u6210\u7684 <code>TfidfVectorizer<\/code> API\u3002<\/p>\n<p><strong>\u8bed\u53e5\uff1a<\/strong><\/p>\n<pre><code class=\"language-python\">TfidfTransformer(norm=&#039;l2&#039;, use_idf=True, smooth_idf=True, sublinear_tf=False)<\/code><\/pre>\n<p>\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from sklearn.feature_extraction.text import TfidfVectorizer, TfidfTransformer, CountVectorizer\nimport numpy as np\n#\u8bed\u6599\ncc = [\n      &#039;aa bb.&#039;,\n      &#039;aa cc.&#039;\n]\n# method 1\nvectorizer = TfidfVectorizer()\nX = vectorizer.fit_transform(cc)\nprint(&#039;feature&#039;,vectorizer.get_feature_names())\nprint(X.toarray())<\/code><\/pre>\n<p>\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-text\">feature [&#039;aa&#039;, &#039;bb&#039;, &#039;cc&#039;]\n[[0.57973867 0.81480247 0.        ]\n [0.57973867 0.         0.81480247]]<\/code><\/pre>\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u9ed8\u8ba4\u8bed\u6599\u4f1a\u628a\u5355\u4e2a\u5b57\u7b26\u5f53\u4f5c\u505c\u6b62\u5355\u8bcd\uff08stop_words\uff09\u8fdb\u884c\u8fc7\u6ee4\uff0c\u5982\u679c\u9700\u8981\u4fdd\u7559\u5355\u4e2a\u5b57\u7b26\u7ec4\u6210\u7684\u5355\u8bcd\uff0c\u53ef\u4ee5\u4fee\u6539\u5206\u8bcd\u65b9\u5f0f\uff1a<code>token_pattern=&#039;(?u)\\\\b\\\\w+\\\\b&#039;<\/code><\/p>\n<p>\u6b64\u5916\uff0c\u4e0a\u9762\u8fd8\u53ef\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\"># method 2\nvectorizer=CountVectorizer()#token_pattern=&#039;(?u)\\\\b\\\\w+\\\\b&#039;\ntransformer = TfidfTransformer()\ncntTf = vectorizer.fit_transform(cc)\nprint(&#039;feature&#039;, vectorizer.get_feature_names())\nprint(cntTf)\ncnt_array = cntTf.toarray()\nX = transformer.fit_transform(cntTf)\nprint(X.toarray())<\/code><\/pre>\n<p>\u7ed3\u679c\uff1a<\/p>\n<pre><code>feature [&#039;aa&#039;, &#039;bb&#039;, &#039;cc&#039;]\n# \u7b2c\u4e00\u4e2a\u6570\u5b57\u8868\u793a\u7b2c\u51e0\u4e2a\u6587\u6863\uff1b\u7b2c\u4e8c\u4e2a\u6570\u5b57\u8868\u793a\u7b2c\u51e0\u4e2afeature\uff0c\u7ed3\u679c\u8868\u793a\u76f8\u5e94\u7684\u8bcd\u9891\u3002\n  (0, 1)        1\n  (0, 0)        1\n  (1, 2)        1\n  (1, 0)        1\n[[0.57973867 0.81480247 0.        ]\n [0.57973867 0.         0.81480247]]<\/code><\/pre>\n<p>\u4e3a\u4e86\u66f4\u52a0\u660e\u767d<code>TfidfTransformer<\/code>\u7684\u64cd\u4f5c\uff0c\u8fdb\u884c\u7b80\u5355\u5206\u89e3\u5b9e\u73b0\u8be5\u529f\u80fd\uff1a<\/p>\n<pre><code class=\"language-python\"># method 3\nvectorizer=CountVectorizer()\ncntTf = vectorizer.fit_transform(cc)\ntf = cnt_array\/np.sum(cnt_array, axis = 1, keepdims = True)\nprint(&#039;tf&#039;,tf)\nidf = np.log((1+len(cnt_array))\/(1+np.sum(cnt_array,axis = 0))) + 1\nprint(&#039;idf&#039;, idf)\nt = tf*idf\nprint(&#039;tfidf&#039;,t)\nprint(&#039;norm tfidf&#039;, t\/np.sqrt(np.sum(t**2, axis = 1, keepdims=True)))<\/code><\/pre>\n<p>\u7ed3\u679c:<\/p>\n<pre><code class=\"language-text\">tf [[0.5 0.5 0. ]\n [0.5 0.  0.5]]\nidf [1.         1.40546511 1.40546511]\ntfidf [[0.5        0.70273255 0.        ]\n [0.5        0.         0.70273255]]\nnorm tfidf [[0.57973867 0.81480247 0.        ]\n [0.57973867 0.         0.81480247]]<\/code><\/pre>\n<p>\u4e5f\u5c31\u662f\u8bf4\uff0c<code>TfidfTransformer<\/code> \u9ed8\u8ba4\u4f1a\u5bf9\u83b7\u5f97\u7684\u5411\u91cf\u9664\u4ee52\u8303\u6570\u8fdb\u884c\u5f52\u4e00\u5316\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20250611114340337.png\" alt=\"image-20250611114340337\" \/><\/p>\n<p><strong>TF-IDF\u7684\u4fe1\u606f\u8bba\u4f9d\u636e<\/strong><\/p>\n<p>\u4e00\u4e2a\u67e5\u8be2\uff08Query\uff09\u4e2d\u7684\u6bcf\u4e2a\u5173\u952e\u8bcd\uff08Key Word\uff09w\u7684\u6743\u91cd\u5e94\u8be5\u53cd\u6620\u8fd9\u4e2a\u8bcd\u67e5\u8be2\u6765\u8bb2\u63d0\u4f9b\u4e86\u591a\u5c11\u4fe1\u606f\u3002\u4e00\u4e2a\u7b80\u5355\u65b9\u6cd5\u662f\u7528\u6bcf\u4e2a\u8bcd\u7684\u4fe1\u606f\u91cf\u4f5c\u4e3a\u4ed6\u7684\u6743\u91cd\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/v2-1c55841df6bbca5169ff2a9b7281c251_1440w.webp\" alt=\"img\" \/><\/p>\n<p>\u4f46\u662f\uff0c\u5982\u679c\u4e24\u4e2a\u8bcd\u51fa\u73b0\u7684\u9891\u7387 TF\u76f8\u540c\uff0c \u4e00\u4e2a\u662f\u67d0\u7bc7\u7279\u5b9a\u6587\u7ae0\u4e2d\u7684\u5e38\u89c1\u8bcd\uff0c\u800c\u53e6\u5916\u4e00\u4e2a\u8bcd\u65f6\u5206\u6563\u5728\u591a\u7bc7\u6587\u7ae0\u4e2d\uff0c\u663e\u7136\u7b2c\u4e00\u4e2a\u8bcd\u6709\u66f4\u9ad8\u7684\u5206\u8fa8\u7387\uff0c\u6743\u91cd\u5e94\u8be5\u66f4\u5927\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/v2-3dd664400572cae6d45a21a3ba539dcf_1440w.webp\" alt=\"img\" \/><\/p>\n<h3><strong>3. HashingVectorizer<\/strong><\/h3>\n<p><strong>\u8bed\u6cd5<\/strong><\/p>\n<pre><code class=\"language-python\">HashingVectorizer(alternate_sign=True, analyzer=&#039;word&#039;, binary=False,\n         decode_error=&#039;strict&#039;, dtype=&lt;class &#039;numpy.float64&#039;&gt;,\n         encoding=&#039;utf-8&#039;, input=&#039;content&#039;, lowercase=True,\n         n_features=1048576, ngram_range=(1, 1), non_negative=False,\n         norm=&#039;l2&#039;, preprocessor=None, stop_words=None, strip_accents=None,\n         token_pattern=&#039;(?u)\\\\b\\\\w\\\\w+\\\\b&#039;, tokenizer=None)<\/code><\/pre>\n<p><strong>\u7279\u70b9<\/strong><\/p>\n<p>\u666e\u901a\u7684CountVectorizer\u5b58\u5728\u4f46\u8bcd\u5e93\u5f88\u5927\u65f6\uff0c\u5360\u7528\u5927\u5185\u5b58\uff0c\u56e0\u6b64\uff0c\u4f7f\u7528hash\u6280\u5de7\uff0c\u5e76\u7528\u7a00\u758f\u77e9\u9635\u5b58\u50a8\u7f16\u8bd1\u540e\u7684\u77e9\u9635\uff0c\u80fd\u5f88\u597d\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<p>\u5b9e\u73b0\u7684\u4f2a\u4ee3\u7801\u4e3a\uff1a<\/p>\n<pre><code class=\"language-c\"> function hashing_vectorizer(features : array of string, N : integer):\n     x := new vector[N]\n     for f in features:\n         h := hash(f)\n         x[h mod N] += 1\n     return x<\/code><\/pre>\n<p>\u8fd9\u91cc\u4f2a\u4ee3\u7801\u6ca1\u6709\u8003\u8651\u5230<code>hash<\/code>\u51b2\u7a81\u7684\u60c5\u51b5\uff0c\u5b9e\u9645\u5b9e\u73b0\u4f1a\u66f4\u52a0\u590d\u6742\u3002<\/p>\n<pre><code class=\"language-python\">from sklearn.feature_extraction.text import HashingVectorizer\ncorpus = [\n     &#039;This is the first document.&#039;,\n     &#039;This document is the second document.&#039;,\n     &#039;And this is the third one.&#039;,\n     &#039;Is this the first document?&#039;,\n ]\nvectorizer = HashingVectorizer(n_features=2**4)\nX = vectorizer.fit_transform(corpus)\nprint(X.toarray())\nprint(X.shape)<\/code><\/pre>\n<p>\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-text\">[[-0.57735027  0.          0.          0.          0.          0.\n   0.          0.         -0.57735027  0.          0.          0.\n   0.          0.57735027  0.          0.        ]\n [-0.81649658  0.          0.          0.          0.          0.\n   0.          0.          0.          0.          0.          0.40824829\n   0.          0.40824829  0.          0.        ]\n [ 0.          0.          0.          0.         -0.70710678  0.70710678\n   0.          0.          0.          0.          0.          0.\n   0.          0.          0.          0.        ]\n [-0.57735027  0.          0.          0.          0.          0.\n   0.          0.         -0.57735027  0.          0.          0.\n   0.          0.57735027  0.          0.        ]]\n(4, 16)<\/code><\/pre>\n<h3><strong>4. \u603b\u7ed3<\/strong><\/h3>\n<p>\u603b\u7684\u800c\u8a00\uff0c\u8fd9\u4e09\u79cd\u90fd\u662f\u8bcd\u888b\u6a21\u578b\u7684\u65b9\u6cd5\uff0c\u5176\u4e2d\uff0c\u7531\u4e8e<code>tfidfvectorizer<\/code>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u964d\u4f4e\u9ad8\u9891\u4fe1\u606f\u91cf\u5c11\u8bcd\u8bed\u7684\u5e72\u6270\uff0c\u5e94\u7528\u5f97\u66f4\u591a\u3002<\/p>\n<hr \/>\n<p>reference:<\/p>\n<ol>\n<li><strong><a href=\"https:\/\/scikit-learn.org\/stable\/modules\/generated\/sklearn.feature_extraction.text.TfidfVectorizer.html\">(\u63a8\u8350)sklearn tfidf<\/a><\/strong>\uff1b<\/li>\n<li><strong><a href=\"https:\/\/blog.csdn.net\/wf592523813\/article\/details\/81911155\">TF-IDF blog<\/a><\/strong>\uff1b<\/li>\n<li><strong><a href=\"https:\/\/www.cnblogs.com\/pinard\/p\/6693230.html\">\u5218\u5efa\u5e73 \u535a\u5ba2<\/a><\/strong>\uff1b<\/li>\n<li><strong><a href=\"https:\/\/scikit-learn.org\/stable\/modules\/feature_extraction.html%23common-vectorizer-usage\">(\u63a8\u8350) Sklearn\u5b98\u7f51 Feature extraction<\/a><\/strong>;<\/li>\n<li><strong><a href=\"https:\/\/zhangzirui.github.io\/posts\/Document-14%20%28sklearn-feature%29.md\">\u5b66\u4e60sklearn\u4e4b\u6587\u672c\u7279\u5f81\u63d0\u53d6<\/a><\/strong>;<\/li>\n<li><strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/Feature_hashing\">wiki, feature hashing<\/a><\/strong>;<\/li>\n<li><strong><a href=\"https:\/\/zhuanlan.zhihu.com\/write\">\u6570\u5b66\u4e4b\u7f8e \u5434\u519b<\/a><\/strong><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>1. CountVectorizer CountVectorizer\u7c7b\u4f1a\u5c06\u6587\u672c\u4e2d\u7684\u8bcd\u8bed\u8f6c\u6362\u4e3a\u8bcd\u9891\u77e9\u9635\u3002 \u4f8b\u5982\u77e9\u9635\u4e2d\u5305\u542b\u4e00\u4e2a\u5143\u7d20a[i][   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[225],"tags":[226],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9426"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9426"}],"version-history":[{"count":2,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9426\/revisions"}],"predecessor-version":[{"id":9428,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9426\/revisions\/9428"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9426"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}