{"id":1007504,"date":"2024-12-27T10:54:10","date_gmt":"2024-12-27T02:54:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1007504.html"},"modified":"2024-12-27T10:54:13","modified_gmt":"2024-12-27T02:54:13","slug":"python%e7%9a%84pyemd%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1007504.html","title":{"rendered":"python\u7684pyemd\u5982\u4f55\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083209\/92a33280-7ffc-4e51-a2c6-8fb7905c8e89.webp\" alt=\"python\u7684pyemd\u5982\u4f55\u4f7f\u7528\" \/><\/p>\n<p><p> <strong>Pyemd\u662f\u4e00\u4e2a\u7528\u4e8e\u8ba1\u7b97\u5730\u7403\u79fb\u52a8\u8ddd\u79bb\uff08Earth Mover&#39;s Distance\uff0cEMD\uff09\u7684Python\u5e93\u3002\u8981\u4f7f\u7528Pyemd\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff0c\u5176\u6b21\u8981\u7406\u89e3\u5982\u4f55\u51c6\u5907\u548c\u8f93\u5165\u6570\u636e\u3002\u4ee5\u4e0b\u662fPyemd\u7684\u57fa\u672c\u4f7f\u7528\u6b65\u9aa4\uff1a\u5b89\u88c5Pyemd\u3001\u51c6\u5907\u6570\u636e\u3001\u8ba1\u7b97EMD\u3001\u5e94\u7528\u5b9e\u4f8b\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u4e2a\u6b65\u9aa4\uff1a\u51c6\u5907\u6570\u636e\u3002\u4e3a\u4e86\u8ba1\u7b97EMD\uff0c\u9700\u8981\u5c06\u6570\u636e\u8868\u793a\u4e3a\u76f4\u65b9\u56fe\u6216\u5206\u5e03\u3002\u6bcf\u4e2a\u76f4\u65b9\u56fe\u9700\u8981\u6709\u76f8\u540c\u7684bin\u6570\uff0c\u5e76\u4e14\u9700\u8981\u5b9a\u4e49\u6bcf\u4e2abin\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5Pyemd<\/p>\n<\/p>\n<p><p>\u8981\u4f7f\u7528Pyemd\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u6765\u5b89\u88c5Pyemd\u3002\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyemd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u5df2\u7ecf\u914d\u7f6e\u597dpip\uff0c\u5e76\u4e14\u4f60\u6709\u5408\u9002\u7684\u6743\u9650\u6765\u5b89\u88c5\u8f6f\u4ef6\u5305\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u51c6\u5907\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528Pyemd\u8ba1\u7b97EMD\u4e4b\u524d\uff0c\u4f60\u9700\u8981\u51c6\u5907\u4e24\u4e2a\u8f93\u5165\u76f4\u65b9\u56fe\u6216\u5206\u5e03\uff0c\u4ee5\u53ca\u4e00\u4e2a\u8ddd\u79bb\u77e9\u9635\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u51c6\u5907\u8fd9\u4e9b\u6570\u636e\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8f93\u5165\u76f4\u65b9\u56fe\u6216\u5206\u5e03<\/strong><\/p>\n<\/p>\n<p><p>\u4f60\u9700\u8981\u51c6\u5907\u4e24\u4e2a\u4e00\u7ef4\u6570\u7ec4\uff0c\u8fd9\u4e9b\u6570\u7ec4\u8868\u793a\u5f85\u6bd4\u8f83\u7684\u4e24\u4e2a\u5206\u5e03\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u6982\u7387\u5206\u5e03P\u548cQ\uff0c\u5b83\u4eec\u5206\u522b\u8868\u793a\u4e3a\u6570\u7ec4\u5f62\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">P = [0.1, 0.4, 0.5]<\/p>\n<p>Q = [0.2, 0.3, 0.5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bf7\u6ce8\u610f\uff0cP\u548cQ\u7684\u957f\u5ea6\u5fc5\u987b\u76f8\u540c\uff0c\u5e76\u4e14\u5b83\u4eec\u7684\u548c\u5e94\u8be5\u4e3a1\uff08\u5373\u5b83\u4eec\u662f\u6982\u7387\u5206\u5e03\uff09\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9a\u4e49\u8ddd\u79bb\u77e9\u9635<\/strong><\/p>\n<\/p>\n<p><p>\u8ddd\u79bb\u77e9\u9635\u7528\u4e8e\u63cf\u8ff0\u76f4\u65b9\u56fe\u7684bin\u4e4b\u95f4\u7684\u201c\u8ddd\u79bb\u201d\u3002\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8ddd\u79bb\u53ef\u80fd\u662f\u7269\u7406\u4e0a\u7684\uff0c\u6216\u662f\u4e00\u4e9b\u62bd\u8c61\u7684\u5ea6\u91cf\u3002\u8ddd\u79bb\u77e9\u9635\u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u8868\u793a\u6bcf\u5bf9bin\u4e4b\u95f4\u7684\u8ddd\u79bb\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>distance_matrix = np.array([<\/p>\n<p>    [0.0, 1.0, 2.0],<\/p>\n<p>    [1.0, 0.0, 1.0],<\/p>\n<p>    [2.0, 1.0, 0.0]<\/p>\n<p>])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0cdistance_matrix[i][j]\u8868\u793a\u76f4\u65b9\u56febin i\u548cbin j\u4e4b\u95f4\u7684\u8ddd\u79bb\u3002\u8fd9\u4e2a\u77e9\u9635\u5fc5\u987b\u662f\u5bf9\u79f0\u7684\uff0c\u5e76\u4e14\u5176\u5bf9\u89d2\u7ebf\u5143\u7d20\u5fc5\u987b\u4e3a0\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u8ba1\u7b97EMD<\/p>\n<\/p>\n<p><p>\u4e00\u65e6\u6570\u636e\u51c6\u5907\u5b8c\u6210\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Pyemd\u5e93\u8ba1\u7b97EMD\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u7b97\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pyemd import emd<\/p>\n<p>emd_value = emd(np.array(P, dtype=np.float64), np.array(Q, dtype=np.float64), distance_matrix)<\/p>\n<p>print(&quot;EMD:&quot;, emd_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>emd()<\/code>\u51fd\u6570\u5c06\u8fd4\u56de\u4e24\u4e2a\u5206\u5e03\u4e4b\u95f4\u7684EMD\u503c\uff0c\u8868\u793a\u5c06\u4e00\u4e2a\u5206\u5e03\u8f6c\u6362\u4e3a\u53e6\u4e00\u4e2a\u5206\u5e03\u6240\u9700\u7684\u201c\u5de5\u4f5c\u91cf\u201d\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u5e94\u7528\u5b9e\u4f8b<\/p>\n<\/p>\n<p><p>Pyemd\u7684\u5e94\u7528\u8303\u56f4\u975e\u5e38\u5e7f\u6cdb\uff0c\u7279\u522b\u662f\u5728\u56fe\u50cf\u5904\u7406\u3001\u81ea\u7136\u8bed\u8a00\u5904\u7406\u548c\u5176\u4ed6\u9700\u8981\u6bd4\u8f83\u5206\u5e03\u7684\u9886\u57df\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e94\u7528\u5b9e\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u56fe\u50cf\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u4e2d\uff0cEMD\u53ef\u4ee5\u7528\u4e8e\u6bd4\u8f83\u989c\u8272\u76f4\u65b9\u56fe\u3002\u5047\u8bbe\u4f60\u6709\u4e24\u5e45\u56fe\u50cf\u7684\u989c\u8272\u76f4\u65b9\u56fe\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528EMD\u6765\u8861\u91cf\u5b83\u4eec\u4e4b\u95f4\u7684\u989c\u8272\u76f8\u4f3c\u6027\u3002\u8fd9\u5728\u56fe\u50cf\u68c0\u7d22\u548c\u5206\u7c7b\u4e2d\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6587\u672c\u6bd4\u8f83<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4e2d\uff0cEMD\u53ef\u4ee5\u7528\u4e8e\u6bd4\u8f83\u6587\u672c\u7684\u4e3b\u9898\u5206\u5e03\u3002\u5047\u8bbe\u4f60\u6709\u4e24\u4e2a\u6587\u6863\u7684\u4e3b\u9898\u5206\u5e03\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528EMD\u6765\u8861\u91cf\u5b83\u4eec\u7684\u4e3b\u9898\u76f8\u4f3c\u6027\u3002\u8fd9\u53ef\u4ee5\u5e94\u7528\u4e8e\u6587\u6863\u805a\u7c7b\u548c\u5206\u7c7b\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u65f6\u95f4\u5e8f\u5217\u5206\u6790<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u4e2d\uff0cEMD\u53ef\u4ee5\u7528\u4e8e\u6bd4\u8f83\u65f6\u95f4\u5e8f\u5217\u7684\u5206\u5e03\u3002\u5047\u8bbe\u4f60\u6709\u4e24\u4e2a\u65f6\u95f4\u5e8f\u5217\u7684\u6570\u636e\u5206\u5e03\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528EMD\u6765\u8861\u91cf\u5b83\u4eec\u7684\u76f8\u4f3c\u6027\u3002\u8fd9\u5bf9\u4e8e\u5f02\u5e38\u68c0\u6d4b\u548c\u6a21\u5f0f\u8bc6\u522b\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6027\u80fd<\/strong><\/p>\n<\/p>\n<p><p>\u8ba1\u7b97EMD\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u7279\u522b\u662f\u5f53\u76f4\u65b9\u56fe\u7684bin\u6570\u589e\u52a0\u65f6\u3002\u5bf9\u4e8e\u8f83\u5927\u7684\u6570\u636e\u96c6\uff0c\u53ef\u80fd\u9700\u8981\u4f18\u5316\u8ddd\u79bb\u77e9\u9635\u7684\u8ba1\u7b97\u6216\u4f7f\u7528\u8fd1\u4f3c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u9884\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8ba1\u7b97EMD\u4e4b\u524d\uff0c\u786e\u4fdd\u6570\u636e\u5df2\u7ecf\u6807\u51c6\u5316\uff0c\u5e76\u4e14\u662f\u6982\u7387\u5206\u5e03\u3002\u5426\u5219\uff0c\u7ed3\u679c\u53ef\u80fd\u4e0d\u51c6\u786e\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5e94\u7528\u9650\u5236<\/strong><\/p>\n<\/p>\n<p><p>Pyemd\u4e3b\u8981\u9002\u7528\u4e8e\u4e00\u7ef4\u5206\u5e03\u7684\u6bd4\u8f83\u3002\u5982\u679c\u9700\u8981\u6bd4\u8f83\u591a\u7ef4\u5206\u5e03\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u5176\u4ed6\u5e93\u6216\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u6709\u6548\u5730\u4f7f\u7528Pyemd\u5e93\u6765\u8ba1\u7b97\u5730\u7403\u79fb\u52a8\u8ddd\u79bb\uff0c\u5e2e\u52a9\u4f60\u5728\u4e0d\u540c\u5e94\u7528\u573a\u666f\u4e0b\u8fdb\u884c\u5206\u5e03\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5b89\u88c5pyemd\u5e93\uff1f<\/strong><br \/>\u8981\u4f7f\u7528pyemd\u5e93\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u5b83\u5df2\u7ecf\u5b89\u88c5\u5728\u60a8\u7684Python\u73af\u5883\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a  <\/p>\n<pre><code class=\"language-bash\">pip install pyemd\n<\/code><\/pre>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165\u8be5\u5e93\u5e76\u5f00\u59cb\u4f7f\u7528\u3002<\/p>\n<p><strong>pyemd\u5e93\u7684\u4e3b\u8981\u529f\u80fd\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>pyemd\u5e93\u4e3b\u8981\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a\u5206\u5e03\u4e4b\u95f4\u7684Earth Mover&#39;s Distance\uff08EMD\uff09\u3002\u5b83\u5e7f\u6cdb\u5e94\u7528\u4e8e\u56fe\u50cf\u5904\u7406\u3001\u8ba1\u7b97\u673a\u89c6\u89c9\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b49\u9886\u57df\u3002\u901a\u8fc7\u8fd9\u4e2a\u5e93\uff0c\u7528\u6237\u53ef\u4ee5\u6709\u6548\u5730\u8861\u91cf\u4e0d\u540c\u5206\u5e03\u4e4b\u95f4\u7684\u76f8\u4f3c\u5ea6\uff0c\u4e3a\u6570\u636e\u5206\u6790\u548c\u6a21\u578b\u4f18\u5316\u63d0\u4f9b\u652f\u6301\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528pyemd\u8ba1\u7b97\u4e24\u4e2a\u5206\u5e03\u7684EMD\uff1f<\/strong><br \/>\u4f7f\u7528pyemd\u8ba1\u7b97EMD\u7684\u8fc7\u7a0b\u76f8\u5bf9\u7b80\u5355\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b9a\u4e49\u4e24\u4e2a\u5206\u5e03\u7684\u76f4\u65b9\u56fe\uff0c\u5e76\u4e14\u51c6\u5907\u4e00\u4e2a\u8ddd\u79bb\u77e9\u9635\u3002\u7136\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>pyemd.emd<\/code>\u51fd\u6570\u6765\u8ba1\u7b97EMD\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nfrom pyemd import emd\n\n# \u5b9a\u4e49\u4e24\u4e2a\u5206\u5e03\na = np.array([0.5, 0.5])\nb = np.array([0.1, 0.9])\n\n# \u521b\u5efa\u8ddd\u79bb\u77e9\u9635\ndistance_matrix = np.array([[0, 1], [1, 0]])\n\n# \u8ba1\u7b97EMD\ndistance = emd(a, b, distance_matrix)\nprint(&quot;EMD:&quot;, distance)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u8ba1\u7b97\u4e24\u4e2a\u7b80\u5355\u5206\u5e03\u4e4b\u95f4\u7684EMD\u503c\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u8c03\u6574\u8f93\u5165\u7684\u5206\u5e03\u548c\u8ddd\u79bb\u77e9\u9635\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Pyemd\u662f\u4e00\u4e2a\u7528\u4e8e\u8ba1\u7b97\u5730\u7403\u79fb\u52a8\u8ddd\u79bb\uff08Earth Mover&#39;s Distance\uff0cEMD\uff09\u7684Pyth [&hellip;]","protected":false},"author":3,"featured_media":1007512,"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\/1007504"}],"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=1007504"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007504\/revisions"}],"predecessor-version":[{"id":1007515,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007504\/revisions\/1007515"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1007512"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1007504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1007504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1007504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}