{"id":1032640,"date":"2024-12-31T11:35:19","date_gmt":"2024-12-31T03:35:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1032640.html"},"modified":"2024-12-31T11:35:21","modified_gmt":"2024-12-31T03:35:21","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e4%bd%bf%e7%94%a8%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1032640.html","title":{"rendered":"\u5982\u4f55\u5728python\u4e2d\u4f7f\u7528\u795e\u7ecf\u7f51\u7edc"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/4331472e-1712-4958-9a0e-fed4d76d2b46.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u5728python\u4e2d\u4f7f\u7528\u795e\u7ecf\u7f51\u7edc\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u4f7f\u7528\u795e\u7ecf\u7f51\u7edc\u7684\u6838\u5fc3\u8981\u70b9\u6709\uff1a\u9009\u62e9\u9002\u5f53\u7684\u5e93\u3001\u51c6\u5907\u6570\u636e\u3001\u5b9a\u4e49\u6a21\u578b\u3001\u8bad\u7ec3\u6a21\u578b\u3001\u8bc4\u4f30\u6a21\u578b<\/strong>\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u7528\u7684\u6280\u5de7\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u9009\u62e9\u9002\u5f53\u7684\u5e93<\/h2>\n<\/p>\n<p><p>Python\u6709\u8bb8\u591a\u7528\u4e8e\u6784\u5efa\u548c\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u7684\u5e93\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u5305\u62ecTensorFlow\u3001Keras\u3001PyTorch\u548cScikit-learn\u3002\u6bcf\u4e2a\u5e93\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002\u4f8b\u5982\uff0c<strong>TensorFlow<\/strong>\u548c<strong>PyTorch<\/strong>\u9002\u7528\u4e8e\u6784\u5efa\u590d\u6742\u7684\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\uff0c\u800c<strong>Keras<\/strong>\u63d0\u4f9b\u4e86\u66f4\u7b80\u4fbf\u7684API\uff0c\u975e\u5e38\u9002\u5408\u521d\u5b66\u8005\u3002<\/p>\n<\/p>\n<p><h3>1. TensorFlow<\/h3>\n<\/p>\n<p><p>TensorFlow\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u7531Google\u5f00\u53d1\u3002\u5b83\u63d0\u4f9b\u4e86\u7075\u6d3b\u6027\u548c\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u89c4\u6a21\u7684\u9879\u76ee\u3002TensorFlow\u7684\u4e3b\u8981\u4f18\u70b9\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u652f\u6301\u6784\u5efa\u4ece\u7b80\u5355\u5230\u590d\u6742\u7684\u6a21\u578b\u3002<\/li>\n<li><strong>\u9ad8\u6027\u80fd<\/strong>\uff1a\u652f\u6301GPU\u52a0\u901f\uff0c\u9002\u5408\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u3002<\/li>\n<li><strong>\u5e7f\u6cdb\u7684\u793e\u533a\u652f\u6301<\/strong>\uff1a\u6709\u4e30\u5bcc\u7684\u6559\u7a0b\u548c\u6587\u6863\u3002<\/li>\n<\/ul>\n<p><h3>2. Keras<\/h3>\n<\/p>\n<p><p>Keras\u662f\u4e00\u4e2a\u9ad8\u7ea7\u795e\u7ecf\u7f51\u7edcAPI\uff0c\u57fa\u4e8eTensorFlow\u3002\u5b83\u7684\u8bbe\u8ba1\u7406\u5ff5\u662f\u7b80\u4fbf\u6613\u7528\uff0c\u9002\u5408\u5feb\u901f\u539f\u578b\u5f00\u53d1\u3002Keras\u7684\u4e3b\u8981\u4f18\u70b9\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6613\u7528\u6027<\/strong>\uff1aAPI\u8bbe\u8ba1\u7b80\u5355\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<\/li>\n<li><strong>\u6a21\u5757\u5316<\/strong>\uff1a\u53ef\u4ee5\u65b9\u4fbf\u5730\u6784\u5efa\u548c\u4fee\u6539\u6a21\u578b\u3002<\/li>\n<li><strong>\u517c\u5bb9\u6027<\/strong>\uff1a\u53ef\u4ee5\u4e0eTensorFlow\u65e0\u7f1d\u96c6\u6210\u3002<\/li>\n<\/ul>\n<p><h3>3. PyTorch<\/h3>\n<\/p>\n<p><p>PyTorch\u662f\u7531Facebook <a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a> Research\u5f00\u53d1\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u3002\u5b83\u4ee5\u52a8\u6001\u8ba1\u7b97\u56fe\u548c\u76f4\u89c2\u7684API\u8457\u79f0\uff0c\u975e\u5e38\u9002\u5408\u7814\u7a76\u548c\u5f00\u53d1\u3002PyTorch\u7684\u4e3b\u8981\u4f18\u70b9\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u52a8\u6001\u8ba1\u7b97\u56fe<\/strong>\uff1a\u65b9\u4fbf\u8c03\u8bd5\u548c\u6a21\u578b\u4fee\u6539\u3002<\/li>\n<li><strong>\u5f3a\u5927\u7684\u793e\u533a\u652f\u6301<\/strong>\uff1a\u6709\u4e30\u5bcc\u7684\u6559\u7a0b\u548c\u5f00\u6e90\u9879\u76ee\u3002<\/li>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u9002\u7528\u4e8e\u5404\u79cd\u6df1\u5ea6\u5b66\u4e60\u4efb\u52a1\u3002<\/li>\n<\/ul>\n<p><h3>4. Scikit-learn<\/h3>\n<\/p>\n<p><p>Scikit-learn\u662f\u4e00\u4e2a<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u5e93\uff0c\u867d\u7136\u4e0d\u4e13\u95e8\u9488\u5bf9\u6df1\u5ea6\u5b66\u4e60\uff0c\u4f46\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u7684API\uff0c\u9002\u5408\u5feb\u901f\u5b9e\u73b0\u548c\u8bc4\u4f30\u57fa\u672c\u7684\u673a\u5668\u5b66\u4e60\u6a21\u578b\u3002Scikit-learn\u7684\u4e3b\u8981\u4f18\u70b9\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7b80\u4fbf\u6027<\/strong>\uff1aAPI\u8bbe\u8ba1\u7b80\u6d01\uff0c\u9002\u5408\u521d\u5b66\u8005\u3002<\/li>\n<li><strong>\u5e7f\u6cdb\u7684\u7b97\u6cd5\u652f\u6301<\/strong>\uff1a\u5305\u62ec\u5206\u7c7b\u3001\u56de\u5f52\u3001\u805a\u7c7b\u7b49\u591a\u79cd\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u3002<\/li>\n<li><strong>\u6613\u4e8e\u96c6\u6210<\/strong>\uff1a\u53ef\u4ee5\u4e0e\u5176\u4ed6Python\u5e93\u65e0\u7f1d\u96c6\u6210\u3002<\/li>\n<\/ul>\n<p><h2>\u4e8c\u3001\u51c6\u5907\u6570\u636e<\/h2>\n<\/p>\n<p><p>\u5728\u6784\u5efa\u795e\u7ecf\u7f51\u7edc\u4e4b\u524d\uff0c\u9700\u8981\u51c6\u5907\u597d\u8bad\u7ec3\u548c\u6d4b\u8bd5\u6570\u636e\u3002\u6570\u636e\u51c6\u5907\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a<\/p>\n<\/p>\n<p><h3>1. \u6570\u636e\u6536\u96c6<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u6536\u96c6\u548c\u6574\u7406\u6570\u636e\u3002\u6570\u636e\u53ef\u4ee5\u6765\u81ea\u5404\u79cd\u6765\u6e90\uff0c\u5982\u6570\u636e\u5e93\u3001\u6587\u4ef6\u3001API\u7b49\u3002\u5bf9\u4e8e\u76d1\u7763\u5b66\u4e60\uff0c\u9700\u8981\u6709\u6807\u7b7e\u6570\u636e\uff0c\u5373\u6bcf\u4e2a\u6837\u672c\u5bf9\u5e94\u7684\u76ee\u6807\u503c\u3002<\/p>\n<\/p>\n<p><h3>2. \u6570\u636e\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u9884\u5904\u7406\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u6b65\u9aa4\uff0c\u76f4\u63a5\u5f71\u54cd\u6a21\u578b\u7684\u6027\u80fd\u3002\u4e3b\u8981\u7684\u9884\u5904\u7406\u6b65\u9aa4\u5305\u62ec\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u6570\u636e\u6e05\u6d17<\/strong>\uff1a\u53bb\u9664\u7f3a\u5931\u503c\u3001\u5f02\u5e38\u503c\u548c\u91cd\u590d\u6570\u636e\u3002<\/li>\n<li><strong>\u6570\u636e\u6807\u51c6\u5316<\/strong>\uff1a\u5c06\u6570\u636e\u7f29\u653e\u5230\u540c\u4e00\u8303\u56f4\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u6709\u5f52\u4e00\u5316\u548c\u6807\u51c6\u5316\u3002<\/li>\n<li><strong>\u6570\u636e\u5206\u5272<\/strong>\uff1a\u5c06\u6570\u636e\u5206\u4e3a\u8bad\u7ec3\u96c6\u3001\u9a8c\u8bc1\u96c6\u548c\u6d4b\u8bd5\u96c6\uff0c\u5e38\u7528\u7684\u6bd4\u4f8b\u662f8:1:1\u3002<\/li>\n<\/ul>\n<p><h3>3. \u6570\u636e\u589e\u5f3a<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u56fe\u50cf\u6570\u636e\uff0c\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u589e\u5f3a\uff0c\u5982\u65cb\u8f6c\u3001\u5e73\u79fb\u3001\u7f29\u653e\u7b49\uff0c\u4ee5\u589e\u52a0\u6570\u636e\u7684\u591a\u6837\u6027\uff0c\u4ece\u800c\u63d0\u9ad8\u6a21\u578b\u7684\u6cdb\u5316\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from sklearn.model_selection import train_test_split<\/p>\n<p>from sklearn.preprocessing import StandardScaler<\/p>\n<h2><strong>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6570\u636e\u96c6 X \u548c\u6807\u7b7e y<\/strong><\/h2>\n<p>X = np.random.rand(1000, 20)  # 1000 \u4e2a\u6837\u672c\uff0c\u6bcf\u4e2a\u6837\u672c\u6709 20 \u4e2a\u7279\u5f81<\/p>\n<p>y = np.random.randint(0, 2, 1000)  # 1000 \u4e2a\u4e8c\u5206\u7c7b\u6807\u7b7e<\/p>\n<h2><strong>\u6570\u636e\u5206\u5272<\/strong><\/h2>\n<p>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<\/p>\n<h2><strong>\u6570\u636e\u6807\u51c6\u5316<\/strong><\/h2>\n<p>scaler = StandardScaler()<\/p>\n<p>X_train = scaler.fit_transform(X_train)<\/p>\n<p>X_test = scaler.transform(X_test)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u5b9a\u4e49\u6a21\u578b<\/h2>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u6570\u636e\u51c6\u5907\u540e\uff0c\u9700\u8981\u5b9a\u4e49\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u3002\u5728Keras\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7Sequential API\u6216Functional API\u5b9a\u4e49\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><h3>1. Sequential API<\/h3>\n<\/p>\n<p><p>Sequential API\u9002\u5408\u6784\u5efa\u7ebf\u6027\u5806\u53e0\u7684\u6a21\u578b\uff0c\u5373\u6bcf\u4e00\u5c42\u7684\u8f93\u51fa\u4f5c\u4e3a\u4e0b\u4e00\u5c42\u7684\u8f93\u5165\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from keras.models import Sequential<\/p>\n<p>from keras.layers import Dense<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u987a\u5e8f\u6a21\u578b<\/strong><\/h2>\n<p>model = Sequential()<\/p>\n<h2><strong>\u6dfb\u52a0\u8f93\u5165\u5c42\u548c\u7b2c\u4e00\u4e2a\u9690\u85cf\u5c42<\/strong><\/h2>\n<p>model.add(Dense(units=64, activation=&#39;relu&#39;, input_dim=20))<\/p>\n<h2><strong>\u6dfb\u52a0\u7b2c\u4e8c\u4e2a\u9690\u85cf\u5c42<\/strong><\/h2>\n<p>model.add(Dense(units=64, activation=&#39;relu&#39;))<\/p>\n<h2><strong>\u6dfb\u52a0\u8f93\u51fa\u5c42<\/strong><\/h2>\n<p>model.add(Dense(units=1, activation=&#39;sigmoid&#39;))<\/p>\n<h2><strong>\u7f16\u8bd1\u6a21\u578b<\/strong><\/h2>\n<p>model.compile(optimizer=&#39;adam&#39;, loss=&#39;binary_crossentropy&#39;, metrics=[&#39;accuracy&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. Functional API<\/h3>\n<\/p>\n<p><p>Functional API\u9002\u5408\u6784\u5efa\u590d\u6742\u7684\u6a21\u578b\uff0c\u5982\u591a\u8f93\u5165\u591a\u8f93\u51fa\u6a21\u578b\u3001\u5171\u4eab\u5c42\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from keras.models import Model<\/p>\n<p>from keras.layers import Input, Dense<\/p>\n<h2><strong>\u5b9a\u4e49\u8f93\u5165\u5c42<\/strong><\/h2>\n<p>inputs = Input(shape=(20,))<\/p>\n<h2><strong>\u5b9a\u4e49\u7b2c\u4e00\u4e2a\u9690\u85cf\u5c42<\/strong><\/h2>\n<p>x = Dense(units=64, activation=&#39;relu&#39;)(inputs)<\/p>\n<h2><strong>\u5b9a\u4e49\u7b2c\u4e8c\u4e2a\u9690\u85cf\u5c42<\/strong><\/h2>\n<p>x = Dense(units=64, activation=&#39;relu&#39;)(x)<\/p>\n<h2><strong>\u5b9a\u4e49\u8f93\u51fa\u5c42<\/strong><\/h2>\n<p>outputs = Dense(units=1, activation=&#39;sigmoid&#39;)(x)<\/p>\n<h2><strong>\u6784\u5efa\u6a21\u578b<\/strong><\/h2>\n<p>model = Model(inputs=inputs, outputs=outputs)<\/p>\n<h2><strong>\u7f16\u8bd1\u6a21\u578b<\/strong><\/h2>\n<p>model.compile(optimizer=&#39;adam&#39;, loss=&#39;binary_crossentropy&#39;, metrics=[&#39;accuracy&#39;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u56db\u3001\u8bad\u7ec3\u6a21\u578b<\/h2>\n<\/p>\n<p><p>\u5728\u5b9a\u4e49\u597d\u6a21\u578b\u540e\uff0c\u9700\u8981\u4f7f\u7528\u8bad\u7ec3\u6570\u636e\u5bf9\u6a21\u578b\u8fdb\u884c\u8bad\u7ec3\u3002\u8bad\u7ec3\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a<\/p>\n<\/p>\n<p><h3>1. \u8bbe\u7f6e\u8d85\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u8d85\u53c2\u6570\u662f\u6307\u6a21\u578b\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u7684\u53c2\u6570\uff0c\u5982\u5b66\u4e60\u7387\u3001\u6279\u6b21\u5927\u5c0f\u3001\u8bad\u7ec3\u8f6e\u6570\u7b49\u3002\u5408\u7406\u8bbe\u7f6e\u8d85\u53c2\u6570\u53ef\u4ee5\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>2. \u8bad\u7ec3\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u8bad\u7ec3\u6570\u636e\u5bf9\u6a21\u578b\u8fdb\u884c\u8bad\u7ec3\uff0c\u5e76\u5728\u9a8c\u8bc1\u6570\u636e\u4e0a\u8bc4\u4f30\u6a21\u578b\u7684\u6027\u80fd\u3002\u8bad\u7ec3\u8fc7\u7a0b\u53ef\u4ee5\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u8fdb\u884c\u76d1\u63a7\uff0c\u5982EarlyStopping\u3001ModelCheckpoint\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from keras.callbacks import EarlyStopping, ModelCheckpoint<\/p>\n<h2><strong>\u8bbe\u7f6e\u56de\u8c03\u51fd\u6570<\/strong><\/h2>\n<p>early_stopping = EarlyStopping(monitor=&#39;val_loss&#39;, patience=10)<\/p>\n<p>model_checkpoint = ModelCheckpoint(&#39;best_model.h5&#39;, save_best_only=True)<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>history = model.fit(X_train, y_train, <\/p>\n<p>                    epochs=100, <\/p>\n<p>                    batch_size=32, <\/p>\n<p>                    validation_split=0.2, <\/p>\n<p>                    callbacks=[early_stopping, model_checkpoint])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e94\u3001\u8bc4\u4f30\u6a21\u578b<\/h2>\n<\/p>\n<p><p>\u5728\u8bad\u7ec3\u5b8c\u6210\u540e\uff0c\u9700\u8981\u4f7f\u7528\u6d4b\u8bd5\u6570\u636e\u5bf9\u6a21\u578b\u8fdb\u884c\u8bc4\u4f30\uff0c\u4ee5\u9a8c\u8bc1\u6a21\u578b\u7684\u6cdb\u5316\u80fd\u529b\u3002\u8bc4\u4f30\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a<\/p>\n<\/p>\n<p><h3>1. \u6a21\u578b\u9884\u6d4b<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u6d4b\u8bd5\u6570\u636e\u8fdb\u884c\u9884\u6d4b\uff0c\u5e76\u8ba1\u7b97\u6a21\u578b\u7684\u6027\u80fd\u6307\u6807\uff0c\u5982\u51c6\u786e\u7387\u3001\u7cbe\u786e\u7387\u3001\u53ec\u56de\u7387\u7b49\u3002<\/p>\n<\/p>\n<p><h3>2. \u6a21\u578b\u8bc4\u4f30<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u6df7\u6dc6\u77e9\u9635\u3001ROC\u66f2\u7ebf\u7b49\u53ef\u89c6\u5316\u5de5\u5177\uff0c\u5bf9\u6a21\u578b\u7684\u6027\u80fd\u8fdb\u884c\u5168\u9762\u8bc4\u4f30\u3002<\/p>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801\uff1a<\/h3>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.metrics import accuracy_score, confusion_matrix, roc_curve, auc<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u6a21\u578b\u9884\u6d4b<\/strong><\/h2>\n<p>y_pred = model.predict(X_test)<\/p>\n<p>y_pred_classes = (y_pred &gt; 0.5).astype(int)<\/p>\n<h2><strong>\u8ba1\u7b97\u51c6\u786e\u7387<\/strong><\/h2>\n<p>accuracy = accuracy_score(y_test, y_pred_classes)<\/p>\n<p>print(&#39;Accuracy:&#39;, accuracy)<\/p>\n<h2><strong>\u7ed8\u5236\u6df7\u6dc6\u77e9\u9635<\/strong><\/h2>\n<p>conf_matrix = confusion_matrix(y_test, y_pred_classes)<\/p>\n<p>plt.figure(figsize=(8, 6))<\/p>\n<p>sns.heatmap(conf_matrix, annot=True, fmt=&#39;d&#39;, cmap=&#39;Blues&#39;)<\/p>\n<p>plt.xlabel(&#39;Predicted&#39;)<\/p>\n<p>plt.ylabel(&#39;True&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u7ed8\u5236 ROC \u66f2\u7ebf<\/strong><\/h2>\n<p>fpr, tpr, _ = roc_curve(y_test, y_pred)<\/p>\n<p>roc_auc = auc(fpr, tpr)<\/p>\n<p>plt.figure(figsize=(8, 6))<\/p>\n<p>plt.plot(fpr, tpr, color=&#39;blue&#39;, label=&#39;ROC curve (area = %0.2f)&#39; % roc_auc)<\/p>\n<p>plt.plot([0, 1], [0, 1], color=&#39;gray&#39;, linestyle=&#39;--&#39;)<\/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 (ROC) Curve&#39;)<\/p>\n<p>plt.legend(loc=&#39;lower right&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u6784\u5efa\u3001\u8bad\u7ec3\u548c\u8bc4\u4f30\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u3002\u65e0\u8bba\u662f\u521d\u5b66\u8005\u8fd8\u662f\u6709\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\uff0c\u90fd\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u65b9\u6cd5\uff0c\u6784\u5efa\u51fa\u6027\u80fd\u4f18\u5f02\u7684\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u4f7f\u7528\u795e\u7ecf\u7f51\u7edc\u9700\u8981\u54ea\u4e9b\u57fa\u672c\u7684\u5e93\u548c\u5de5\u5177\uff1f<\/strong><br \/>\u5728Python\u4e2d\u6784\u5efa\u795e\u7ecf\u7f51\u7edc\uff0c\u901a\u5e38\u9700\u8981\u4e00\u4e9b\u5173\u952e\u7684\u5e93\uff0c\u5982TensorFlow\u548cKeras\uff0cPyTorch\u7b49\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\uff0c\u4f7f\u5f97\u8bbe\u8ba1\u548c\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u3002\u6b64\u5916\uff0cNumPy\u548cPandas\u7b49\u5e93\u4e5f\u5f88\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u4eec\u7528\u4e8e\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002\u786e\u4fdd\u5b89\u88c5\u8fd9\u4e9b\u5e93\u540e\uff0c\u53ef\u4ee5\u5f00\u59cb\u6784\u5efa\u548c\u8bad\u7ec3\u4f60\u7684\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u3002<\/p>\n<p><strong>\u6211\u5982\u4f55\u9009\u62e9\u9002\u5408\u6211\u7684\u9879\u76ee\u7684\u795e\u7ecf\u7f51\u7edc\u67b6\u6784\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u795e\u7ecf\u7f51\u7edc\u67b6\u6784\u901a\u5e38\u53d6\u51b3\u4e8e\u9879\u76ee\u7684\u5177\u4f53\u9700\u6c42\u3002\u5bf9\u4e8e\u56fe\u50cf\u5904\u7406\u4efb\u52a1\uff0c\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\uff08CNN\uff09\u5e38\u5e38\u662f\u9996\u9009\uff1b\u800c\u5904\u7406\u5e8f\u5217\u6570\u636e\u65f6\uff0c\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\uff08RNN\uff09\u6216\u957f\u77ed\u671f\u8bb0\u5fc6\u7f51\u7edc\uff08LSTM\uff09\u53ef\u80fd\u66f4\u4e3a\u5408\u9002\u3002\u4e86\u89e3\u4f60\u7684\u6570\u636e\u7c7b\u578b\u548c\u4efb\u52a1\u76ee\u6807\u662f\u9009\u62e9\u5408\u9002\u67b6\u6784\u7684\u5173\u952e\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u53c2\u8003\u4e00\u4e9b\u73b0\u6210\u7684\u6a21\u578b\u7ed3\u6784\uff0c\u5982ResNet\u6216VGG\uff0c\u4ee5\u6b64\u4f5c\u4e3a\u8d77\u70b9\u3002<\/p>\n<p><strong>\u5728\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u65f6\uff0c\u6211\u5e94\u8be5\u6ce8\u610f\u54ea\u4e9b\u8d85\u53c2\u6570\u7684\u8c03\u6574\uff1f<\/strong><br \/>\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u65f6\uff0c\u8d85\u53c2\u6570\u7684\u8c03\u6574\u81f3\u5173\u91cd\u8981\uff0c\u5305\u62ec\u5b66\u4e60\u7387\u3001\u6279\u91cf\u5927\u5c0f\u3001\u8bad\u7ec3\u8f6e\u6570\u548c\u7f51\u7edc\u5c42\u6570\u7b49\u3002\u5b66\u4e60\u7387\u5f71\u54cd\u6a21\u578b\u7684\u6536\u655b\u901f\u5ea6\uff0c\u6279\u91cf\u5927\u5c0f\u4f1a\u5f71\u54cd\u6a21\u578b\u7684\u7a33\u5b9a\u6027\u548c\u8bad\u7ec3\u65f6\u95f4\u3002\u9002\u5f53\u7684\u8bad\u7ec3\u8f6e\u6570\u53ef\u4ee5\u786e\u4fdd\u6a21\u578b\u5145\u5206\u5b66\u4e60\uff0c\u800c\u7f51\u7edc\u5c42\u6570\u548c\u6bcf\u5c42\u7684\u795e\u7ecf\u5143\u6570\u91cf\u5219\u51b3\u5b9a\u4e86\u6a21\u578b\u7684\u590d\u6742\u6027\u3002\u4f7f\u7528\u4ea4\u53c9\u9a8c\u8bc1\u548c\u7f51\u683c\u641c\u7d22\u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u627e\u5230\u6700\u4f73\u7684\u8d85\u53c2\u6570\u7ec4\u5408\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528\u795e\u7ecf\u7f51\u7edc\u7684\u6838\u5fc3\u8981\u70b9\u6709\uff1a\u9009\u62e9\u9002\u5f53\u7684\u5e93\u3001\u51c6\u5907\u6570\u636e\u3001\u5b9a\u4e49\u6a21\u578b\u3001\u8bad\u7ec3\u6a21\u578b\u3001\u8bc4\u4f30\u6a21\u578b\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd [&hellip;]","protected":false},"author":3,"featured_media":1032647,"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\/1032640"}],"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=1032640"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032640\/revisions"}],"predecessor-version":[{"id":1032651,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1032640\/revisions\/1032651"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1032647"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1032640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1032640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1032640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}