{"id":1156821,"date":"2025-01-13T18:21:00","date_gmt":"2025-01-13T10:21:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1156821.html"},"modified":"2025-01-13T18:21:02","modified_gmt":"2025-01-13T10:21:02","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97auc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1156821.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u8ba1\u7b97auc"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25195215\/4526121f-fc2f-4d3d-bd10-524b067658fc.webp\" alt=\"python\u4e2d\u5982\u4f55\u8ba1\u7b97auc\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8ba1\u7b97AUC\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>scikit-learn<\/code>\u5e93\u4e2d\u7684<code>roc_auc_score<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>numpy<\/code>\u8fdb\u884c\u81ea\u5b9a\u4e49\u8ba1\u7b97\u3001\u4f7f\u7528<code>tensorflow<\/code>\u6216<code>keras<\/code>\u4e2d\u7684\u65b9\u6cd5\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528<code>scikit-learn<\/code>\u5e93\u4e2d\u7684<code>roc_auc_score<\/code>\u51fd\u6570\u662f\u6700\u7b80\u5355\u548c\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u8ba1\u7b97AUC\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528scikit-learn\u5e93\u4e2d\u7684roc_auc_score\u51fd\u6570<\/h3>\n<\/p>\n<p><p><code>scikit-learn<\/code>\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u5e93\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u8bb8\u591a\u7528\u4e8e\u6a21\u578b\u8bc4\u4f30\u7684\u51fd\u6570\u548c\u5de5\u5177\u3002\u8ba1\u7b97AUC\u6700\u5e38\u7528\u7684\u51fd\u6570\u662f<code>roc_auc_score<\/code>\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.metrics import roc_auc_score<\/p>\n<h2><strong>\u5047\u8bbe\u4f60\u6709\u771f\u5b9e\u6807\u7b7e\u548c\u9884\u6d4b\u6982\u7387<\/strong><\/h2>\n<p>y_true = [0, 0, 1, 1]<\/p>\n<p>y_scores = [0.1, 0.4, 0.35, 0.8]<\/p>\n<h2><strong>\u8ba1\u7b97AUC<\/strong><\/h2>\n<p>auc = roc_auc_score(y_true, y_scores)<\/p>\n<p>print(f&quot;AUC: {auc}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>roc_auc_score<\/code>\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u771f\u5b9e\u6807\u7b7e\u548c\u9884\u6d4b\u6982\u7387\u3002\u771f\u5b9e\u6807\u7b7e\u662f\u4e8c\u8fdb\u5236\uff080\u62161\uff09\uff0c\u9884\u6d4b\u6982\u7387\u662f\u6a21\u578b\u9884\u6d4b\u7684\u6982\u7387\u5206\u6570\u3002\u51fd\u6570\u4f1a\u8fd4\u56deAUC\u7684\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528numpy\u8fdb\u884c\u81ea\u5b9a\u4e49\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4e0d\u60f3\u4f9d\u8d56\u7b2c\u4e09\u65b9\u5e93\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u8fdb\u884cAUC\u7684\u81ea\u5b9a\u4e49\u8ba1\u7b97\u3002\u8fd9\u4e2a\u65b9\u6cd5\u6d89\u53ca\u8ba1\u7b97ROC\u66f2\u7ebf\u7684\u5404\u4e2a\u70b9\uff0c\u7136\u540e\u4f7f\u7528\u68af\u5f62\u6cd5\u5219\u8ba1\u7b97\u9762\u79ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def custom_roc_auc(y_true, y_scores):<\/p>\n<p>    # \u6392\u5e8f<\/p>\n<p>    desc_score_indices = np.argsort(y_scores)[::-1]<\/p>\n<p>    y_true = np.array(y_true)[desc_score_indices]<\/p>\n<p>    y_scores = np.array(y_scores)[desc_score_indices]<\/p>\n<p>    # \u6b63\u8d1f\u6837\u672c\u6570<\/p>\n<p>    n_pos = np.sum(y_true)<\/p>\n<p>    n_neg = len(y_true) - n_pos<\/p>\n<p>    # \u8ba1\u7b97\u7d2f\u8ba1TPR\u548cFPR<\/p>\n<p>    tps = np.cumsum(y_true)<\/p>\n<p>    fps = np.cumsum(1 - y_true)<\/p>\n<p>    tpr = tps \/ n_pos<\/p>\n<p>    fpr = fps \/ n_neg<\/p>\n<p>    # \u8ba1\u7b97AUC<\/p>\n<p>    auc = np.trapz(tpr, fpr)<\/p>\n<p>    return auc<\/p>\n<p>y_true = [0, 0, 1, 1]<\/p>\n<p>y_scores = [0.1, 0.4, 0.35, 0.8]<\/p>\n<h2><strong>\u8ba1\u7b97AUC<\/strong><\/h2>\n<p>auc = custom_roc_auc(y_true, y_scores)<\/p>\n<p>print(f&quot;AUC: {auc}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u65b9\u6cd5\u9996\u5148\u5bf9\u9884\u6d4b\u6982\u7387\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u8ba1\u7b97\u7d2f\u8ba1\u7684TPR\u548cFPR\uff0c\u6700\u540e\u4f7f\u7528\u68af\u5f62\u6cd5\u5219\u8ba1\u7b97AUC\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528tensorflow\u6216keras\u4e2d\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4f7f\u7528<code>tensorflow<\/code>\u6216<code>keras<\/code>\u8fdb\u884c\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u7684\u8bad\u7ec3\uff0c\u5b83\u4eec\u4e5f\u63d0\u4f9b\u4e86\u8ba1\u7b97AUC\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u662f\u4f7f\u7528<code>tensorflow<\/code>\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<h2><strong>\u5047\u8bbe\u4f60\u6709\u771f\u5b9e\u6807\u7b7e\u548c\u9884\u6d4b\u6982\u7387<\/strong><\/h2>\n<p>y_true = [0, 0, 1, 1]<\/p>\n<p>y_scores = [0.1, 0.4, 0.35, 0.8]<\/p>\n<h2><strong>\u8f6c\u6362\u4e3aTensorFlow\u5f20\u91cf<\/strong><\/h2>\n<p>y_true = tf.constant(y_true, dtype=tf.float32)<\/p>\n<p>y_scores = tf.constant(y_scores, dtype=tf.float32)<\/p>\n<h2><strong>\u8ba1\u7b97AUC<\/strong><\/h2>\n<p>auc_metric = tf.keras.metrics.AUC()<\/p>\n<p>auc_metric.update_state(y_true, y_scores)<\/p>\n<p>auc = auc_metric.result().numpy()<\/p>\n<p>print(f&quot;AUC: {auc}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5df2\u7ecf\u5728\u4f7f\u7528<code>tensorflow<\/code>\u6216<code>keras<\/code>\u8fdb\u884c\u6a21\u578b\u8bad\u7ec3\u7684\u573a\u666f\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5e93\u5185\u7f6e\u4e86\u8ba1\u7b97AUC\u7684\u51fd\u6570\uff0c\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u7075\u6d3b\u5730\u8ba1\u7b97AUC\u3002<strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u63a8\u8350\u4f7f\u7528<code>scikit-learn<\/code>\u5e93\u4e2d\u7684<code>roc_auc_score<\/code>\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u4e14\u9ad8\u6548<\/strong>\u3002\u5bf9\u4e8e\u66f4\u9ad8\u7684\u81ea\u5b9a\u4e49\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528<code>numpy<\/code>\u8fdb\u884c\u81ea\u5b9a\u4e49\u8ba1\u7b97\uff0c\u6216\u8005\u5728\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u4e2d\u4f7f\u7528<code>tensorflow<\/code>\u6216<code>keras<\/code>\u7684\u5185\u7f6e\u65b9\u6cd5\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u6838\u5fc3\u6b65\u9aa4\u90fd\u662f\u76f8\u4f3c\u7684\uff1a\u8ba1\u7b97ROC\u66f2\u7ebf\uff0c\u7136\u540e\u4f7f\u7528\u68af\u5f62\u6cd5\u5219\u8ba1\u7b97\u9762\u79ef\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97AUC\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528scikit-learn\u5e93\u6765\u8ba1\u7b97AUC\u3002\u9996\u5148\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<pre><code class=\"language-bash\">pip install scikit-learn\n<\/code><\/pre>\n<p>\u8ba1\u7b97AUC\u7684\u6b65\u9aa4\u5305\u62ec\uff1a<\/p>\n<ol>\n<li>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3002<\/li>\n<li>\u51c6\u5907\u771f\u5b9e\u6807\u7b7e\u548c\u9884\u6d4b\u6982\u7387\u3002<\/li>\n<li>\u4f7f\u7528<code>roc_auc_score<\/code>\u51fd\u6570\u8ba1\u7b97AUC\u503c\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-python\">from sklearn.metrics import roc_auc_score\n\n# \u793a\u4f8b\u771f\u5b9e\u6807\u7b7e\u548c\u9884\u6d4b\u6982\u7387\ny_true = [0, 1, 1, 0, 1]\ny_scores = [0.1, 0.4, 0.35, 0.8, 0.7]\n\n# \u8ba1\u7b97AUC\nauc = roc_auc_score(y_true, y_scores)\nprint(&quot;AUC\u503c\u4e3a:&quot;, auc)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u80fd\u591f\u5e2e\u52a9\u7528\u6237\u5feb\u901f\u5f97\u5230\u6a21\u578b\u7684AUC\u503c\uff0c\u4ee5\u8bc4\u4f30\u5176\u6027\u80fd\u3002<\/p>\n<p><strong>AUC\u503c\u7684\u610f\u4e49\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>AUC\uff08Area Under the Curve\uff09\u4ee3\u8868\u66f2\u7ebf\u4e0b\u9762\u79ef\uff0c\u901a\u5e38\u4e0eROC\uff08Receiver Operating Characteristic\uff09\u66f2\u7ebf\u76f8\u5173\u8054\u3002AUC\u503c\u8303\u56f4\u57280\u52301\u4e4b\u95f4\uff1a<\/p>\n<ul>\n<li>1\u8868\u793a\u5b8c\u7f8e\u7684\u5206\u7c7b\u5668\u3002<\/li>\n<li>0.5\u8868\u793a\u6a21\u578b\u65e0\u533a\u5206\u80fd\u529b\uff0c\u76f8\u5f53\u4e8e\u968f\u673a\u731c\u6d4b\u3002<\/li>\n<li>\u4f4e\u4e8e0.5\u5219\u8868\u793a\u6a21\u578b\u7684\u9884\u6d4b\u6548\u679c\u4e0d\u4f73\u3002<\/li>\n<\/ul>\n<p>AUC\u8d8a\u63a5\u8fd11\uff0c\u6a21\u578b\u7684\u6027\u80fd\u8d8a\u597d\uff0c\u56e0\u6b64\u5728\u8bc4\u4f30\u5206\u7c7b\u6a21\u578b\u65f6\uff0cAUC\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6307\u6807\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u4e0d\u540c\u573a\u666f\u4e2d\u4f7f\u7528AUC\uff1f<\/strong><\/p>\n<p>AUC\u9002\u7528\u4e8e\u4e8c\u5206\u7c7b\u95ee\u9898\u7684\u6027\u80fd\u8bc4\u4f30\uff0c\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u573a\u666f\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u533b\u7597\u8bca\u65ad\uff1a\u8bc4\u4f30\u75be\u75c5\u9884\u6d4b\u6a21\u578b\u7684\u6548\u679c\u3002<\/li>\n<li>\u4fe1\u7528\u8bc4\u5206\uff1a\u5206\u6790\u8d37\u6b3e\u8fdd\u7ea6\u98ce\u9669\u3002<\/li>\n<li>\u8425\u9500\uff1a\u8861\u91cf\u7528\u6237\u8f6c\u5316\u7387\u9884\u6d4b\u6a21\u578b\u7684\u80fd\u529b\u3002<\/li>\n<\/ul>\n<p>\u5728\u8fd9\u4e9b\u573a\u666f\u4e2d\uff0cAUC\u80fd\u591f\u5e2e\u52a9\u51b3\u7b56\u8005\u4e86\u89e3\u6a21\u578b\u7684\u6709\u6548\u6027\u548c\u53ef\u9760\u6027\uff0c\u4ece\u800c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u505a\u51fa\u66f4\u597d\u7684\u51b3\u7b56\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8ba1\u7b97AUC\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528scikit-learn\u5e93\u4e2d\u7684roc_auc_score\u51fd\u6570\u3001\u4f7f\u7528nu [&hellip;]","protected":false},"author":3,"featured_media":1156827,"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\/1156821"}],"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=1156821"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156821\/revisions"}],"predecessor-version":[{"id":1156829,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1156821\/revisions\/1156829"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1156827"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1156821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1156821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1156821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}