{"id":982890,"date":"2024-12-27T07:14:41","date_gmt":"2024-12-26T23:14:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/982890.html"},"modified":"2024-12-27T07:14:43","modified_gmt":"2024-12-26T23:14:43","slug":"python%e5%a6%82%e4%bd%95%e7%94%bb%e5%87%baks%e5%9b%be","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/982890.html","title":{"rendered":"python\u5982\u4f55\u753b\u51faks\u56fe"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211154\/3f86be68-59d7-4463-95d9-e3ab81f84922.webp\" alt=\"python\u5982\u4f55\u753b\u51faks\u56fe\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u7ed8\u5236KS\uff08Kolmogorov-Smirnov\uff09\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e93\u5982Matplotlib\u548cSciPy\u3001\u5229\u7528KS\u68c0\u9a8c\u7ed3\u679c\u3001\u521b\u5efa\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u3001\u53ef\u89c6\u5316\u6bd4\u8f83\u3002<\/strong>KS\u56fe\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\uff08CDF\uff09\uff0c\u901a\u5e38\u7528\u4e8e\u8bc4\u4f30\u6a21\u578b\u9884\u6d4b\u4e0e\u5b9e\u9645\u7ed3\u679c\u7684\u5dee\u5f02\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7ed8\u5236KS\u56fe\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u4ee5\u4e0bPython\u5e93\uff1aMatplotlib\u3001SciPy\u548cNumPy\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u6570\u636e\u5904\u7406\u548c\u53ef\u89c6\u5316\u7684\u57fa\u672c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib scipy numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u7406\u89e3KS\u68c0\u9a8c<\/p>\n<\/p>\n<p><p>KS\u68c0\u9a8c\u662f\u4e00\u4e2a\u975e\u53c2\u6570\u68c0\u9a8c\uff0c\u7528\u4e8e\u786e\u5b9a\u4e24\u4e2a\u6837\u672c\u662f\u5426\u6765\u81ea\u540c\u4e00\u4e2a\u5206\u5e03\u3002\u5b83\u901a\u8fc7\u8ba1\u7b97\u4e24\u4e2a\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u4e4b\u95f4\u7684\u6700\u5927\u8ddd\u79bb\u6765\u5b9e\u73b0\u3002\u8fd9\u4e2a\u6700\u5927\u8ddd\u79bb\u5c31\u662fKS\u7edf\u8ba1\u91cf\uff0c\u5b83\u88ab\u7528\u6765\u8861\u91cf\u6837\u672c\u95f4\u7684\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u51c6\u5907\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884cKS\u68c0\u9a8c\u548c\u7ed8\u5236KS\u56fe\u4e4b\u524d\uff0c\u9700\u8981\u51c6\u5907\u597d\u6570\u636e\u3002\u8fd9\u901a\u5e38\u6d89\u53ca\u4e24\u4e2a\u6b65\u9aa4\uff1a\u83b7\u53d6\u6837\u672c\u6570\u636e\u548c\u8ba1\u7b97\u5176\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u83b7\u53d6\u6837\u672c\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u6837\u672c\u6570\u636e\u53ef\u4ee5\u6765\u81ea\u6a21\u578b\u9884\u6d4b\u548c\u5b9e\u9645\u89c2\u5bdf\u503c\u3002\u5bf9\u4e8e\u4e8c\u5206\u7c7b\u95ee\u9898\uff0c\u901a\u5e38\u4f1a\u8ba1\u7b97\u9633\u6027\u6837\u672c\u548c\u9634\u6027\u6837\u672c\u7684\u7d2f\u79ef\u5206\u5e03\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e\uff1a\u6a21\u578b\u9884\u6d4b\u6982\u7387\u548c\u5b9e\u9645\u6807\u7b7e<\/strong><\/h2>\n<p>y_pred = np.random.rand(100)  # \u6a21\u578b\u9884\u6d4b\u7684\u6982\u7387<\/p>\n<p>y_true = np.random.choice([0, 1], size=100)  # \u5b9e\u9645\u6807\u7b7e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8ba1\u7b97\u7d2f\u79ef\u5206\u5e03\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528NumPy\u548cSciPy\u5e93\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u8ba1\u7b97\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy import stats<\/p>\n<h2><strong>\u5bf9\u4e8e\u9633\u6027\u6837\u672c\u548c\u9634\u6027\u6837\u672c\u5206\u522b\u8ba1\u7b97\u7d2f\u79ef\u5206\u5e03<\/strong><\/h2>\n<p>cdf_pos = stats.cumfreq(y_pred[y_true == 1], numbins=100)<\/p>\n<p>cdf_neg = stats.cumfreq(y_pred[y_true == 0], numbins=100)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u7ed8\u5236KS\u56fe<\/p>\n<\/p>\n<p><p>\u4e00\u65e6\u8ba1\u7b97\u4e86\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\uff0c\u5c31\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5e93\u7ed8\u5236KS\u56fe\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u56fe\u5f62\u548c\u8f74<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u56fe\u5f62\u548c\u8f74\uff0c\u4ee5\u4fbf\u7ed8\u5236\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>fig, ax = plt.subplots(figsize=(10, 6))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7ed8\u5236\u7d2f\u79ef\u5206\u5e03\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u5206\u522b\u7ed8\u5236\u9633\u6027\u6837\u672c\u548c\u9634\u6027\u6837\u672c\u7684\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ed8\u5236\u9633\u6027\u6837\u672cCDF<\/p>\n<p>ax.plot(cdf_pos.lowerlimit + np.linspace(0, cdf_pos.binsize * cdf_pos.cumcount.size, cdf_pos.cumcount.size),<\/p>\n<p>        cdf_pos.cumcount \/ max(cdf_pos.cumcount),<\/p>\n<p>        label=&#39;Positive Sample CDF&#39;)<\/p>\n<h2><strong>\u7ed8\u5236\u9634\u6027\u6837\u672cCDF<\/strong><\/h2>\n<p>ax.plot(cdf_neg.lowerlimit + np.linspace(0, cdf_neg.binsize * cdf_neg.cumcount.size, cdf_neg.cumcount.size),<\/p>\n<p>        cdf_neg.cumcount \/ max(cdf_neg.cumcount),<\/p>\n<p>        label=&#39;Negative Sample CDF&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6dfb\u52a0KS\u7edf\u8ba1\u91cf<\/strong><\/p>\n<\/p>\n<p><p>\u8ba1\u7b97KS\u7edf\u8ba1\u91cf\u5e76\u5728\u56fe\u4e2d\u6807\u51fa\u6700\u5927\u8ddd\u79bb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97KS\u7edf\u8ba1\u91cf<\/p>\n<p>ks_statistic, p_value = stats.ks_2samp(y_pred[y_true == 1], y_pred[y_true == 0])<\/p>\n<h2><strong>\u627e\u5230\u6700\u5927\u8ddd\u79bb<\/strong><\/h2>\n<p>max_distance = np.max(np.abs(cdf_pos.cumcount \/ max(cdf_pos.cumcount) - cdf_neg.cumcount \/ max(cdf_neg.cumcount)))<\/p>\n<h2><strong>\u5728\u56fe\u4e2d\u6807\u51fa\u6700\u5927\u8ddd\u79bb<\/strong><\/h2>\n<p>ax.annotate(f&#39;KS Statistic: {ks_statistic:.2f}&#39;,<\/p>\n<p>            xy=(0.5, 0.5), xycoords=&#39;axes fraction&#39;,<\/p>\n<p>            fontsize=12, ha=&#39;center&#39;, va=&#39;center&#39;,<\/p>\n<p>            bbox=dict(boxstyle=&#39;round&#39;, facecolor=&#39;wheat&#39;, alpha=0.5))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bbe\u7f6e\u56fe\u5f62\u6837\u5f0f<\/strong><\/p>\n<\/p>\n<p><p>\u8bbe\u7f6e\u56fe\u5f62\u6807\u9898\u3001\u6807\u7b7e\u548c\u56fe\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">ax.set_title(&#39;KS Plot&#39;)<\/p>\n<p>ax.set_xlabel(&#39;Predicted Probability&#39;)<\/p>\n<p>ax.set_ylabel(&#39;Cumulative Distribution&#39;)<\/p>\n<p>ax.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u89e3\u91ca\u548c\u4f18\u5316KS\u56fe<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u89e3\u91caKS\u56fe<\/strong><\/p>\n<\/p>\n<p><p>\u5728KS\u56fe\u4e2d\uff0c\u4e24\u4e2a\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u4e4b\u95f4\u7684\u6700\u5927\u5782\u76f4\u8ddd\u79bb\u5373\u4e3aKS\u7edf\u8ba1\u91cf\u3002\u8be5\u7edf\u8ba1\u91cf\u8d8a\u5927\uff0c\u8bf4\u660e\u6a21\u578b\u9884\u6d4b\u4e0e\u5b9e\u9645\u7ed3\u679c\u4e4b\u95f4\u7684\u5dee\u5f02\u8d8a\u5927\u3002\u56e0\u6b64\uff0c\u5728\u6a21\u578b\u8bc4\u4f30\u4e2d\uff0cKS\u7edf\u8ba1\u91cf\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6307\u6807\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f18\u5316KS\u56fe<\/strong><\/p>\n<\/p>\n<ul>\n<li><strong>\u9009\u62e9\u9002\u5f53\u7684bin\u6570<\/strong>\uff1a\u5728\u8ba1\u7b97\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\u65f6\uff0c\u9009\u62e9\u9002\u5f53\u7684bin\u6570\u53ef\u4ee5\u63d0\u9ad8\u56fe\u5f62\u7684\u6e05\u6670\u5ea6\u3002<\/li>\n<li><strong>\u6570\u636e\u9884\u5904\u7406<\/strong>\uff1a\u5728\u8ba1\u7b97CDF\u4e4b\u524d\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u6807\u51c6\u5316\u6216\u53bb\u9664\u5f02\u5e38\u503c\u3002<\/li>\n<li><strong>\u4f7f\u7528\u6837\u5f0f\u548c\u989c\u8272<\/strong>\uff1a\u901a\u8fc7\u8c03\u6574\u56fe\u5f62\u7684\u6837\u5f0f\u548c\u989c\u8272\uff0c\u53ef\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u548c\u89c6\u89c9\u6548\u679c\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><p>\u603b\u7ed3\uff0c\u7ed8\u5236KS\u56fe\u662f\u6a21\u578b\u8bc4\u4f30\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u6b65\u9aa4\u3002\u901a\u8fc7\u6bd4\u8f83\u6a21\u578b\u9884\u6d4b\u548c\u5b9e\u9645\u7ed3\u679c\u7684\u7d2f\u79ef\u5206\u5e03\u51fd\u6570\uff0c\u53ef\u4ee5\u76f4\u89c2\u5730\u8bc4\u4f30\u6a21\u578b\u7684\u6027\u80fd\u3002\u5229\u7528Python\u4e2d\u7684Matplotlib\u548cSciPy\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\uff0c\u5e76\u901a\u8fc7\u5408\u9002\u7684\u4f18\u5316\u7b56\u7565\u63d0\u9ad8\u56fe\u5f62\u7684\u6e05\u6670\u5ea6\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u7ed8\u5236KS\u56fe\uff1f<\/strong><br \/>KS\u56fe\uff0c\u5373Kolmogorov-Smirnov\u56fe\uff0c\u662f\u7528\u4e8e\u6bd4\u8f83\u4e24\u4e2a\u5206\u5e03\u7684\u6709\u6548\u5de5\u5177\u3002\u4f7f\u7528Python\u7ed8\u5236KS\u56fe\u7684\u5e38\u89c1\u65b9\u6cd5\u662f\u901a\u8fc7<code>matplotlib<\/code>\u548c<code>scipy<\/code>\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a\u9996\u5148\uff0c\u5b89\u88c5\u76f8\u5e94\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>scipy.stats.ks_2samp<\/code>\u65b9\u6cd5\u8fdb\u884cKS\u68c0\u9a8c\uff0c\u6700\u540e\u7528<code>matplotlib<\/code>\u7ed8\u5236\u56fe\u5f62\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nimport matplotlib.pyplot as plt\nfrom scipy import stats\n\n# \u751f\u6210\u4e24\u7ec4\u968f\u673a\u6570\u636e\ndata1 = np.random.normal(0, 1, 1000)\ndata2 = np.random.normal(0, 1.5, 1000)\n\n# \u8ba1\u7b97KS\u7edf\u8ba1\u91cf\nks_statistic, p_value = stats.ks_2samp(data1, data2)\n\n# \u7ed8\u5236KS\u56fe\nplt.figure(figsize=(10, 6))\nplt.hist(data1, bins=30, alpha=0.5, label=&#39;Data 1&#39;)\nplt.hist(data2, bins=30, alpha=0.5, label=&#39;Data 2&#39;)\nplt.title(&#39;KS\u56fe\u793a\u4f8b&#39;)\nplt.xlabel(&#39;\u503c&#39;)\nplt.ylabel(&#39;\u9891\u7387&#39;)\nplt.legend()\nplt.show()\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u7ed8\u5236KS\u56fe\u9700\u8981\u54ea\u4e9b\u5e93\uff1f<\/strong><br \/>\u7ed8\u5236KS\u56fe\u901a\u5e38\u9700\u8981<code>matplotlib<\/code>\u548c<code>scipy<\/code>\u8fd9\u4e24\u4e2a\u5e93\u3002<code>matplotlib<\/code>\u7528\u4e8e\u56fe\u5f62\u7ed8\u5236\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u53ef\u89c6\u5316\u529f\u80fd\uff1b\u800c<code>scipy<\/code>\u5219\u5305\u542b\u4e86\u7edf\u8ba1\u5206\u6790\u7684\u529f\u80fd\uff0c\u5305\u62ecKS\u68c0\u9a8c\u3002\u5728\u5b89\u88c5\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\uff1a<code>pip install matplotlib scipy<\/code>\u3002<\/p>\n<p><strong>KS\u56fe\u7684\u5e94\u7528\u573a\u666f\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>KS\u56fe\u5e7f\u6cdb\u5e94\u7528\u4e8e\u7edf\u8ba1\u5206\u6790\uff0c\u5c24\u5176\u662f\u5728\u6bd4\u8f83\u4e24\u4e2a\u4e0d\u540c\u6837\u672c\u7684\u5206\u5e03\u65f6\u3002\u4f8b\u5982\uff0c\u5728\u91d1\u878d\u9886\u57df\uff0c\u53ef\u4ee5\u7528\u6765\u6bd4\u8f83\u4e0d\u540c\u6295\u8d44\u7ec4\u5408\u7684\u56de\u62a5\u7387\u5206\u5e03\uff1b\u5728\u751f\u7269\u7edf\u8ba1\u4e2d\uff0c\u53ef\u4ee5\u6bd4\u8f83\u4e0d\u540c\u836f\u7269\u5bf9\u60a3\u8005\u53cd\u5e94\u7684\u6548\u679c\u5206\u5e03\u3002\u8fd9\u79cd\u56fe\u5f62\u5316\u7684\u6bd4\u8f83\u65b9\u5f0f\u4f7f\u5f97\u5206\u6790\u7ed3\u679c\u66f4\u52a0\u76f4\u89c2\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u7ed8\u5236KS\uff08Kolmogorov-Smirnov\uff09\u56fe\uff0c\u53ef\u4ee5\u4f7f\u7528\u5e93\u5982Matplotlib\u548cSci [&hellip;]","protected":false},"author":3,"featured_media":982896,"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\/982890"}],"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=982890"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/982890\/revisions"}],"predecessor-version":[{"id":982897,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/982890\/revisions\/982897"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/982896"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=982890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=982890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=982890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}