{"id":1011788,"date":"2024-12-27T11:33:07","date_gmt":"2024-12-27T03:33:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1011788.html"},"modified":"2024-12-27T11:33:10","modified_gmt":"2024-12-27T03:33:10","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97%e4%bd%99%e5%bc%a6%e8%b7%9d%e7%a6%bb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1011788.html","title":{"rendered":"python\u5982\u4f55\u8ba1\u7b97\u4f59\u5f26\u8ddd\u79bb"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085807\/ef777210-0c2e-4102-a69b-ef68d5c69d2d.webp\" alt=\"python\u5982\u4f55\u8ba1\u7b97\u4f59\u5f26\u8ddd\u79bb\" \/><\/p>\n<p><p> <strong>Python\u8ba1\u7b97\u4f59\u5f26\u8ddd\u79bb\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u5e93\u3001\u7b2c\u4e09\u65b9\u5e93\u7b49\u3002\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Scipy\u5e93\u4e2d\u7684<code>spatial.distance.cosine<\/code>\u51fd\u6570\u3001Numpy\u5e93\u7684\u5411\u91cf\u64cd\u4f5c\u548c\u81ea\u5b9a\u4e49\u51fd\u6570\u3002\u63a8\u8350\u4f7f\u7528Scipy\u5e93\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7b80\u6d01\u4e14\u9ad8\u6548\u7684\u8ba1\u7b97\u65b9\u6cd5\u3002<\/strong><\/p>\n<\/p>\n<p><p>Python\u4e2dScipy\u5e93\u7684<code>spatial.distance.cosine<\/code>\u51fd\u6570\u662f\u8ba1\u7b97\u4f59\u5f26\u8ddd\u79bb\u7684\u6807\u51c6\u65b9\u6cd5\u4e4b\u4e00\u3002\u4f59\u5f26\u8ddd\u79bb\u662f\u901a\u8fc7\u8ba1\u7b97\u4e24\u4e2a\u5411\u91cf\u95f4\u7684\u4f59\u5f26\u76f8\u4f3c\u5ea6\uff0c\u7136\u540e\u75281\u51cf\u53bb\u4f59\u5f26\u76f8\u4f3c\u5ea6\u5f97\u5230\u7684\u3002\u4f59\u5f26\u76f8\u4f3c\u5ea6\u672c\u8d28\u4e0a\u662f\u4e24\u4e2a\u5411\u91cf\u5728\u5411\u91cf\u7a7a\u95f4\u4e2d\u7684\u5939\u89d2\u7684\u4f59\u5f26\u503c\uff0c\u5176\u503c\u8303\u56f4\u5728[-1,1]\u4e4b\u95f4\uff0c\u800c\u4f59\u5f26\u8ddd\u79bb\u662f\u4e00\u4e2a\u975e\u8d1f\u503c\uff0c\u5728[0,2]\u4e4b\u95f4\uff0c\u7528\u4e8e\u5ea6\u91cf\u4e24\u4e2a\u5411\u91cf\u7684\u65b9\u5411\u76f8\u4f3c\u7a0b\u5ea6\u3002Scipy\u5e93\u7684\u5b9e\u73b0\u53ef\u4ee5\u907f\u514d\u624b\u52a8\u5904\u7406\u5411\u91cf\u7684\u89c4\u683c\u5316\u548c\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\u7684\u590d\u6742\u6027\uff0c\u786e\u4fdd\u8ba1\u7b97\u7684\u51c6\u786e\u6027\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f59\u5f26\u8ddd\u79bb\u7684\u57fa\u672c\u6982\u5ff5<\/p>\n<\/p>\n<p><p>\u4f59\u5f26\u8ddd\u79bb\u662f\u7528\u4e8e\u8861\u91cf\u4e24\u4e2a\u5411\u91cf\u4e4b\u95f4\u65b9\u5411\u5dee\u5f02\u7684\u4e00\u79cd\u5ea6\u91cf\u65b9\u6cd5\u3002\u4e0e\u6b27\u6c0f\u8ddd\u79bb\u4e0d\u540c\uff0c\u4f59\u5f26\u8ddd\u79bb\u53ea\u5173\u6ce8\u5411\u91cf\u95f4\u7684\u65b9\u5411\u6027\u800c\u5ffd\u7565\u5176\u5927\u5c0f\u3002\u5176\u5e94\u7528\u5e7f\u6cdb\uff0c\u5c24\u5176\u662f\u5728\u6587\u672c\u5206\u6790\u548c\u81ea\u7136\u8bed\u8a00\u5904\u7406\u9886\u57df\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4f59\u5f26\u76f8\u4f3c\u5ea6<\/strong><\/p>\n<\/p>\n<p><p>\u4f59\u5f26\u76f8\u4f3c\u5ea6\u5b9a\u4e49\u4e3a\u4e24\u4e2a\u975e\u96f6\u5411\u91cf\u4e4b\u95f4\u7684\u5939\u89d2\u7684\u4f59\u5f26\u503c\uff0c\u5176\u516c\u5f0f\u4e3a\uff1a<\/p>\n<\/p>\n<p><p>[<\/p>\n<p>\\text{Cosine Similarity} = \\frac{A \\cdot B}{||A|| \\times ||B||}<\/p>\n<p>]<\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c(A)\u548c(B)\u662f\u4e24\u4e2a\u5411\u91cf\uff0c(\\cdot)\u8868\u793a\u5411\u91cf\u70b9\u79ef\uff0c(||A||)\u548c(||B||)\u8868\u793a\u5411\u91cf\u7684\u8303\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f59\u5f26\u8ddd\u79bb<\/strong><\/p>\n<\/p>\n<p><p>\u4f59\u5f26\u8ddd\u79bb\u5219\u662f\u901a\u8fc7\u4ee5\u4e0b\u516c\u5f0f\u8ba1\u7b97\u5f97\u51fa\u7684\uff1a<\/p>\n<\/p>\n<p><p>[<\/p>\n<p>\\text{Cosine Distance} = 1 &#8211; \\text{Cosine Similarity}<\/p>\n<p>]<\/p>\n<\/p>\n<p><p>\u8fd9\u6837\u8ba1\u7b97\u51fa\u6765\u7684\u8ddd\u79bb\u503c\u57280\u52302\u4e4b\u95f4\uff0c0\u8868\u793a\u5b8c\u5168\u76f8\u4f3c\uff0c2\u8868\u793a\u5b8c\u5168\u4e0d\u76f8\u4f3c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528Scipy\u5e93\u8ba1\u7b97\u4f59\u5f26\u8ddd\u79bb<\/p>\n<\/p>\n<p><p>Scipy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u5b66\u548c\u79d1\u5b66\u8ba1\u7b97\u529f\u80fd\u3002\u5728Scipy\u4e2d\uff0c<code>spatial.distance<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u8ba1\u7b97\u591a\u79cd\u8ddd\u79bb\u7684\u65b9\u6cd5\uff0c\u5176\u4e2d\u5305\u62ec\u4f59\u5f26\u8ddd\u79bb\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Scipy\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u5c1a\u672a\u5b89\u88c5Scipy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scipy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528<code>spatial.distance.cosine<\/code>\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7<code>spatial.distance.cosine<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u8ba1\u7b97\u4e24\u4e2a\u5411\u91cf\u4e4b\u95f4\u7684\u4f59\u5f26\u8ddd\u79bb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.spatial import distance<\/p>\n<p>vector_a = [1, 2, 3]<\/p>\n<p>vector_b = [4, 5, 6]<\/p>\n<p>cosine_dist = distance.cosine(vector_a, vector_b)<\/p>\n<p>print(&quot;Cosine Distance:&quot;, cosine_dist)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u5411\u91cf\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5b83\u4eec\u4e4b\u95f4\u7684\u4f59\u5f26\u8ddd\u79bb\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528Numpy\u5e93\u8ba1\u7b97\u4f59\u5f26\u8ddd\u79bb<\/p>\n<\/p>\n<p><p>Numpy\u662fPython\u7684\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u6570\u7ec4\u548c\u77e9\u9635\u64cd\u4f5c\u3002\u5c3d\u7ba1Numpy\u6ca1\u6709\u76f4\u63a5\u63d0\u4f9b\u4f59\u5f26\u8ddd\u79bb\u7684\u8ba1\u7b97\u51fd\u6570\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u5411\u91cf\u64cd\u4f5c\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Numpy\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u5c1a\u672a\u5b89\u88c5Numpy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u901a\u8fc7\u5411\u91cf\u64cd\u4f5c\u8ba1\u7b97\u4f59\u5f26\u8ddd\u79bb<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Numpy\u7684\u5411\u91cf\u64cd\u4f5c\uff0c\u53ef\u4ee5\u624b\u52a8\u8ba1\u7b97\u4f59\u5f26\u76f8\u4f3c\u5ea6\u548c\u4f59\u5f26\u8ddd\u79bb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>vector_a = np.array([1, 2, 3])<\/p>\n<p>vector_b = np.array([4, 5, 6])<\/p>\n<p>dot_product = np.dot(vector_a, vector_b)<\/p>\n<p>norm_a = np.linalg.norm(vector_a)<\/p>\n<p>norm_b = np.linalg.norm(vector_b)<\/p>\n<p>cosine_similarity = dot_product \/ (norm_a * norm_b)<\/p>\n<p>cosine_distance = 1 - cosine_similarity<\/p>\n<p>print(&quot;Cosine Distance:&quot;, cosine_distance)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u9700\u8981\u624b\u52a8\u8ba1\u7b97\u70b9\u79ef\u548c\u8303\u6570\uff0c\u4f46\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u4f59\u5f26\u8ddd\u79bb\u7684\u8ba1\u7b97\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u81ea\u5b9a\u4e49\u51fd\u6570\u8ba1\u7b97\u4f59\u5f26\u8ddd\u79bb<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528Scipy\u548cNumpy\u5e93\uff0c\u7528\u6237\u4e5f\u53ef\u4ee5\u7f16\u5199\u81ea\u5b9a\u4e49\u51fd\u6570\u6765\u8ba1\u7b97\u4f59\u5f26\u8ddd\u79bb\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u63d0\u4f9b\u66f4\u5927\u7684\u7075\u6d3b\u6027\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u5bf9\u8ba1\u7b97\u8fc7\u7a0b\u8fdb\u884c\u7279\u5b9a\u8c03\u6574\u65f6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u81ea\u5b9a\u4e49\u4f59\u5f26\u8ddd\u79bb\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u81ea\u5b9a\u4e49\u51fd\u6570\u53ef\u4ee5\u5229\u7528\u6807\u51c6Python\u5e93\u5b9e\u73b0\u4f59\u5f26\u8ddd\u79bb\u7684\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def cosine_distance(vector_a, vector_b):<\/p>\n<p>    dot_product = sum(a * b for a, b in zip(vector_a, vector_b))<\/p>\n<p>    norm_a = sum(a * a for a in vector_a)  0.5<\/p>\n<p>    norm_b = sum(b * b for b in vector_b)  0.5<\/p>\n<p>    cosine_similarity = dot_product \/ (norm_a * norm_b)<\/p>\n<p>    return 1 - cosine_similarity<\/p>\n<p>vector_a = [1, 2, 3]<\/p>\n<p>vector_b = [4, 5, 6]<\/p>\n<p>print(&quot;Cosine Distance:&quot;, cosine_distance(vector_a, vector_b))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u51fd\u6570\u901a\u8fc7\u70b9\u79ef\u548c\u8303\u6570\u7684\u8ba1\u7b97\u5b9e\u73b0\u4f59\u5f26\u76f8\u4f3c\u5ea6\uff0c\u7136\u540e\u901a\u8fc71\u51cf\u53bb\u76f8\u4f3c\u5ea6\u5f97\u5230\u4f59\u5f26\u8ddd\u79bb\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u5e94\u7528\u573a\u666f\u4e0e\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u4f59\u5f26\u8ddd\u79bb\u5728\u8bb8\u591a\u9886\u57df\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u5c24\u5176\u662f\u5728\u6587\u672c\u76f8\u4f3c\u5ea6\u5206\u6790\u3001\u56fe\u50cf\u5904\u7406\u548c\u63a8\u8350\u7cfb\u7edf\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e94\u7528\u573a\u666f\u548c\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6587\u672c\u76f8\u4f3c\u5ea6\u5206\u6790<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6587\u672c\u5206\u6790\u4e2d\uff0c\u6587\u672c\u901a\u5e38\u88ab\u8868\u793a\u4e3a\u5411\u91cf\uff08\u5982\u8bcd\u888b\u6a21\u578b\uff09\u3002\u4f59\u5f26\u8ddd\u79bb\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u4e0d\u540c\u6587\u672c\u4e4b\u95f4\u7684\u76f8\u4f3c\u6027\uff0c\u4ece\u800c\u5b9e\u73b0\u6587\u672c\u5206\u7c7b\u548c\u805a\u7c7b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u56fe\u50cf\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0c\u56fe\u50cf\u901a\u5e38\u88ab\u8868\u793a\u4e3a\u7279\u5f81\u5411\u91cf\u3002\u4f59\u5f26\u8ddd\u79bb\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u56fe\u50cf\u7279\u5f81\u5411\u91cf\u4e4b\u95f4\u7684\u76f8\u4f3c\u6027\uff0c\u4ece\u800c\u5b9e\u73b0\u56fe\u50cf\u8bc6\u522b\u548c\u5206\u7c7b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u63a8\u8350\u7cfb\u7edf<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u63a8\u8350\u7cfb\u7edf\u4e2d\uff0c\u7528\u6237\u7684\u504f\u597d\u548c\u7269\u54c1\u7684\u7279\u5f81\u901a\u5e38\u88ab\u8868\u793a\u4e3a\u5411\u91cf\u3002\u901a\u8fc7\u8ba1\u7b97\u4f59\u5f26\u8ddd\u79bb\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7528\u6237\u4e0e\u7269\u54c1\u4e4b\u95f4\u7684\u5339\u914d\uff0c\u4ece\u800c\u8fdb\u884c\u4e2a\u6027\u5316\u63a8\u8350\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u5411\u91cf\u89c4\u683c\u5316<\/strong>\uff1a\u5728\u8ba1\u7b97\u4f59\u5f26\u8ddd\u79bb\u65f6\uff0c\u786e\u4fdd\u5411\u91cf\u5df2\u8fdb\u884c\u89c4\u683c\u5316\u5904\u7406\uff0c\u4ee5\u907f\u514d\u56e0\u91cf\u7ea7\u4e0d\u540c\u5bfc\u81f4\u7684\u8bef\u5dee\u3002<\/li>\n<li><strong>\u7f3a\u5931\u503c\u5904\u7406<\/strong>\uff1a\u5728\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u5411\u91cf\u4e2d\u5b58\u5728\u7f3a\u5931\u503c\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u8ba1\u7b97\u7ed3\u679c\u3002\u9700\u8981\u8fdb\u884c\u9002\u5f53\u7684\u7f3a\u5931\u503c\u5904\u7406\u3002<\/li>\n<li><strong>\u5411\u91cf\u7ef4\u5ea6\u4e00\u81f4\u6027<\/strong>\uff1a\u786e\u4fdd\u53c2\u4e0e\u8ba1\u7b97\u7684\u5411\u91cf\u7ef4\u5ea6\u4e00\u81f4\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u8ba1\u7b97\u9519\u8bef\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\uff0c\u6211\u4eec\u53ef\u4ee5\u6e05\u6670\u5730\u4e86\u89e3\u5230\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684\u65b9\u6cd5\u8ba1\u7b97\u4f59\u5f26\u8ddd\u79bb\uff0c\u5e76\u4e86\u89e3\u5176\u5728\u4e0d\u540c\u9886\u57df\u7684\u5e94\u7528\u53ca\u6ce8\u610f\u4e8b\u9879\u3002\u8fd9\u4e9b\u77e5\u8bc6\u5c06\u5e2e\u52a9\u6211\u4eec\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u597d\u5730\u5e94\u7528\u4f59\u5f26\u8ddd\u79bb\u8fd9\u4e00\u91cd\u8981\u7684\u5ea6\u91cf\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528NumPy\u5e93\u8ba1\u7b97\u4f59\u5f26\u8ddd\u79bb\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u8ba1\u7b97\u4f59\u5f26\u8ddd\u79bb\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86NumPy\u5e93\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u8fdb\u884c\u8ba1\u7b97\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\ndef cosine_distance(a, b):\n    return 1 - np.dot(a, b) \/ (np.linalg.norm(a) * np.linalg.norm(b))\n\nvector1 = np.array([1, 2, 3])\nvector2 = np.array([4, 5, 6])\ndistance = cosine_distance(vector1, vector2)\nprint(distance)\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570\u6765\u8ba1\u7b97\u4e24\u5411\u91cf\u4e4b\u95f4\u7684\u4f59\u5f26\u8ddd\u79bb\uff0c\u5e76\u4f7f\u7528\u793a\u4f8b\u5411\u91cf\u8fdb\u884c\u6f14\u793a\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u4f7f\u7528SciPy\u5e93\u8fdb\u884c\u4f59\u5f26\u8ddd\u79bb\u8ba1\u7b97\u7684\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>SciPy\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5185\u7f6e\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8ba1\u7b97\u4f59\u5f26\u8ddd\u79bb\u3002\u4f7f\u7528SciPy\u7684\u597d\u5904\u5728\u4e8e\u5176\u4f18\u5316\u548c\u9ad8\u6548\u7684\u5b9e\u73b0\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u6765\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-python\">from scipy.spatial.distance import cosine\n\nvector1 = [1, 2, 3]\nvector2 = [4, 5, 6]\ndistance = cosine(vector1, vector2)\nprint(distance)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u8fd4\u56de\u4e24\u4e2a\u5411\u91cf\u4e4b\u95f4\u7684\u4f59\u5f26\u8ddd\u79bb\uff0c\u4f7f\u7528SciPy\u5e93\u7684\u5b9e\u73b0\u80fd\u66f4\u5feb\u5730\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7f3a\u5931\u6570\u636e\u5bf9\u4f59\u5f26\u8ddd\u79bb\u8ba1\u7b97\u7684\u5f71\u54cd\uff1f<\/strong><br \/>\u5728\u8ba1\u7b97\u4f59\u5f26\u8ddd\u79bb\u65f6\uff0c\u7f3a\u5931\u6570\u636e\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8ba1\u7b97\u7ed3\u679c\u7684\u4e0d\u51c6\u786e\u3002\u5982\u679c\u4f60\u7684\u6570\u636e\u96c6\u5b58\u5728\u7f3a\u5931\u503c\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u586b\u5145\u65b9\u6cd5\uff08\u5982\u5747\u503c\u586b\u5145\uff09\u6216\u8005\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u6837\u672c\u3002\u786e\u4fdd\u5728\u8ba1\u7b97\u524d\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\uff0c\u4ee5\u63d0\u9ad8\u8ba1\u7b97\u7684\u51c6\u786e\u6027\u548c\u6709\u6548\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8ba1\u7b97\u4f59\u5f26\u8ddd\u79bb\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u5e93\u3001\u7b2c\u4e09\u65b9\u5e93\u7b49\u3002\u6838\u5fc3\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Scipy\u5e93\u4e2d\u7684spati [&hellip;]","protected":false},"author":3,"featured_media":1011802,"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\/1011788"}],"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=1011788"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011788\/revisions"}],"predecessor-version":[{"id":1011806,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1011788\/revisions\/1011806"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1011802"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1011788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1011788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1011788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}