{"id":1063774,"date":"2024-12-31T16:03:25","date_gmt":"2024-12-31T08:03:25","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1063774.html"},"modified":"2024-12-31T16:03:27","modified_gmt":"2024-12-31T08:03:27","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%8a%8a%e9%b8%a2%e5%b0%be%e8%8a%b1%e5%88%86%e5%bc%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1063774.html","title":{"rendered":"\u5982\u4f55\u7528python\u628a\u9e22\u5c3e\u82b1\u5206\u5f00"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/5dbfe1fa-63d1-40aa-8248-3938b8fae192.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u628a\u9e22\u5c3e\u82b1\u5206\u5f00\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u5c06\u9e22\u5c3e\u82b1\u6570\u636e\u96c6\u8fdb\u884c\u5206\u7c7b\u7684\u65b9\u6cd5\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u6570\u636e\u5bfc\u5165\u3001\u6570\u636e\u9884\u5904\u7406\u3001\u7279\u5f81\u9009\u62e9\u3001\u6a21\u578b\u9009\u62e9\u4e0e\u8bad\u7ec3\u3001\u6a21\u578b\u8bc4\u4f30\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528Scikit-Learn\u5e93\u8fdb\u884c\u5206\u7c7b\u662f\u4e00\u4e2a\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5b8c\u6210\u8fd9\u4e9b\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5bfc\u5165\u6570\u636e<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165\u9e22\u5c3e\u82b1\u6570\u636e\u96c6\u3002Scikit-Learn\u5e93\u4e2d\u5df2\u7ecf\u5305\u542b\u4e86\u8fd9\u4e2a\u6570\u636e\u96c6\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5b83\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.datasets import load_iris<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u5bfc\u5165\u6570\u636e\u96c6<\/strong><\/h2>\n<p>iris = load_iris()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>data = pd.DataFrame(data=iris.data, columns=iris.feature_names)<\/p>\n<p>data[&#39;target&#39;] = iris.target<\/p>\n<h2><strong>\u663e\u793a\u524d\u51e0\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u6570\u636e\u9884\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u5206\u7c7b\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u8fdb\u884c\u4e00\u4e9b\u6570\u636e\u9884\u5904\u7406\u5de5\u4f5c\u3002\u8fd9\u5305\u62ec\u68c0\u67e5\u6570\u636e\u7684\u5b8c\u6574\u6027\u3001\u6807\u51c6\u5316\u7279\u5f81\u503c\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">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.preprocessing import StandardScaler<\/p>\n<h2><strong>\u5206\u79bb\u7279\u5f81\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>X = data.drop(&#39;target&#39;, axis=1)<\/p>\n<p>y = data[&#39;target&#39;]<\/p>\n<h2><strong>\u5c06\u6570\u636e\u96c6\u5206\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.2, random_state=42)<\/p>\n<h2><strong>\u6807\u51c6\u5316\u7279\u5f81\u503c<\/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><p>\u4e09\u3001\u9009\u62e9\u7279\u5f81<\/p>\n<\/p>\n<p><p>\u9e22\u5c3e\u82b1\u6570\u636e\u96c6\u5305\u542b\u56db\u4e2a\u7279\u5f81\uff1a\u82b1\u843c\u957f\u5ea6\u3001\u82b1\u843c\u5bbd\u5ea6\u3001\u82b1\u74e3\u957f\u5ea6\u548c\u82b1\u74e3\u5bbd\u5ea6\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u53ef\u89c6\u5316\u548c\u7edf\u8ba1\u5206\u6790\u6765\u4e86\u89e3\u8fd9\u4e9b\u7279\u5f81\u7684\u91cd\u8981\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import seaborn as sns<\/p>\n<h2><strong>\u53ef\u89c6\u5316\u7279\u5f81\u4e4b\u95f4\u7684\u5173\u7cfb<\/strong><\/h2>\n<p>sns.pairplot(data, hue=&#39;target&#39;, markers=[&quot;o&quot;, &quot;s&quot;, &quot;D&quot;])<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u8ba1\u7b97\u7279\u5f81\u7684\u76f8\u5173\u6027\u77e9\u9635<\/strong><\/h2>\n<p>correlation_matrix = data.corr()<\/p>\n<p>sns.heatmap(correlation_matrix, annot=True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u6a21\u578b\u9009\u62e9\u4e0e\u8bad\u7ec3<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u4e00\u4e2a\u5206\u7c7b\u6a21\u578b\u5e76\u8bad\u7ec3\u5b83\u3002\u6211\u4eec\u5c06\u4f7f\u7528K\u90bb\u8fd1\u7b97\u6cd5\uff08KNN\uff09\u4f5c\u4e3a\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.neighbors import KNeighborsClassifier<\/p>\n<h2><strong>\u521d\u59cb\u5316KNN\u5206\u7c7b\u5668<\/strong><\/h2>\n<p>knn = KNeighborsClassifier(n_neighbors=3)<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>knn.fit(X_train, y_train)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u6a21\u578b\u8bc4\u4f30<\/p>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u8bc4\u4f30\u6a21\u578b\u7684\u6027\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528\u6df7\u6dc6\u77e9\u9635\u3001\u51c6\u786e\u7387\u3001\u53ec\u56de\u7387\u3001F1\u5206\u6570\u7b49\u6307\u6807\u6765\u8bc4\u4f30\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.metrics import confusion_matrix, classification_report, accuracy_score<\/p>\n<h2><strong>\u9884\u6d4b\u6d4b\u8bd5\u96c6<\/strong><\/h2>\n<p>y_pred = knn.predict(X_test)<\/p>\n<h2><strong>\u8ba1\u7b97\u6df7\u6dc6\u77e9\u9635<\/strong><\/h2>\n<p>conf_matrix = confusion_matrix(y_test, y_pred)<\/p>\n<p>print(&quot;Confusion Matrix:\\n&quot;, conf_matrix)<\/p>\n<h2><strong>\u751f\u6210\u5206\u7c7b\u62a5\u544a<\/strong><\/h2>\n<p>class_report = classification_report(y_test, y_pred)<\/p>\n<p>print(&quot;Classification Report:\\n&quot;, class_report)<\/p>\n<h2><strong>\u8ba1\u7b97\u51c6\u786e\u7387<\/strong><\/h2>\n<p>accuracy = accuracy_score(y_test, y_pred)<\/p>\n<p>print(&quot;Accuracy:&quot;, accuracy)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528Python\u6210\u529f\u5730\u5bf9\u9e22\u5c3e\u82b1\u6570\u636e\u96c6\u8fdb\u884c\u5206\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u6a21\u578b\u7684\u8bc4\u4f30\u7ed3\u679c\u8c03\u6574\u53c2\u6570\u6216\u9009\u62e9\u5176\u4ed6\u6a21\u578b\uff0c\u4ee5\u63d0\u5347\u5206\u7c7b\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5bfc\u5165\u9e22\u5c3e\u82b1\u6570\u636e\u96c6\u662f\u6211\u4eec\u8fdb\u884c\u5206\u7c7b\u7684\u7b2c\u4e00\u6b65\u3002Scikit-Learn\u5e93\u4e2d\u7684<code>load_iris<\/code>\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u5730\u52a0\u8f7d\u8fd9\u4e2a\u6570\u636e\u96c6\u3002\u4f7f\u7528Pandas\u5e93\u5c06\u6570\u636e\u8f6c\u6362\u6210DataFrame\u683c\u5f0f\uff0c\u4ee5\u4fbf\u4e8e\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.datasets import load_iris<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u5bfc\u5165\u6570\u636e\u96c6<\/strong><\/h2>\n<p>iris = load_iris()<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>data = pd.DataFrame(data=iris.data, columns=iris.feature_names)<\/p>\n<p>data[&#39;target&#39;] = iris.target<\/p>\n<h2><strong>\u663e\u793a\u524d\u51e0\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>load_iris<\/code>\u51fd\u6570\u52a0\u8f7d\u6570\u636e\u96c6\u3002\u6211\u4eec\u5c06\u6570\u636e\u96c6\u8f6c\u6362\u4e3aDataFrame\uff0c\u5e76\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u5217<code>target<\/code>\u6765\u5b58\u50a8\u6807\u7b7e\u3002\u6700\u540e\uff0c\u6211\u4eec\u663e\u793a\u524d\u51e0\u884c\u6570\u636e\u4ee5\u786e\u8ba4\u6570\u636e\u96c6\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u6570\u636e\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u9884\u5904\u7406\u662f<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u6784\u5efa\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u6211\u4eec\u9700\u8981\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u7279\u5f81\u6807\u51c6\u5316\u7b49\u64cd\u4f5c\uff0c\u4ee5\u786e\u4fdd\u6a21\u578b\u80fd\u591f\u6709\u6548\u5730\u5b66\u4e60\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.model_selection import train_test_split<\/p>\n<p>from sklearn.preprocessing import StandardScaler<\/p>\n<h2><strong>\u5206\u79bb\u7279\u5f81\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>X = data.drop(&#39;target&#39;, axis=1)<\/p>\n<p>y = data[&#39;target&#39;]<\/p>\n<h2><strong>\u5c06\u6570\u636e\u96c6\u5206\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.2, random_state=42)<\/p>\n<h2><strong>\u6807\u51c6\u5316\u7279\u5f81\u503c<\/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><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5c06\u7279\u5f81\u548c\u6807\u7b7e\u5206\u79bb\u5f00\u6765\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>train_test_split<\/code>\u51fd\u6570\u5c06\u6570\u636e\u96c6\u5212\u5206\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528<code>StandardScaler<\/code>\u5bf9\u7279\u5f81\u503c\u8fdb\u884c\u6807\u51c6\u5316\u5904\u7406\uff0c\u4ee5\u6d88\u9664\u4e0d\u540c\u7279\u5f81\u91cf\u7eb2\u4e4b\u95f4\u7684\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u9009\u62e9\u7279\u5f81<\/h3>\n<\/p>\n<p><p>\u7279\u5f81\u9009\u62e9\u662f\u4e3a\u4e86\u51cf\u5c11\u6570\u636e\u7ef4\u5ea6\uff0c\u63d0\u9ad8\u6a21\u578b\u7684\u8bad\u7ec3\u901f\u5ea6\u548c\u51c6\u786e\u6027\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u53ef\u89c6\u5316\u548c\u7edf\u8ba1\u5206\u6790\u6765\u8bc4\u4f30\u7279\u5f81\u7684\u91cd\u8981\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import seaborn as sns<\/p>\n<h2><strong>\u53ef\u89c6\u5316\u7279\u5f81\u4e4b\u95f4\u7684\u5173\u7cfb<\/strong><\/h2>\n<p>sns.pairplot(data, hue=&#39;target&#39;, markers=[&quot;o&quot;, &quot;s&quot;, &quot;D&quot;])<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u8ba1\u7b97\u7279\u5f81\u7684\u76f8\u5173\u6027\u77e9\u9635<\/strong><\/h2>\n<p>correlation_matrix = data.corr()<\/p>\n<p>sns.heatmap(correlation_matrix, annot=True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528Seaborn\u5e93\u521b\u5efa\u7279\u5f81\u5bf9\u4e4b\u95f4\u7684\u6563\u70b9\u56fe\u548c\u76f8\u5173\u6027\u70ed\u56fe\u3002\u901a\u8fc7\u8fd9\u4e9b\u56fe\u5f62\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u89c2\u5730\u770b\u5230\u7279\u5f81\u4e4b\u95f4\u7684\u5173\u7cfb\u548c\u76f8\u5173\u6027\uff0c\u4ece\u800c\u5e2e\u52a9\u6211\u4eec\u9009\u62e9\u66f4\u91cd\u8981\u7684\u7279\u5f81\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6a21\u578b\u9009\u62e9\u4e0e\u8bad\u7ec3<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b\u8fdb\u884c\u8bad\u7ec3\u662f\u5206\u7c7b\u4efb\u52a1\u7684\u5173\u952e\u6b65\u9aa4\u3002\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528K\u90bb\u8fd1\u7b97\u6cd5\uff08KNN\uff09\u4f5c\u4e3a\u793a\u4f8b\u8fdb\u884c\u5206\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.neighbors import KNeighborsClassifier<\/p>\n<h2><strong>\u521d\u59cb\u5316KNN\u5206\u7c7b\u5668<\/strong><\/h2>\n<p>knn = KNeighborsClassifier(n_neighbors=3)<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>knn.fit(X_train, y_train)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521d\u59cb\u5316\u4e00\u4e2aKNN\u5206\u7c7b\u5668\uff0c\u5e76\u8bbe\u7f6e\u90bb\u8fd1\u70b9\u7684\u6570\u91cf\u4e3a3\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u8bad\u7ec3\u96c6\u8bad\u7ec3\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u6a21\u578b\u8bc4\u4f30<\/h3>\n<\/p>\n<p><p>\u8bc4\u4f30\u6a21\u578b\u7684\u6027\u80fd\u662f\u673a\u5668\u5b66\u4e60\u6d41\u7a0b\u4e2d\u7684\u91cd\u8981\u4e00\u6b65\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6df7\u6dc6\u77e9\u9635\u3001\u5206\u7c7b\u62a5\u544a\u548c\u51c6\u786e\u7387\u7b49\u6307\u6807\u6765\u8bc4\u4f30\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.metrics import confusion_matrix, classification_report, accuracy_score<\/p>\n<h2><strong>\u9884\u6d4b\u6d4b\u8bd5\u96c6<\/strong><\/h2>\n<p>y_pred = knn.predict(X_test)<\/p>\n<h2><strong>\u8ba1\u7b97\u6df7\u6dc6\u77e9\u9635<\/strong><\/h2>\n<p>conf_matrix = confusion_matrix(y_test, y_pred)<\/p>\n<p>print(&quot;Confusion Matrix:\\n&quot;, conf_matrix)<\/p>\n<h2><strong>\u751f\u6210\u5206\u7c7b\u62a5\u544a<\/strong><\/h2>\n<p>class_report = classification_report(y_test, y_pred)<\/p>\n<p>print(&quot;Classification Report:\\n&quot;, class_report)<\/p>\n<h2><strong>\u8ba1\u7b97\u51c6\u786e\u7387<\/strong><\/h2>\n<p>accuracy = accuracy_score(y_test, y_pred)<\/p>\n<p>print(&quot;Accuracy:&quot;, accuracy)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528\u6d4b\u8bd5\u96c6\u8fdb\u884c\u9884\u6d4b\uff0c\u7136\u540e\u8ba1\u7b97\u6df7\u6dc6\u77e9\u9635\u3001\u751f\u6210\u5206\u7c7b\u62a5\u544a\u5e76\u8ba1\u7b97\u51c6\u786e\u7387\u3002\u901a\u8fc7\u8fd9\u4e9b\u8bc4\u4f30\u6307\u6807\uff0c\u6211\u4eec\u53ef\u4ee5\u4e86\u89e3\u6a21\u578b\u7684\u6027\u80fd\uff0c\u5e76\u6839\u636e\u7ed3\u679c\u8fdb\u884c\u76f8\u5e94\u7684\u8c03\u6574\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u6a21\u578b\u4f18\u5316\u4e0e\u8c03\u53c2<\/h3>\n<\/p>\n<p><p>\u5728\u521d\u6b65\u8bc4\u4f30\u6a21\u578b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u6a21\u578b\u53c2\u6570\u6765\u8fdb\u4e00\u6b65\u4f18\u5316\u6a21\u578b\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8eKNN\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u6574<code>n_neighbors<\/code>\u53c2\u6570\u7684\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.model_selection import GridSearchCV<\/p>\n<h2><strong>\u5b9a\u4e49\u53c2\u6570\u7f51\u683c<\/strong><\/h2>\n<p>param_grid = {&#39;n_neighbors&#39;: [1, 3, 5, 7, 9, 11, 13, 15]}<\/p>\n<h2><strong>\u4f7f\u7528\u7f51\u683c\u641c\u7d22\u8fdb\u884c\u53c2\u6570\u8c03\u4f18<\/strong><\/h2>\n<p>grid_search = GridSearchCV(KNeighborsClassifier(), param_grid, cv=5)<\/p>\n<p>grid_search.fit(X_train, y_train)<\/p>\n<h2><strong>\u8f93\u51fa\u6700\u4f73\u53c2\u6570<\/strong><\/h2>\n<p>print(&quot;Best Parameters:&quot;, grid_search.best_params_)<\/p>\n<h2><strong>\u4f7f\u7528\u6700\u4f73\u53c2\u6570\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>best_knn = grid_search.best_estimator_<\/p>\n<p>best_knn.fit(X_train, y_train)<\/p>\n<h2><strong>\u8bc4\u4f30\u4f18\u5316\u540e\u7684\u6a21\u578b<\/strong><\/h2>\n<p>y_pred_optimized = best_knn.predict(X_test)<\/p>\n<p>optimized_accuracy = accuracy_score(y_test, y_pred_optimized)<\/p>\n<p>print(&quot;Optimized Accuracy:&quot;, optimized_accuracy)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>GridSearchCV<\/code>\u8fdb\u884c\u53c2\u6570\u8c03\u4f18\u3002\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53c2\u6570\u7f51\u683c\uff0c\u5176\u4e2d\u5305\u542b\u4e0d\u540c\u7684<code>n_neighbors<\/code>\u503c\u3002\u901a\u8fc7\u4ea4\u53c9\u9a8c\u8bc1\uff0c\u6211\u4eec\u53ef\u4ee5\u627e\u5230\u6700\u4f73\u53c2\u6570\u7ec4\u5408\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u6700\u4f73\u53c2\u6570\u8bad\u7ec3\u6a21\u578b\uff0c\u5e76\u8bc4\u4f30\u4f18\u5316\u540e\u7684\u6a21\u578b\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5176\u4ed6\u5206\u7c7b\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86KNN\u7b97\u6cd5\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c1d\u8bd5\u5176\u4ed6\u5206\u7c7b\u7b97\u6cd5\uff0c\u5982\u652f\u6301\u5411\u91cf\u673a\uff08SVM\uff09\u3001\u51b3\u7b56\u6811\u3001\u968f\u673a\u68ee\u6797\u7b49\u3002\u8fd9\u4e9b\u7b97\u6cd5\u5728\u4e0d\u540c\u7684\u6570\u636e\u96c6\u548c\u4efb\u52a1\u4e2d\u53ef\u80fd\u8868\u73b0\u51fa\u4e0d\u540c\u7684\u4f18\u52a3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.svm import SVC<\/p>\n<p>from sklearn.tree import DecisionTreeClassifier<\/p>\n<p>from sklearn.ensemble import RandomForestClassifier<\/p>\n<h2><strong>\u521d\u59cb\u5316\u4e0d\u540c\u7684\u5206\u7c7b\u5668<\/strong><\/h2>\n<p>svc = SVC()<\/p>\n<p>decision_tree = DecisionTreeClassifier()<\/p>\n<p>random_forest = RandomForestClassifier()<\/p>\n<h2><strong>\u8bad\u7ec3\u548c\u8bc4\u4f30SVM<\/strong><\/h2>\n<p>svc.fit(X_train, y_train)<\/p>\n<p>y_pred_svc = svc.predict(X_test)<\/p>\n<p>svc_accuracy = accuracy_score(y_test, y_pred_svc)<\/p>\n<p>print(&quot;SVM Accuracy:&quot;, svc_accuracy)<\/p>\n<h2><strong>\u8bad\u7ec3\u548c\u8bc4\u4f30\u51b3\u7b56\u6811<\/strong><\/h2>\n<p>decision_tree.fit(X_train, y_train)<\/p>\n<p>y_pred_tree = decision_tree.predict(X_test)<\/p>\n<p>tree_accuracy = accuracy_score(y_test, y_pred_tree)<\/p>\n<p>print(&quot;Decision Tree Accuracy:&quot;, tree_accuracy)<\/p>\n<h2><strong>\u8bad\u7ec3\u548c\u8bc4\u4f30\u968f\u673a\u68ee\u6797<\/strong><\/h2>\n<p>random_forest.fit(X_train, y_train)<\/p>\n<p>y_pred_forest = random_forest.predict(X_test)<\/p>\n<p>forest_accuracy = accuracy_score(y_test, y_pred_forest)<\/p>\n<p>print(&quot;Random Forest Accuracy:&quot;, forest_accuracy)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5206\u522b\u521d\u59cb\u5316\u4e86SVM\u3001\u51b3\u7b56\u6811\u548c\u968f\u673a\u68ee\u6797\u5206\u7c7b\u5668\uff0c\u5e76\u4f7f\u7528\u8bad\u7ec3\u96c6\u5bf9\u5b83\u4eec\u8fdb\u884c\u8bad\u7ec3\u3002\u7136\u540e\uff0c\u6211\u4eec\u8bc4\u4f30\u6bcf\u4e2a\u6a21\u578b\u7684\u6027\u80fd\uff0c\u5e76\u6bd4\u8f83\u5b83\u4eec\u7684\u51c6\u786e\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u6210\u529f\u5730\u5bf9\u9e22\u5c3e\u82b1\u6570\u636e\u96c6\u8fdb\u884c\u5206\u7c7b\u3002\u6211\u4eec\u4ece\u6570\u636e\u5bfc\u5165\u3001\u9884\u5904\u7406\u3001\u7279\u5f81\u9009\u62e9\u3001\u6a21\u578b\u9009\u62e9\u4e0e\u8bad\u7ec3\u3001\u6a21\u578b\u8bc4\u4f30\u3001\u6a21\u578b\u4f18\u5316\u4e0e\u8c03\u53c2\u3001\u5c1d\u8bd5\u5176\u4ed6\u5206\u7c7b\u7b97\u6cd5\u7b49\u65b9\u9762\u8fdb\u884c\u4e86\u8be6\u7ec6\u7684\u4ecb\u7ecd\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u673a\u5668\u5b66\u4e60\u5206\u7c7b\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\uff0c\u5e76\u4e0d\u65ad\u4f18\u5316\u6a21\u578b\uff0c\u4ee5\u8fbe\u5230\u6700\u4f73\u7684\u5206\u7c7b\u6548\u679c\u3002\u901a\u8fc7\u4e0d\u65ad\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u6211\u4eec\u53ef\u4ee5\u63d0\u9ad8\u5bf9\u673a\u5668\u5b66\u4e60\u7684\u7406\u89e3\u548c\u5e94\u7528\u80fd\u529b\uff0c\u4ece\u800c\u89e3\u51b3\u66f4\u591a\u5b9e\u9645\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u7528Python\u5b9e\u73b0\u9e22\u5c3e\u82b1\u6570\u636e\u96c6\u7684\u5206\u7c7b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u673a\u5668\u5b66\u4e60\u5e93\u5982scikit-learn\u6765\u5bf9\u9e22\u5c3e\u82b1\u6570\u636e\u96c6\u8fdb\u884c\u5206\u7c7b\u3002\u9996\u5148\uff0c\u5bfc\u5165\u76f8\u5e94\u7684\u5e93\uff0c\u52a0\u8f7d\u6570\u636e\u96c6\uff0c\u7136\u540e\u9009\u62e9\u5408\u9002\u7684\u5206\u7c7b\u7b97\u6cd5\uff0c\u6bd4\u5982\u51b3\u7b56\u6811\u3001\u652f\u6301\u5411\u91cf\u673a\u6216\u968f\u673a\u68ee\u6797\u3002\u63a5\u4e0b\u6765\uff0c\u5206\u5272\u6570\u636e\u96c6\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\uff0c\u8bad\u7ec3\u6a21\u578b\u5e76\u8bc4\u4f30\u5176\u51c6\u786e\u6027\uff0c\u6700\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u53ef\u89c6\u5316\u5de5\u5177\u6765\u5c55\u793a\u5206\u7c7b\u7ed3\u679c\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u7528\u7684\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u53ef\u4ee5\u7528\u6765\u5206\u7c7b\u9e22\u5c3e\u82b1\uff1f<\/strong><br \/>\u5206\u7c7b\u9e22\u5c3e\u82b1\u65f6\uff0c\u5e38\u89c1\u7684\u7b97\u6cd5\u5305\u62ec\u51b3\u7b56\u6811\u3001\u968f\u673a\u68ee\u6797\u3001K\u8fd1\u90bb\uff08KNN\uff09\u3001\u652f\u6301\u5411\u91cf\u673a\uff08SVM\uff09\u548c\u795e\u7ecf\u7f51\u7edc\u7b49\u3002\u8fd9\u4e9b\u7b97\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u6570\u636e\u96c6\u7684\u7279\u70b9\u548c\u9700\u6c42\u8fdb\u884c\u9009\u62e9\u3002\u51b3\u7b56\u6811\u6613\u4e8e\u7406\u89e3\uff0c\u968f\u673a\u68ee\u6797\u53ef\u4ee5\u63d0\u9ad8\u51c6\u786e\u7387\uff0cK\u8fd1\u90bb\u7b80\u5355\u76f4\u89c2\uff0c\u800c\u652f\u6301\u5411\u91cf\u673a\u9002\u5408\u5904\u7406\u9ad8\u7ef4\u6570\u636e\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bc4\u4f30\u5206\u7c7b\u6a21\u578b\u7684\u6548\u679c\uff1f<\/strong><br \/>\u8bc4\u4f30\u5206\u7c7b\u6a21\u578b\u7684\u6548\u679c\u901a\u5e38\u4f7f\u7528\u51c6\u786e\u7387\u3001\u53ec\u56de\u7387\u3001F1-score\u548c\u6df7\u6dc6\u77e9\u9635\u7b49\u6307\u6807\u3002\u51c6\u786e\u7387\u53cd\u6620\u4e86\u6b63\u786e\u9884\u6d4b\u7684\u6837\u672c\u5360\u603b\u6837\u672c\u7684\u6bd4\u4f8b\uff0c\u53ec\u56de\u7387\u8868\u793a\u6b63\u786e\u8bc6\u522b\u7684\u6b63\u6837\u672c\u5360\u6240\u6709\u6b63\u6837\u672c\u7684\u6bd4\u4f8b\uff0cF1-score\u662f\u51c6\u786e\u7387\u548c\u53ec\u56de\u7387\u7684\u8c03\u548c\u5e73\u5747\u6570\uff0c\u800c\u6df7\u6dc6\u77e9\u9635\u5219\u63d0\u4f9b\u4e86\u6a21\u578b\u5728\u5404\u4e2a\u7c7b\u522b\u4e0a\u7684\u9884\u6d4b\u60c5\u51b5\u3002\u53ef\u4ee5\u4f7f\u7528scikit-learn\u4e2d\u7684\u76f8\u5173\u51fd\u6570\u6765\u8ba1\u7b97\u8fd9\u4e9b\u6307\u6807\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u5c06\u9e22\u5c3e\u82b1\u6570\u636e\u96c6\u8fdb\u884c\u5206\u7c7b\u7684\u65b9\u6cd5\u5305\u62ec\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u6570\u636e\u5bfc\u5165\u3001\u6570\u636e\u9884\u5904\u7406\u3001\u7279\u5f81\u9009\u62e9\u3001\u6a21\u578b\u9009\u62e9\u4e0e\u8bad\u7ec3\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1063779,"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\/1063774"}],"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=1063774"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1063774\/revisions"}],"predecessor-version":[{"id":1063780,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1063774\/revisions\/1063780"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1063779"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1063774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1063774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1063774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}