{"id":972038,"date":"2024-12-27T05:40:40","date_gmt":"2024-12-26T21:40:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/972038.html"},"modified":"2024-12-27T05:40:42","modified_gmt":"2024-12-26T21:40:42","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97roc%e6%9b%b2%e7%ba%bf","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/972038.html","title":{"rendered":"python\u5982\u4f55\u8ba1\u7b97roc\u66f2\u7ebf"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24194857\/eb7c76ac-4b8e-471b-913f-c8439c40c5b8.webp\" alt=\"python\u5982\u4f55\u8ba1\u7b97roc\u66f2\u7ebf\" \/><\/p>\n<p><p> <strong>Python\u8ba1\u7b97ROC\u66f2\u7ebf\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528\u6a21\u578b\u9884\u6d4b\u6982\u7387\u3001\u8ba1\u7b97\u771f\u6b63\u7387\u548c\u5047\u6b63\u7387\u3001\u4f7f\u7528<code>roc_curve<\/code>\u51fd\u6570\u7ed8\u5236\u66f2\u7ebf\u3001\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\u3002\u8fd9\u4e9b\u6b65\u9aa4\u5e2e\u52a9\u6211\u4eec\u6709\u6548\u5730\u8bc4\u4f30\u5206\u7c7b\u6a21\u578b\u7684\u8868\u73b0\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1a\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u8bad\u7ec3\u597d\u7684\u5206\u7c7b\u6a21\u578b\u5bf9\u6d4b\u8bd5\u6570\u636e\u8fdb\u884c\u9884\u6d4b\uff0c\u5e76\u83b7\u53d6\u9884\u6d4b\u6982\u7387\u3002\u63a5\u7740\uff0c\u4f7f\u7528\u8fd9\u4e9b\u9884\u6d4b\u6982\u7387\u548c\u771f\u5b9e\u6807\u7b7e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>sklearn<\/code>\u5e93\u4e2d\u7684<code>roc_curve<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u771f\u6b63\u7387\uff08True Positive Rate\uff09\u548c\u5047\u6b63\u7387\uff08False Positive Rate\uff09\u3002\u6700\u540e\uff0c\u5229\u7528\u8fd9\u4e9b\u7387\u7ed8\u5236ROC\u66f2\u7ebf\uff0c\u5e76\u901a\u8fc7AUC\uff08Area Under Curve\uff09\u6765\u8bc4\u4f30\u6a21\u578b\u7684\u6027\u80fd\u3002AUC\u503c\u8d8a\u63a5\u8fd11\uff0c\u8868\u793a\u6a21\u578b\u6027\u80fd\u8d8a\u597d\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u8bb2\u89e3\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u548c\u7ed8\u5236ROC\u66f2\u7ebf\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u51c6\u5907\u5de5\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u8ba1\u7b97ROC\u66f2\u7ebf\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u5fc5\u8981\u7684Python\u5e93\uff0c\u5982<code>numpy<\/code>\u3001<code>matplotlib<\/code>\u548c<code>scikit-learn<\/code>\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u6570\u636e\u5904\u7406\u3001\u7ed8\u56fe\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy matplotlib scikit-learn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5f00\u59cb\u51c6\u5907\u6570\u636e\u548c\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6570\u636e\u51c6\u5907\u4e0e\u6a21\u578b\u8bad\u7ec3<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u51c6\u5907\u4e00\u4e2a\u4e8c\u5206\u7c7b\u6570\u636e\u96c6\u5e76\u8bad\u7ec3\u4e00\u4e2a\u5206\u7c7b\u6a21\u578b\u3002\u8fd9\u91cc\u6211\u4eec\u4ee5<code>scikit-learn<\/code>\u4e2d\u7684<code>make_classification<\/code>\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u96c6\uff0c\u5e76\u4f7f\u7528\u903b\u8f91\u56de\u5f52\u6a21\u578b\u4f5c\u4e3a\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.datasets import make_classification<\/p>\n<p>from sklearn.model_selection import tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_test_split<\/p>\n<p>from sklearn.linear_model import LogisticRegression<\/p>\n<h2><strong>\u751f\u6210\u6570\u636e\u96c6<\/strong><\/h2>\n<p>X, y = make_classification(n_samples=1000, n_features=20, n_classes=2, random_state=42)<\/p>\n<h2><strong>\u5206\u5272\u6570\u636e\u96c6\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6<\/strong><\/h2>\n<p>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)<\/p>\n<h2><strong>\u8bad\u7ec3\u903b\u8f91\u56de\u5f52\u6a21\u578b<\/strong><\/h2>\n<p>model = LogisticRegression()<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e00\u6b65\uff0c\u6211\u4eec\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b1000\u4e2a\u6837\u672c\u300120\u4e2a\u7279\u5f81\u7684\u4e8c\u5206\u7c7b\u6570\u636e\u96c6\uff0c\u5e76\u4f7f\u752830%\u7684\u6570\u636e\u4f5c\u4e3a\u6d4b\u8bd5\u96c6\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528\u903b\u8f91\u56de\u5f52\u6a21\u578b\u8fdb\u884c\u4e86\u8bad\u7ec3\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8ba1\u7b97\u9884\u6d4b\u6982\u7387<\/h3>\n<\/p>\n<p><p>\u8bad\u7ec3\u6a21\u578b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6a21\u578b\u5bf9\u6d4b\u8bd5\u96c6\u8fdb\u884c\u9884\u6d4b\uff0c\u5e76\u83b7\u53d6\u9884\u6d4b\u6982\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u83b7\u53d6\u9884\u6d4b\u6982\u7387<\/p>\n<p>y_scores = model.predict_proba(X_test)[:, 1]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>predict_proba<\/code>\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u6bcf\u4e2a\u6837\u672c\u5c5e\u4e8e\u6b63\u7c7b\u7684\u6982\u7387\u3002\u8fd9\u4e9b\u6982\u7387\u5c06\u7528\u4e8e\u540e\u7eed\u7684ROC\u66f2\u7ebf\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8ba1\u7b97ROC\u66f2\u7ebf<\/h3>\n<\/p>\n<p><p>\u6709\u4e86\u9884\u6d4b\u6982\u7387\u548c\u771f\u5b9e\u6807\u7b7e\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>roc_curve<\/code>\u51fd\u6570\u8ba1\u7b97\u771f\u6b63\u7387\u548c\u5047\u6b63\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.metrics import roc_curve, auc<\/p>\n<h2><strong>\u8ba1\u7b97ROC\u66f2\u7ebf<\/strong><\/h2>\n<p>fpr, tpr, thresholds = roc_curve(y_test, y_scores)<\/p>\n<h2><strong>\u8ba1\u7b97AUC\u503c<\/strong><\/h2>\n<p>roc_auc = auc(fpr, tpr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e00\u6b65\uff0c\u6211\u4eec\u901a\u8fc7<code>roc_curve<\/code>\u51fd\u6570\u83b7\u5f97\u4e86\u5047\u6b63\u7387\uff08fpr\uff09\u548c\u771f\u6b63\u7387\uff08tpr\uff09\uff0c\u5e76\u4f7f\u7528<code>auc<\/code>\u51fd\u6570\u8ba1\u7b97\u4e86AUC\u503c\u3002AUC\u503c\u7528\u4e8e\u91cf\u5316\u6a21\u578b\u7684\u5206\u7c7b\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7ed8\u5236ROC\u66f2\u7ebf<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u5c06ROC\u66f2\u7ebf\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u7ed8\u5236ROC\u66f2\u7ebf<\/strong><\/h2>\n<p>plt.figure()<\/p>\n<p>plt.plot(fpr, tpr, color=&#39;blue&#39;, lw=2, label=&#39;ROC curve (area = %0.2f)&#39; % roc_auc)<\/p>\n<p>plt.plot([0, 1], [0, 1], color=&#39;gray&#39;, lw=2, linestyle=&#39;--&#39;)<\/p>\n<p>plt.xlim([0.0, 1.0])<\/p>\n<p>plt.ylim([0.0, 1.05])<\/p>\n<p>plt.xlabel(&#39;False Positive Rate&#39;)<\/p>\n<p>plt.ylabel(&#39;True Positive Rate&#39;)<\/p>\n<p>plt.title(&#39;Receiver Operating Characteristic&#39;)<\/p>\n<p>plt.legend(loc=&quot;lower right&quot;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u7ed8\u5236\u4e86ROC\u66f2\u7ebf\uff0c\u5e76\u5728\u56fe\u4e2d\u6807\u6ce8\u4e86AUC\u503c\u3002ROC\u66f2\u7ebf\u4e0b\u65b9\u7684\u9762\u79ef\uff08AUC\uff09\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6307\u6807\uff0c\u7528\u4e8e\u8bc4\u4f30\u5206\u7c7b\u6a21\u578b\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6a21\u578b\u6027\u80fd\u8bc4\u4f30<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6a21\u578b\u8bc4\u4f30\u65f6\uff0cROC\u66f2\u7ebf\u548cAUC\u503c\u662f\u4e24\u4e2a\u91cd\u8981\u7684\u6307\u6807\u3002ROC\u66f2\u7ebf\u63d0\u4f9b\u4e86\u6a21\u578b\u5728\u4e0d\u540c\u9608\u503c\u4e0b\u7684\u8868\u73b0\uff0c\u800cAUC\u503c\u5219\u91cf\u5316\u4e86\u6a21\u578b\u7684\u6574\u4f53\u6027\u80fd\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>ROC\u66f2\u7ebf<\/strong>\uff1a\u901a\u8fc7\u89c2\u5bdf\u66f2\u7ebf\u7684\u5f62\u72b6\u548c\u4f4d\u7f6e\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u89c2\u5730\u4e86\u89e3\u6a21\u578b\u5728\u5404\u79cd\u9608\u503c\u4e0b\u7684\u6027\u80fd\u3002\u5982\u679c\u66f2\u7ebf\u9760\u8fd1\u5de6\u4e0a\u89d2\uff0c\u6a21\u578b\u7684\u6027\u80fd\u8f83\u597d\u3002<\/li>\n<li><strong>AUC\u503c<\/strong>\uff1aAUC\u503c\u57280\u52301\u4e4b\u95f4\uff0c\u503c\u8d8a\u9ad8\u8868\u793a\u6a21\u578b\u6027\u80fd\u8d8a\u597d\u3002AUC\u503c\u4e3a0.5\u8868\u793a\u6a21\u578b\u7684\u9884\u6d4b\u6548\u679c\u4e0e\u968f\u673a\u731c\u6d4b\u76f8\u5f53\u3002<\/li>\n<\/ul>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5168\u9762\u4e86\u89e3\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u5e76\u8bc4\u4f30ROC\u66f2\u7ebf\uff0c\u4ece\u800c\u66f4\u597d\u5730\u7406\u89e3\u5206\u7c7b\u6a21\u578b\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u4ec0\u4e48\u662fROC\u66f2\u7ebf\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>ROC\u66f2\u7ebf\uff08\u63a5\u6536\u8005\u64cd\u4f5c\u7279\u5f81\u66f2\u7ebf\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u8bc4\u4f30\u4e8c\u5206\u7c7b\u6a21\u578b\u6027\u80fd\u7684\u5de5\u5177\u3002\u5b83\u901a\u8fc7\u7ed8\u5236\u771f\u6b63\u7387\uff08TPR\uff09\u4e0e\u5047\u6b63\u7387\uff08FPR\uff09\u4e4b\u95f4\u7684\u5173\u7cfb\u6765\u5c55\u793a\u6a21\u578b\u5728\u4e0d\u540c\u9608\u503c\u4e0b\u7684\u8868\u73b0\u3002ROC\u66f2\u7ebf\u8d8a\u9760\u8fd1\u5de6\u4e0a\u89d2\uff0c\u6a21\u578b\u7684\u6027\u80fd\u8d8a\u597d\uff0c\u901a\u5e38\u901a\u8fc7AUC\uff08\u66f2\u7ebf\u4e0b\u9762\u79ef\uff09\u6765\u91cf\u5316\u6a21\u578b\u7684\u6574\u4f53\u8868\u73b0\u3002<\/p>\n<p><strong>2. \u5728Python\u4e2d\u5982\u4f55\u751f\u6210ROC\u66f2\u7ebf\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u4e2d\u7684<code>scikit-learn<\/code>\u5e93\u6765\u8ba1\u7b97\u548c\u7ed8\u5236ROC\u66f2\u7ebf\u3002\u9996\u5148\uff0c\u9700\u8981\u5b89\u88c5\u8be5\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>roc_curve<\/code>\u51fd\u6570\u6765\u83b7\u53d6TPR\u548cFPR\uff0c\u6700\u540e\u5229\u7528<code>matplotlib<\/code>\u5e93\u8fdb\u884c\u53ef\u89c6\u5316\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from sklearn.metrics import roc_curve, auc\nimport matplotlib.pyplot as plt\n\n# \u5047\u8bbey_true\u662f\u5b9e\u9645\u6807\u7b7e\uff0cy_scores\u662f\u6a21\u578b\u9884\u6d4b\u7684\u5206\u6570\nfpr, tpr, thresholds = roc_curve(y_true, y_scores)\nroc_auc = auc(fpr, tpr)\n\nplt.plot(fpr, tpr, color=&#39;blue&#39;, label=&#39;ROC curve (area = %0.2f)&#39; % roc_auc)\nplt.plot([0, 1], [0, 1], color=&#39;red&#39;, linestyle=&#39;--&#39;)\nplt.xlabel(&#39;False Positive Rate&#39;)\nplt.ylabel(&#39;True Positive Rate&#39;)\nplt.title(&#39;Receiver Operating Characteristic&#39;)\nplt.legend(loc=&#39;lower right&#39;)\nplt.show()\n<\/code><\/pre>\n<p><strong>3. \u5982\u4f55\u89e3\u91caROC\u66f2\u7ebf\u4e2d\u7684AUC\u503c\uff1f<\/strong><br \/>AUC\u503c\u8868\u793aROC\u66f2\u7ebf\u4e0b\u7684\u9762\u79ef\uff0c\u5176\u503c\u8303\u56f4\u4ece0\u52301\u3002AUC\u7b49\u4e8e0.5\u8868\u793a\u6a21\u578b\u6ca1\u6709\u8fa8\u522b\u80fd\u529b\uff0c\u76f8\u5f53\u4e8e\u968f\u673a\u731c\u6d4b\uff1bAUC\u5927\u4e8e0.5\u4f46\u5c0f\u4e8e1\u5219\u8868\u793a\u6a21\u578b\u5177\u6709\u4e00\u5b9a\u7684\u5206\u7c7b\u80fd\u529b\uff1b\u5f53AUC\u7b49\u4e8e1\u65f6\uff0c\u6a21\u578b\u5b8c\u7f8e\u533a\u5206\u4e86\u6240\u6709\u6b63\u8d1f\u6837\u672c\u3002\u56e0\u6b64\uff0cAUC\u503c\u8d8a\u63a5\u8fd11\uff0c\u6a21\u578b\u7684\u6027\u80fd\u5c31\u8d8a\u597d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8ba1\u7b97ROC\u66f2\u7ebf\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u4f7f\u7528\u6a21\u578b\u9884\u6d4b\u6982\u7387\u3001\u8ba1\u7b97\u771f\u6b63\u7387\u548c\u5047\u6b63\u7387\u3001\u4f7f\u7528roc_curve\u51fd\u6570\u7ed8\u5236 [&hellip;]","protected":false},"author":3,"featured_media":972042,"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\/972038"}],"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=972038"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972038\/revisions"}],"predecessor-version":[{"id":972044,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/972038\/revisions\/972044"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/972042"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=972038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=972038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=972038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}