{"id":705,"date":"2024-03-15T18:05:31","date_gmt":"2024-03-15T10:05:31","guid":{"rendered":"http:\/\/lingbo.online\/?p=705"},"modified":"2024-03-19T13:56:45","modified_gmt":"2024-03-19T05:56:45","slug":"tf-idf","status":"publish","type":"post","link":"https:\/\/lingbo.online\/index.php\/algorithm_learning\/tf-idf\/","title":{"rendered":"TF-IDF\u7b97\u6cd5\u8be6\u89e3"},"content":{"rendered":"<h1>\u7b80\u4ecb<\/h1>\n<p>TF-IDF(term frequency\u2013inverse document frequency)\u662f\u4e00\u79cd\u7528\u4e8e\u4fe1\u606f\u68c0\u7d22\u4e0e\u6570\u636e\u6316\u6398\u7684\u5e38\u7528\u52a0\u6743\u6280\u672f\uff0c\u5e38\u7528\u4e8e\u6316\u6398\u6587\u7ae0\u4e2d\u7684\u5173\u952e\u8bcd\uff0c\u800c\u4e14\u7b97\u6cd5\u7b80\u5355\u9ad8\u6548\uff0c\u5e38\u88ab\u5de5\u4e1a\u7528\u4e8e\u6700\u5f00\u59cb\u7684\u6587\u672c\u6570\u636e\u6e05\u6d17\u3002<br \/>\nTF-IDF\u7531\u4e24\u90e8\u5206\u7ec4\u6210\uff0c\u7b2c\u4e00\u90e8\u5206\uff1aTF(Term Frequency)\u2014\u2014\u8bcd\u9891\uff1b\u7b2c\u4e8c\u90e8\u5206\uff1aIDF\uff08Inverse Document Frequency\uff09\u2014\u2014\u9006\u6587\u6863\u9891\u7387\u3002\u5f53\u6709TF\uff08\u8bcd\u9891\uff09\u548c\u9006\u6587\u6863\u9891\u7387\uff08IDF\uff09\u540e\uff0c\u5c06\u8fd9\u4e24\u4e2a\u8bcd\u76f8\u4e58\uff0c\u5f97\u5230\u4e00\u4e2a\u8bcd\u7684TF-IDF\u503c\u3002\u67d0\u4e2a\u8bcd\u5728\u6587\u7ae0\u4e2d\u7684TF-IDF\u8d8a\u5927\uff0c\u90a3\u4e48\u4e00\u822c\u800c\u8a00\u8fd9\u4e2a\u8bcd\u5728\u6587\u7ae0\u4e2d\u7684\u91cd\u8981\u6027\u8d8a\u9ad8\uff0c\u6240\u4ee5\u901a\u8fc7\u8ba1\u7b97\u6587\u7ae0\u4e2d\u5404\u4e2a\u8bcd\u7684TF-IDF\uff0c\u7531\u5c0f\u5230\u5927\u6392\u5e8f\uff0c\u6392\u5728\u6700\u524d\u9762\u7684\u51e0\u4e2a\u8bcd\u5c31\u662f\u8be5\u6587\u7ae0\u7684\u5173\u952e\u8bcd\u3002<\/p>\n<h1>TF-IDF\u7b97\u6cd5\u6b65\u9aa4<\/h1>\n<ul>\n<li>\u8bcd\u9891\u8ba1\u7b97\u4e0e\u6807\u51c6\u5316\uff1a<br \/>\n<img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2024\/03\/2.png\" alt=\"\" \/><\/li>\n<li>\u8ba1\u7b97\u9006\u6587\u6863\u9891\u7387\uff1a\u6b64\u65f6\u9700\u8981\u4e00\u4e2a\u8bed\u6599\u5e93\uff08corpus\uff09\u7528\u6765\u6a21\u62df\u8bed\u8a00\u7684\u4f7f\u7528\u73af\u5883\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-![](https:\/\/lingbo.online\/wp-content\/uploads\/2024\/03\/4.png)content\/uploads\/2024\/03\/3.png\" alt=\"\" \/><br \/>\n\u5982\u679c\u4e00\u4e2a\u8bcd\u8d8a\u5e38\u89c1\uff0c\u90a3\u4e48\u5206\u6bcd\u5c31\u8d8a\u5927\uff0c\u9006\u6587\u6863\u9891\u7387\u5c31\u8d8a\u5c0f\u8d8a\u63a5\u8fd10\u3002\u5206\u6bcd\u4e4b\u6240\u4ee5\u8981\u52a01\uff0c\u662f\u4e3a\u4e86\u907f\u514d\u5206\u6bcd\u4e3a0\uff08\u5373\u6240\u6709\u6587\u6863\u90fd\u4e0d\u5305\u542b\u8be5\u8bcd\uff09\u3002<span class=\"katex-eq\" data-katex-display=\"false\">log<\/span>\u8868\u793a\u5bf9\u5f97\u5230\u7684\u503c\u53d6\u5bf9\u6570\u3002<\/li>\n<li>\u7b2c\u4e09\u6b65\uff0c\u8ba1\u7b97TF-IDF\uff1a<br \/>\n<img decoding=\"async\" src=\"https:\/\/lingbo.online\/wp-content\/uploads\/2024\/03\/4.png\" alt=\"\" \/><br \/>\n\u53ef\u4ee5\u770b\u5230\uff0cTF-IDF\u4e0e\u4e00\u4e2a\u8bcd\u5728\u6587\u6863\u4e2d\u7684\u51fa\u73b0\u6b21\u6570\u6210\u6b63\u6bd4\uff0c\u4e0e\u8be5\u8bcd\u5728\u6574\u4e2a\u8bed\u8a00\u4e2d\u7684\u51fa\u73b0\u6b21\u6570\u6210\u53cd\u6bd4\u3002\u6240\u4ee5\uff0c\u81ea\u52a8\u63d0\u53d6\u5173\u952e\u8bcd\u7684\u7b97\u6cd5\u5c31\u662f\u8ba1\u7b97\u51fa\u6587\u6863\u4e2d\u6bcf\u4e2a\u8bcd\u7684TF-IDF\u503c\uff0c\u7136\u540e\u6309\u964d\u5e8f\u6392\u5217\uff0c\u53d6\u6392\u5728\u6700\u524d\u9762\u7684\u51e0\u4e2a\u8bcd\u3002<\/li>\n<\/ul>\n<h1>\u4f18\u7f3a\u70b9<\/h1>\n<p>TF-IDF\u7684\u4f18\u70b9\u662f\u7b80\u5355\u5feb\u901f\uff0c\u800c\u4e14\u5bb9\u6613\u7406\u89e3\u3002\u7f3a\u70b9\u662f\u6709\u65f6\u5019\u7528\u8bcd\u9891\u6765\u8861\u91cf\u6587\u7ae0\u4e2d\u7684\u4e00\u4e2a\u8bcd\u7684\u91cd\u8981\u6027\u4e0d\u591f\u5168\u9762\uff0c\u6709\u65f6\u5019\u91cd\u8981\u7684\u8bcd\u51fa\u73b0\u7684\u53ef\u80fd\u4e0d\u591f\u591a\uff0c\u800c\u4e14\u8fd9\u79cd\u8ba1\u7b97\u65e0\u6cd5\u4f53\u73b0\u4f4d\u7f6e\u4fe1\u606f\uff0c\u65e0\u6cd5\u4f53\u73b0\u8bcd\u5728\u4e0a\u4e0b\u6587\u7684\u91cd\u8981\u6027\u3002\u5982\u679c\u8981\u4f53\u73b0\u8bcd\u7684\u4e0a\u4e0b\u6587\u7ed3\u6784\uff0c\u90a3\u4e48\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528word2vec\u7b97\u6cd5\u6765\u652f\u6301\u3002<\/p>\n<h1>\u793a\u4f8b\u4ee3\u7801<\/h1>\n<pre><code class=\"language-python\">def TF-IDF(path,dic):\n    tf-idf={}\n    fold_list=os.listdir(path)\n    count=0\n    idf={}\n    for key in dic.keys():\n        idf[key] = 1\n        tf_idf[key] = 0\n\n        for fold in fold_list:\n            file_list=os.listdir(path+&#039;\/&#039;+fold)\n            count+=len(file_list)\n            for file in file_list:\n                with open(path+os.sep+fold+os.sep+file) as f:\n                    text=f.read()\n                    for key in dic.keys():\n                        if key in text:\n                            idf[key]+=1\n        for key,value in idf.items():\n            idf[key]=log(count\/value+1)\n        for key,value in tf_idf.items():\n            tf_idf[key]=dic[key]*idf[key]\n        return tf_idf<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb TF-IDF(term frequency\u2013inverse document frequency)\u662f\u4e00\u79cd\u7528\u4e8e\u4fe1\u606f\u68c0\u7d22\u4e0e\u6570\u636e\u6316 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[45,2],"tags":[],"class_list":["post-705","post","type-post","status-publish","format-standard","hentry","category-llms","category-algorithm_learning"],"_links":{"self":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/comments?post=705"}],"version-history":[{"count":1,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/705\/revisions"}],"predecessor-version":[{"id":710,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/posts\/705\/revisions\/710"}],"wp:attachment":[{"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/media?parent=705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/categories?post=705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lingbo.online\/index.php\/wp-json\/wp\/v2\/tags?post=705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}