{"id":2528,"date":"2025-06-25T10:36:50","date_gmt":"2025-06-25T02:36:50","guid":{"rendered":"https:\/\/sanlangcode.com\/?p=2528"},"modified":"2025-06-25T10:36:50","modified_gmt":"2025-06-25T02:36:50","slug":"%e4%bd%99%e5%bc%a6%e7%9b%b8%e4%bc%bc%e5%ba%a6%ef%bc%88cosine-similarity%ef%bc%89%e5%92%8cl2%e8%b7%9d%e7%a6%bb%ef%bc%88%e6%ac%a7%e6%b0%8f%e8%b7%9d%e7%a6%bb%ef%bc%8cl2-distance-euclidean-distance","status":"publish","type":"post","link":"https:\/\/sanlangcode.com\/index.php\/2025\/06\/25\/%e4%bd%99%e5%bc%a6%e7%9b%b8%e4%bc%bc%e5%ba%a6%ef%bc%88cosine-similarity%ef%bc%89%e5%92%8cl2%e8%b7%9d%e7%a6%bb%ef%bc%88%e6%ac%a7%e6%b0%8f%e8%b7%9d%e7%a6%bb%ef%bc%8cl2-distance-euclidean-distance\/","title":{"rendered":"\u4f59\u5f26\u76f8\u4f3c\u5ea6\uff08Cosine Similarity\uff09\u548cL2\u8ddd\u79bb\uff08\u6b27\u6c0f\u8ddd\u79bb\uff0cL2 Distance\/Euclidean Distance\uff09"},"content":{"rendered":"\n<p><strong>\u4f59\u5f26\u76f8\u4f3c\u5ea6\uff08Cosine Similarity\uff09<\/strong>\u548c<strong>L2\u8ddd\u79bb\uff08\u6b27\u6c0f\u8ddd\u79bb\uff0cL2 Distance\/Euclidean Distance\uff09<\/strong>\uff0c\u5e76\u5bf9\u6bd4\u5b83\u4eec\u7684\u9002\u7528\u573a\u666f\u548c\u6570\u5b66\u539f\u7406\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. \u4f59\u5f26\u76f8\u4f3c\u5ea6\uff08Cosine Similarity\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u6570\u5b66\u5b9a\u4e49<\/h3>\n\n\n\n<p>\u4f59\u5f26\u76f8\u4f3c\u5ea6\u8861\u91cf\u7684\u662f\u4e24\u4e2a\u5411\u91cf\u5728\u5411\u91cf\u7a7a\u95f4\u4e2d\u65b9\u5411\u7684\u76f8\u4f3c\u7a0b\u5ea6\uff0c\u800c<strong>\u4e0d\u5173\u5fc3\u5b83\u4eec\u7684\u957f\u5ea6<\/strong>\u3002\u5176\u503c\u57df\u4e3a ([-1, 1])\uff08\u5927\u591a\u6570\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5411\u91cf\u4e3a\u975e\u8d1f\uff0c\u503c\u57df\u4e3a ([0, 1])\uff09\u3002<\/p>\n\n\n\n<p>\u516c\u5f0f\u5982\u4e0b\uff1a<br>[<br>\\text{Cosine Similarity}(\\mathbf{A}, \\mathbf{B}) = \\frac{\\mathbf{A} \\cdot \\mathbf{B}}{|\\mathbf{A}| \\cdot |\\mathbf{B}|}<br>]<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>(\\mathbf{A} \\cdot \\mathbf{B}) \u662f\u5411\u91cf\u7684\u70b9\u79ef<\/li>\n\n\n\n<li>(|\\mathbf{A}|) \u548c (|\\mathbf{B}|) \u662f\u5411\u91cf\u7684L2\u8303\u6570\uff08\u5373\u957f\u5ea6\uff09<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u76f4\u89c2\u7406\u89e3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53d6\u503c\u4e3a1\uff1a\u4e24\u4e2a\u5411\u91cf\u65b9\u5411\u5b8c\u5168\u4e00\u81f4\uff08\u5939\u89d20\u00b0\uff09<\/li>\n\n\n\n<li>\u53d6\u503c\u4e3a0\uff1a\u4e24\u4e2a\u5411\u91cf\u6b63\u4ea4\uff08\u5939\u89d290\u00b0\uff0c\u5b8c\u5168\u65e0\u5173\uff09<\/li>\n\n\n\n<li>\u53d6\u503c\u4e3a-1\uff1a\u4e24\u4e2a\u5411\u91cf\u65b9\u5411\u5b8c\u5168\u76f8\u53cd\uff08\u5939\u89d2180\u00b0\uff09<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u9002\u7528\u573a\u666f<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6587\u672c\u76f8\u4f3c\u5ea6\uff08\u5982TF-IDF\u3001embedding\u7b49\uff09<\/li>\n\n\n\n<li>\u53ea\u5173\u5fc3\u201c\u5185\u5bb9\u201d\u800c\u4e0d\u5173\u5fc3\u201c\u91cf\u7ea7\u201d\u7684\u573a\u666f<\/li>\n\n\n\n<li>\u9ad8\u7ef4\u7a00\u758f\u5411\u91cf\uff08\u5982NLP\u4e2d\u7684\u8bcd\u888b\u6a21\u578b\uff09<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u70b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bf9\u5411\u91cf\u7684\u957f\u5ea6\u4e0d\u654f\u611f\uff0c\u53ea\u5173\u6ce8\u65b9\u5411<\/li>\n\n\n\n<li>\u9002\u5408\u5f52\u4e00\u5316\u540e\u7684\u5411\u91cf<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. L2\u8ddd\u79bb\uff08\u6b27\u6c0f\u8ddd\u79bb\uff0cL2 Distance\/Euclidean Distance\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u6570\u5b66\u5b9a\u4e49<\/h3>\n\n\n\n<p>L2\u8ddd\u79bb\u8861\u91cf\u7684\u662f\u4e24\u4e2a\u70b9\u5728\u7a7a\u95f4\u4e2d\u7684\u201c\u76f4\u7ebf\u8ddd\u79bb\u201d\uff0c\u5373\u201c\u6700\u77ed\u8def\u5f84\u201d\u3002\u5176\u503c\u57df\u4e3a ([0, +\\infty))\u3002<\/p>\n\n\n\n<p>\u516c\u5f0f\u5982\u4e0b\uff1a<br>[<br>\\text{L2 Distance}(\\mathbf{A}, \\mathbf{B}) = |\\mathbf{A} &#8211; \\mathbf{B}|_2 = \\sqrt{\\sum_{i=1}^n (A_i &#8211; B_i)^2}<br>]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u76f4\u89c2\u7406\u89e3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8ddd\u79bb\u8d8a\u5c0f\uff0c\u4e24\u4e2a\u5411\u91cf\u8d8a\u63a5\u8fd1<\/li>\n\n\n\n<li>\u8ddd\u79bb\u4e3a0\uff0c\u8868\u793a\u4e24\u4e2a\u5411\u91cf\u5b8c\u5168\u91cd\u5408<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u9002\u7528\u573a\u666f<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u56fe\u50cf\u68c0\u7d22\u3001\u7269\u7406\u7a7a\u95f4\u8ddd\u79bb<\/li>\n\n\n\n<li>\u5411\u91cf\u7684\u201c\u91cf\u7ea7\u201d\u6709\u5b9e\u9645\u610f\u4e49\u7684\u573a\u666f<\/li>\n\n\n\n<li>\u5411\u91cf\u6ca1\u6709\u5f52\u4e00\u5316\uff0c\u4e14\u957f\u5ea6\u6709\u610f\u4e49<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f18\u70b9<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u76f4\u89c2\u3001\u6613\u4e8e\u7406\u89e3<\/li>\n\n\n\n<li>\u9002\u5408\u5ea6\u91cf\u201c\u7edd\u5bf9\u5dee\u5f02\u201d<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u5bf9\u6bd4\u4e0e\u9009\u62e9<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>\u4f59\u5f26\u76f8\u4f3c\u5ea6<\/th><th>L2\u8ddd\u79bb\uff08\u6b27\u6c0f\u8ddd\u79bb\uff09<\/th><\/tr><\/thead><tbody><tr><td>\u5173\u6ce8\u70b9<\/td><td>\u65b9\u5411\uff08\u5939\u89d2\uff09<\/td><td>\u7edd\u5bf9\u8ddd\u79bb\uff08\u957f\u5ea6\u5dee\u5f02\uff09<\/td><\/tr><tr><td>\u662f\u5426\u5f52\u4e00\u5316<\/td><td>\u901a\u5e38\u9700\u8981<\/td><td>\u4e0d\u9700\u8981<\/td><\/tr><tr><td>\u53d6\u503c\u8303\u56f4<\/td><td>([-1, 1])\uff08\u5e38\u75280~1\uff09<\/td><td>([0, +\\infty))<\/td><\/tr><tr><td>\u9002\u5408\u573a\u666f<\/td><td>\u6587\u672c\u3001\u8bed\u4e49\u3001\u5f52\u4e00\u5316\u5411\u91cf<\/td><td>\u56fe\u50cf\u3001\u7269\u7406\u7a7a\u95f4\u3001\u539f\u59cb\u5411\u91cf<\/td><\/tr><tr><td>\u957f\u5ea6\u654f\u611f\u6027<\/td><td>\u4e0d\u654f\u611f<\/td><td>\u654f\u611f<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u4ee3\u7801\u4e3e\u4f8b<\/h2>\n\n\n\n<p>\u5047\u8bbe\u6709\u4e24\u4e2a\u5411\u91cfA\u548cB\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\n\nA = np.array(&#91;1, 2, 3])\nB = np.array(&#91;4, 5, 6])\n\n# \u4f59\u5f26\u76f8\u4f3c\u5ea6\ncos_sim = np.dot(A, B) \/ (np.linalg.norm(A) * np.linalg.norm(B))\n\n# L2\u8ddd\u79bb\nl2_dist = np.linalg.norm(A - B)\n\nprint('Cosine Similarity:', cos_sim)\nprint('L2 Distance:', l2_dist)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u603b\u7ed3<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f59\u5f26\u76f8\u4f3c\u5ea6<\/strong>\uff1a\u9002\u5408\u8861\u91cf\u201c\u65b9\u5411\u201d\u76f8\u4f3c\u6027\uff0c\u5e38\u7528\u4e8e\u6587\u672c\u3001\u8bed\u4e49\u68c0\u7d22\u3002<\/li>\n\n\n\n<li><strong>L2\u8ddd\u79bb<\/strong>\uff1a\u9002\u5408\u8861\u91cf\u201c\u7edd\u5bf9\u8ddd\u79bb\u201d\uff0c\u5e38\u7528\u4e8e\u56fe\u50cf\u3001\u7269\u7406\u7a7a\u95f4\u7b49\u573a\u666f\u3002<\/li>\n\n\n\n<li><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4f59\u5f26\u76f8\u4f3c\u5ea6\uff08Cosine Sim&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-2528","post","type-post","status-publish","format-standard","hentry","category-4"],"_links":{"self":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/2528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/comments?post=2528"}],"version-history":[{"count":1,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/2528\/revisions"}],"predecessor-version":[{"id":2529,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/posts\/2528\/revisions\/2529"}],"wp:attachment":[{"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/media?parent=2528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/categories?post=2528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sanlangcode.com\/index.php\/wp-json\/wp\/v2\/tags?post=2528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}