{"id":3773,"date":"2025-10-11T22:06:08","date_gmt":"2025-10-11T14:06:08","guid":{"rendered":"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svm%e5%a4%9a%e5%88%86%e7%b1%bb%e7%ae%97%e6%b3%95%e3%80%90python%e3%80%91\/"},"modified":"2025-10-12T00:15:02","modified_gmt":"2025-10-11T16:15:02","slug":"svmclasspy","status":"publish","type":"post","link":"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/","title":{"rendered":"SVM\u591a\u5206\u7c7b\u7b97\u6cd5 &#8211; \u57fa\u4e8e\u652f\u6301\u5411\u91cf\u673a\u7684\u591a\u7c7b\u522b\u5206\u7c7b\u3010Python\u3011"},"content":{"rendered":"\n\n<h4 class=\"wp-block-heading\">\u4e00\u3001\u4ee3\u7801\u8fd0\u884c\u73af\u5883<\/h4>\n\n\n\n\n<ul>\n<li><strong>Python<\/strong>: Python 3.8\u53ca\u4ee5\u4e0a\u7248\u672c<\/li>\n<li><strong>\u4f9d\u8d56\u5e93<\/strong>:<\/li>\n<li><code>numpy &gt;= 1.21.0<\/code> &#8211; \u6570\u503c\u8ba1\u7b97\u5e93<\/li>\n<li><code>pandas &gt;= 1.3.0<\/code> &#8211; \u6570\u636e\u5904\u7406\u5e93<\/li>\n<li><code>matplotlib &gt;= 3.4.0<\/code> &#8211; \u6570\u636e\u53ef\u89c6\u5316\u5e93<\/li>\n<li><code>scikit-learn &gt;= 1.0.0<\/code> &#8211; \u673a\u5668\u5b66\u4e60\u5e93<\/li>\n<\/ul>\n\n\n\n\n<h4 class=\"wp-block-heading\">\u4e8c\u3001\u7a0b\u5e8f\u4ecb\u7ecd<\/h4>\n\n\n\n\n<h5 class=\"wp-block-heading\">\u7a0b\u5e8f\u6587\u4ef6\u7ed3\u6784<\/h5>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-text\">SVM_Multiclass_Classification\/\n\u251c\u2500\u2500 demoSVMMultiClass.py         # \u6f14\u793a\u811a\u672c\n\u251c\u2500\u2500 FunSVMMultiClass.py          # \u6838\u5fc3\u51fd\u6570\n\u251c\u2500\u2500 iris.csv                     # \u9e22\u5c3e\u82b1\u6570\u636e\u96c6\n\u251c\u2500\u2500 requirements.txt             # \u4f9d\u8d56\u5e93\u5217\u8868\n\u251c\u2500\u2500 \u4ee3\u7801\u8bf4\u660e.txt                  # \u4ee3\u7801\u8bf4\u660e\u6587\u6863\n\u2514\u2500\u2500 figure\/                      # \u7ed3\u679c\u56fe\u7247\u6587\u4ef6\u5939\n    \u251c\u2500\u2500 \u56fe1_\u6df7\u6dc6\u77e9\u9635.png\n    \u251c\u2500\u2500 \u56fe2_\u5206\u7c7b\u5bf9\u6bd4.png\n    \u251c\u2500\u2500 \u56fe3_\u6027\u80fd\u6307\u6807.png\n    \u2514\u2500\u2500 \u56fe4_\u6570\u636e\u5206\u5e03.png<\/code><\/pre>\n\n\n\n\n<h5 class=\"wp-block-heading\">\u6587\u4ef6\u8bf4\u660e<\/h5>\n\n\n\n\n<p>1. <strong>demoSVMMultiClass.py<\/strong><\/p>\n\n\n\n\n<ul>\n<li>\u8bf4\u660e\uff1aSVM\u591a\u5206\u7c7b\u7b97\u6cd5\u7684\u6f14\u793a\u811a\u672c\u6587\u4ef6\uff0c\u662f\u6838\u5fc3\u51fd\u6570 <code>FunSVMMultiClass<\/code> \u7684\u6d4b\u8bd5\u6587\u4ef6\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\u3002<\/li>\n<li>\u529f\u80fd\uff1a\u52a0\u8f7d\u9e22\u5c3e\u82b1\u6570\u636e\u96c6\uff0c\u8bbe\u7f6e\u7b97\u6cd5\u53c2\u6570\uff0c\u8c03\u7528\u6838\u5fc3\u51fd\u6570\u8fdb\u884c\u591a\u5206\u7c7b\u8bad\u7ec3\u548c\u6d4b\u8bd5\uff0c\u8f93\u51fa\u6027\u80fd\u6307\u6807\u5e76\u751f\u6210\u53ef\u89c6\u5316\u7ed3\u679c\u3002<\/li>\n<li>\u8fd0\u884c\u7ed3\u679c\uff1a<\/li>\n<\/ul>\n\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1988\" height=\"1771\" src=\"https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig1.png\" alt=\"\u56fe1\uff1aSVM\u591a\u5206\u7c7b\u6df7\u6dc6\u77e9\u9635\" class=\"wp-image-3786\" srcset=\"https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig1.png 1988w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig1-300x267.png 300w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig1-1024x912.png 1024w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig1-768x684.png 768w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig1-1536x1368.png 1536w\" sizes=\"(max-width: 1988px) 100vw, 1988px\" \/><figcaption class=\"wp-element-caption\">\u56fe1\uff1aSVM\u591a\u5206\u7c7b\u6df7\u6dc6\u77e9\u9635\uff0c\u5c55\u793a\u4e86\u5404\u7c7b\u522b\u7684\u5206\u7c7b\u51c6\u786e\u6027\uff0c\u5bf9\u89d2\u7ebf\u5143\u7d20\u8868\u793a\u6b63\u786e\u5206\u7c7b\u7684\u6837\u672c\u6570<\/figcaption><\/figure>\n\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"2968\" height=\"1465\" src=\"https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig2.png\" alt=\"\u56fe2\uff1a\u771f\u5b9e\u7c7b\u522b\u4e0e\u9884\u6d4b\u7c7b\u522b\u5bf9\u6bd4\u56fe\" class=\"wp-image-3787\" srcset=\"https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig2.png 2968w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig2-300x148.png 300w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig2-1024x505.png 1024w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig2-768x379.png 768w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig2-1536x758.png 1536w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig2-2048x1011.png 2048w\" sizes=\"(max-width: 2968px) 100vw, 2968px\" \/><figcaption class=\"wp-element-caption\">\u56fe2\uff1a\u771f\u5b9e\u7c7b\u522b\u4e0e\u9884\u6d4b\u7c7b\u522b\u5bf9\u6bd4\u56fe\uff0c\u84dd\u8272\u5706\u5708\u8868\u793a\u771f\u5b9e\u6807\u7b7e\uff0c\u7ea2\u8272\u53c9\u53f7\u8868\u793a\u9884\u6d4b\u6807\u7b7e<\/figcaption><\/figure>\n\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"2668\" height=\"1772\" src=\"https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig3.png\" alt=\"\u56fe3\uff1a\u5404\u7c7b\u522b\u6027\u80fd\u6307\u6807\u5bf9\u6bd4\" class=\"wp-image-3788\" srcset=\"https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig3.png 2668w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig3-300x199.png 300w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig3-1024x680.png 1024w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig3-768x510.png 768w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig3-1536x1020.png 1536w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig3-2048x1360.png 2048w\" sizes=\"(max-width: 2668px) 100vw, 2668px\" \/><figcaption class=\"wp-element-caption\">\u56fe3\uff1a\u5404\u7c7b\u522b\u6027\u80fd\u6307\u6807\u5bf9\u6bd4\uff0c\u5305\u62ec\u7cbe\u786e\u7387\u3001\u53ec\u56de\u7387\u548cF1\u5206\u6570<\/figcaption><\/figure>\n\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"4167\" height=\"1766\" src=\"https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig4.png\" alt=\"\u56fe4\uff1a\u6570\u636e\u5206\u5e03\u6563\u70b9\u56fe\" class=\"wp-image-3789\" srcset=\"https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig4.png 4167w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig4-300x127.png 300w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig4-1024x434.png 1024w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig4-768x325.png 768w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig4-1536x651.png 1536w, https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig4-2048x868.png 2048w\" sizes=\"(max-width: 4167px) 100vw, 4167px\" \/><figcaption class=\"wp-element-caption\">\u56fe4\uff1a\u6570\u636e\u5206\u5e03\u6563\u70b9\u56fe\uff08\u901a\u8fc7PCA\u964d\u7ef4\u52302\u7ef4\uff09\uff0c\u4e0d\u540c\u989c\u8272\u8868\u793a\u4e0d\u540c\u7c7b\u522b<\/figcaption><\/figure>\n\n\n\n\n<p>\u547d\u4ee4\u884c\u8f93\u51fa\u793a\u4f8b\uff1a<\/p>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-text\">     \u5f00\u59cb\u52a0\u8f7d\u6570\u636e...\n     \u6570\u636e\u52a0\u8f7d\u5b8c\u6210\uff01\n     \u6570\u636e\u96c6\u5927\u5c0f\uff1a150\u4e2a\u6837\u672c\uff0c4\u4e2a\u7279\u5f81\n\n     ===== SVM\u591a\u5206\u7c7b\u7b97\u6cd5 =====\n     \u8bad\u7ec3\u96c6\u6837\u672c\u6570: 105\n     \u6d4b\u8bd5\u96c6\u6837\u672c\u6570: 45\n     \u7c7b\u522b\u6570\u91cf: 3\n\n     ===== \u5206\u7c7b\u7ed3\u679c =====\n     \u6d4b\u8bd5\u96c6\u51c6\u786e\u7387: 100.00%\n     \u7cbe\u786e\u7387: 100.00%\n     \u53ec\u56de\u7387: 100.00%\n     F1\u5206\u6570: 100.00%\n\n     \u5404\u7c7b\u522b\u6027\u80fd\u6307\u6807\uff1a\n     \u7c7b\u522b Iris-setosa: \u7cbe\u786e\u7387=100.00%, \u53ec\u56de\u7387=100.00%, F1=100.00%\n     \u7c7b\u522b Iris-versicolor: \u7cbe\u786e\u7387=100.00%, \u53ec\u56de\u7387=100.00%, F1=100.00%\n     \u7c7b\u522b Iris-virginica: \u7cbe\u786e\u7387=100.00%, \u53ec\u56de\u7387=100.00%, F1=100.00%\n\n     \u6240\u6709\u56fe\u7247\u5df2\u4fdd\u5b58\u5230figure\u6587\u4ef6\u5939\n\n     \u6f14\u793a\u5b8c\u6210\uff01<\/code><\/pre>\n\n\n\n<p>2. <strong>FunSVMMultiClass.py<\/strong><\/p>\n\n\n\n\n<ul>\n<li>\u8bf4\u660e\uff1aSVM\u591a\u5206\u7c7b\u7b97\u6cd5\u7684\u6838\u5fc3\u51fd\u6570\uff0c\u5b9e\u73b0\u4e86\u5b8c\u6574\u7684\u591a\u7c7b\u522b\u5206\u7c7b\u6d41\u7a0b\u3002<\/li>\n<li>\u529f\u80fd\uff1a<\/li>\n<\/ul>\n\n\n\n\n<ul>\n<li>\u652f\u6301\u591a\u79cd\u6838\u51fd\u6570\uff08\u7ebf\u6027\u6838\u3001RBF\u6838\u3001\u591a\u9879\u5f0f\u6838\uff09<\/li>\n<li>\u91c7\u7528One-vs-Rest\uff08OvR\uff09\u7b56\u7565\u8fdb\u884c\u591a\u5206\u7c7b<\/li>\n<li>\u81ea\u52a8\u6570\u636e\u6807\u51c6\u5316\u548c\u8bad\u7ec3\u96c6\/\u6d4b\u8bd5\u96c6\u5212\u5206<\/li>\n<li>\u5b8c\u6574\u7684\u6027\u80fd\u8bc4\u4f30\uff08\u51c6\u786e\u7387\u3001\u7cbe\u786e\u7387\u3001\u53ec\u56de\u7387\u3001F1\u5206\u6570\uff09<\/li>\n<li>\u751f\u62105\u79cd\u53ef\u89c6\u5316\u7ed3\u679c\uff08\u6df7\u6dc6\u77e9\u9635\u3001\u5206\u7c7b\u5bf9\u6bd4\u3001\u6027\u80fd\u6307\u6807\u3001\u6570\u636e\u5206\u5e03\u3001\u51b3\u7b56\u8fb9\u754c\uff09<\/li>\n<li>\u652f\u6301\u4e2d\u6587\u5b57\u4f53\u663e\u793a<\/li>\n<li>\u6240\u6709\u56fe\u7247\u81ea\u52a8\u4fdd\u5b58\u5230figure\u6587\u4ef6\u5939<\/li>\n<li>\u51fd\u6570\u5b9a\u4e49\u53ca\u53c2\u6570\u89e3\u91ca\uff1a<\/li>\n<\/ul>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">     def FunSVMMultiClass(X: np.ndarray, \n                          y: np.ndarray, \n                          options: Optional[Dict[str, Any]] = None) -&gt; Tuple[float, float, float, float, List[SVC], Dict[str, Any]]:\n         &quot;&quot;&quot;\n         SVM\u591a\u5206\u7c7b\u7b97\u6cd5\u51fd\u6570\n\n         Parameters:\n         -----------\n         X : np.ndarray\n             \u7279\u5f81\u6570\u636e\uff0cshape\u4e3a(n_samples, n_features)\n         y : np.ndarray\n             \u6807\u7b7e\u6570\u636e\uff0cshape\u4e3a(n_samples,)\n         options : Dict[str, Any], optional\n             \u53c2\u6570\u8bbe\u7f6e\u5b57\u5178\uff0c\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1a\n             - test_size: \u6d4b\u8bd5\u96c6\u6bd4\u4f8b\uff0c\u9ed8\u8ba40.3\uff0830%\u6d4b\u8bd5\uff0c70%\u8bad\u7ec3\uff09\n             - kernel: \u6838\u51fd\u6570\u7c7b\u578b\uff0c&#039;linear&#039;|&#039;rbf&#039;|&#039;poly&#039;\uff0c\u9ed8\u8ba4&#039;rbf&#039;\n             - C: \u60e9\u7f5a\u7cfb\u6570\uff0c\u9ed8\u8ba41.0\n             - gamma: \u6838\u7cfb\u6570\uff0c\u9ed8\u8ba4&#039;scale&#039;\n             - degree: \u591a\u9879\u5f0f\u6838\u51fd\u6570\u9636\u6570\uff0c\u9ed8\u8ba43\n             - standardize: \u662f\u5426\u6807\u51c6\u5316\uff0c\u9ed8\u8ba4True\n             - figflag: \u662f\u5426\u7ed8\u56fe\uff0c\u9ed8\u8ba4True\n             - random_state: \u968f\u673a\u79cd\u5b50\uff0c\u9ed8\u8ba4123456\n\n         Returns:\n         --------\n         accuracy : float\n             \u6d4b\u8bd5\u96c6\u51c6\u786e\u7387\n         recall : float\n             \u53ec\u56de\u7387\uff08\u5404\u7c7b\u522b\u5e73\u5747\uff09\n         precision : float\n             \u7cbe\u786e\u7387\uff08\u5404\u7c7b\u522b\u5e73\u5747\uff09\n         f1 : float\n             F1\u5206\u6570\uff08\u5404\u7c7b\u522b\u5e73\u5747\uff09\n         models : List[SVC]\n             \u8bad\u7ec3\u597d\u7684SVM\u6a21\u578b\n         info : Dict[str, Any]\n             \u5305\u542b\u8be6\u7ec6\u7ed3\u679c\u7684\u5b57\u5178\n         &quot;&quot;&quot;<\/code><\/pre>\n\n\n\n<p>3. <strong>iris.csv<\/strong><\/p>\n\n\n\n\n<ul>\n<li>\u8bf4\u660e\uff1a\u7ecf\u5178\u7684\u9e22\u5c3e\u82b1\u6570\u636e\u96c6\uff0c\u5305\u542b150\u4e2a\u6837\u672c\uff0c4\u4e2a\u7279\u5f81\uff08\u843c\u7247\u957f\u5ea6\u3001\u843c\u7247\u5bbd\u5ea6\u3001\u82b1\u74e3\u957f\u5ea6\u3001\u82b1\u74e3\u5bbd\u5ea6\uff09\uff0c3\u4e2a\u7c7b\u522b\uff08Setosa\u3001Versicolor\u3001Virginica\uff09\u3002<\/li>\n<li>\u7528\u9014\uff1a\u4f5c\u4e3a\u6f14\u793a\u6570\u636e\uff0c\u5c55\u793aSVM\u591a\u5206\u7c7b\u7b97\u6cd5\u7684\u6548\u679c\u3002<\/li>\n<\/ul>\n\n\n\n<p>4. <strong>requirements.txt<\/strong><\/p>\n\n\n\n\n<ul>\n<li>\u8bf4\u660e\uff1aPython\u4f9d\u8d56\u5e93\u5217\u8868\u6587\u4ef6\uff0c\u5217\u51fa\u4e86\u8fd0\u884c\u7a0b\u5e8f\u6240\u9700\u7684\u6240\u6709\u7b2c\u4e09\u65b9\u5e93\u53ca\u5176\u7248\u672c\u8981\u6c42\u3002<\/li>\n<li>\u7528\u9014\uff1a\u901a\u8fc7 <code>pip install -r requirements.txt<\/code> \u547d\u4ee4\u4e00\u952e\u5b89\u88c5\u6240\u6709\u4f9d\u8d56\u3002<\/li>\n<\/ul>\n\n\n\n<p>5. <strong>\u4ee3\u7801\u8bf4\u660e.txt<\/strong><\/p>\n\n\n\n\n<ul>\n<li>\u8bf4\u660e\uff1a\u8be6\u7ec6\u7684\u4ee3\u7801\u4f7f\u7528\u8bf4\u660e\u6587\u6863\uff0c\u5305\u542b\u73af\u5883\u8981\u6c42\u3001\u5feb\u901f\u5f00\u59cb\u6307\u5357\u3001\u4e3b\u8981\u529f\u80fd\u4ecb\u7ecd\u548c\u53c2\u6570\u8bf4\u660e\u3002<\/li>\n<\/ul>\n\n\n\n\n<h4 class=\"wp-block-heading\">\u4e09\u3001\u5feb\u901f\u5f00\u59cb<\/h4>\n\n\n\n\n<h5 class=\"wp-block-heading\">1. \u73af\u5883\u914d\u7f6e<\/h5>\n\n\n\n\n<p><strong>\u5b89\u88c5Python\uff1a<\/strong><\/p>\n\n\n\n\n<ul>\n<li>\u524d\u5f80 <a href=\"https:\/\/www.python.org\/downloads\/\">Python\u5b98\u7f51<\/a> \u4e0b\u8f7d\u5e76\u5b89\u88c5 Python 3.8 \u6216\u66f4\u9ad8\u7248\u672c<\/li>\n<li>\u5b89\u88c5\u65f6\u52fe\u9009 &#8220;Add Python to PATH&#8221;<\/li>\n<\/ul>\n\n\n\n\n<p><strong>\u5b89\u88c5\u4f9d\u8d56\u5e93\uff1a<\/strong><\/p>\n\n\n\n\n<p>\u65b9\u6cd5\u4e00\uff08\u63a8\u8350\uff09\uff1a\u4f7f\u7528 <code>requirements.txt<\/code><\/p>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\"># \u6253\u5f00\u547d\u4ee4\u884c\uff0c\u5207\u6362\u5230\u7a0b\u5e8f\u76ee\u5f55\ncd path\/to\/SVM_Multiclass_Classification\n\n# \u5b89\u88c5\u6240\u6709\u4f9d\u8d56\npip install -r requirements.txt<\/code><\/pre>\n\n\n\n\n<p>\u65b9\u6cd5\u4e8c\uff1a\u624b\u52a8\u5b89\u88c5<\/p>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">pip install numpy pandas matplotlib scikit-learn<\/code><\/pre>\n\n\n\n\n<p><strong>\u9a8c\u8bc1\u5b89\u88c5\uff1a<\/strong><\/p>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">python -c &quot;import numpy, pandas, matplotlib, sklearn; print(&#039;\u4f9d\u8d56\u5e93\u5b89\u88c5\u6210\u529f\uff01&#039;)&quot;<\/code><\/pre>\n\n\n\n\n<h5 class=\"wp-block-heading\">2. \u8fd0\u884c\u6d4b\u8bd5\u811a\u672c<\/h5>\n\n\n\n\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n\n\n\n\n<p>1. \u6253\u5f00\u547d\u4ee4\u884c\uff08Windows\u6309Win+R\u8f93\u5165cmd\uff0cMac\u6309Command+\u7a7a\u683c\u8f93\u5165Terminal\uff09 2. \u5207\u6362\u5230\u7a0b\u5e8f\u76ee\u5f55\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">   cd path\/to\/SVM_Multiclass_Classification<\/code><\/pre>\n\n\n\n<p>3. \u8fd0\u884c\u6f14\u793a\u811a\u672c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">   python demoSVMMultiClass.py<\/code><\/pre>\n\n\n\n\n<p><strong>\u5224\u65ad\u7a0b\u5e8f\u662f\u5426\u6b63\u5e38\u8fd0\u884c\uff1a<\/strong><\/p>\n\n\n\n\n<ul>\n<li>\u547d\u4ee4\u884c\u8f93\u51fa\u5b8c\u6574\u7684\u6027\u80fd\u6307\u6807\u4fe1\u606f<\/li>\n<li>\u81ea\u52a8\u5f39\u51fa4\u4e2a\u56fe\u5f62\u7a97\u53e3\uff0c\u663e\u793a\u6df7\u6dc6\u77e9\u9635\u3001\u5206\u7c7b\u5bf9\u6bd4\u3001\u6027\u80fd\u6307\u6807\u548c\u6570\u636e\u5206\u5e03<\/li>\n<li>figure\u6587\u4ef6\u5939\u4e2d\u81ea\u52a8\u751f\u62104\u5f20PNG\u56fe\u7247<\/li>\n<li>\u6ca1\u6709\u62a5\u9519\u4fe1\u606f<\/li>\n<\/ul>\n\n\n\n\n<h5 class=\"wp-block-heading\">3. \u4fee\u6539\u4eff\u771f\u6570\u636e\/\u5bfc\u5165\u6570\u636e<\/h5>\n\n\n\n\n<h6 class=\"wp-block-heading\">\u60c5\u51b5\u4e00\uff1a\u4f7f\u7528\u81ea\u5df1\u7684\u4eff\u771f\u6570\u636e<\/h6>\n\n\n\n\n<p>1. \u590d\u5236\u4e00\u4e2a <code>demoSVMMultiClass.py<\/code> \u7684\u6587\u4ef6\u526f\u672c\uff08\u5982 <code>my_demo.py<\/code>\uff09 2. \u5728\u526f\u672c\u4e2d\u4fee\u6539\u6570\u636e\u5bfc\u5165\u90e8\u5206\uff08\u7b2c16-21\u884c\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">   # 1. \u6570\u636e\u5bfc\u5165\n   # \u4f7f\u7528\u81ea\u5df1\u7684\u6570\u636e\u66ff\u6362\n   X = your_feature_array  # \u66ff\u6362\u4e3a\u4f60\u7684\u7279\u5f81\u6570\u636e\uff0cshape=(n_samples, n_features)\n   y = your_label_array    # \u66ff\u6362\u4e3a\u4f60\u7684\u6807\u7b7e\u6570\u636e\uff0cshape=(n_samples,)<\/code><\/pre>\n\n\n\n\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n\n\n\n\n<ul>\n<li><code>X<\/code> \u5fc5\u987b\u662fnumpy\u6570\u7ec4\u6216\u53ef\u8f6c\u6362\u4e3anumpy\u6570\u7ec4\u7684\u683c\u5f0f\uff08\u5982list\u3001pandas DataFrame\uff09<\/li>\n<li><code>y<\/code> \u5fc5\u987b\u662f\u4e00\u7ef4\u6570\u7ec4\u6216\u5217\u8868<\/li>\n<li>\u6837\u672c\u6570\u5fc5\u987b\u4e0e\u6807\u7b7e\u6570\u91cf\u4e00\u81f4<\/li>\n<li>\u7279\u5f81\u6570\u5efa\u8bae\u57282-100\u4e4b\u95f4<\/li>\n<\/ul>\n\n\n\n\n<h6 class=\"wp-block-heading\">\u60c5\u51b5\u4e8c\uff1a\u4f7f\u7528\u771f\u5b9e\u91c7\u96c6\u7684\u6570\u636e<\/h6>\n\n\n\n\n<p><strong>\u4eceCSV\u6587\u4ef6\u5bfc\u5165\uff08\u63a8\u8350\uff09\uff1a<\/strong><\/p>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">import pandas as pd\nimport numpy as np\n\n# \u8bfb\u53d6CSV\u6587\u4ef6\ndata = pd.read_csv(&#039;your_data.csv&#039;)\n\n# \u65b9\u6cd51\uff1a\u524d\u51e0\u5217\u662f\u7279\u5f81\uff0c\u6700\u540e\u4e00\u5217\u662f\u6807\u7b7e\nX = data.iloc[:, :-1].values\ny = data.iloc[:, -1].values\n\n# \u65b9\u6cd52\uff1a\u6307\u5b9a\u5217\u540d\nX = data[[&#039;feature1&#039;, &#039;feature2&#039;, &#039;feature3&#039;]].values\ny = data[&#039;label&#039;].values<\/code><\/pre>\n\n\n\n\n<p><strong>\u4eceExcel\u6587\u4ef6\u5bfc\u5165\uff1a<\/strong><\/p>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">import pandas as pd\n\n# \u8bfb\u53d6Excel\u6587\u4ef6\ndata = pd.read_excel(&#039;your_data.xlsx&#039;, sheet_name=&#039;Sheet1&#039;)\nX = data.iloc[:, :-1].values\ny = data.iloc[:, -1].values<\/code><\/pre>\n\n\n\n\n<p><strong>\u4eceTXT\u6587\u4ef6\u5bfc\u5165\uff1a<\/strong><\/p>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">import numpy as np\n\n# \u5047\u8bbe\u6570\u636e\u4e3a\u7a7a\u683c\u6216Tab\u5206\u9694\ndata = np.loadtxt(&#039;your_data.txt&#039;)\nX = data[:, :-1]\ny = data[:, -1]\n\n# \u5982\u679c\u662f\u9017\u53f7\u5206\u9694\ndata = np.loadtxt(&#039;your_data.txt&#039;, delimiter=&#039;,&#039;)\nX = data[:, :-1]\ny = data[:, -1]<\/code><\/pre>\n\n\n\n\n<p><strong>\u4ecenumpy\u6570\u7ec4\u5bfc\u5165\uff1a<\/strong><\/p>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">import numpy as np\n\n# \u52a0\u8f7d.npy\u6587\u4ef6\ndata = np.load(&#039;your_data.npy&#039;)\nX = data[:, :-1]\ny = data[:, -1]<\/code><\/pre>\n\n\n\n\n<p><strong>\u5982\u679c\u6ca1\u6709\u6d4b\u8bd5\u6807\u7b7e\uff1a<\/strong> \u5982\u679c\u4f60\u7684\u6570\u636e\u6ca1\u6709\u5206\u5f00\u7684\u6d4b\u8bd5\u96c6\u6807\u7b7e\uff0c\u9700\u8981\u4fee\u6539\u4ee3\u7801\uff1a<\/p>\n\n\n\n\n<p>1. \u5220\u9664\u6216\u6ce8\u91ca\u6389\u6027\u80fd\u8bc4\u4f30\u76f8\u5173\u4ee3\u7801 2. \u53ea\u5173\u6ce8\u6a21\u578b\u8bad\u7ec3\u548c\u9884\u6d4b\u7ed3\u679c 3. \u53ef\u4ee5\u4f7f\u7528 <code>info[&#039;y_pred&#039;]<\/code> \u83b7\u53d6\u9884\u6d4b\u7ed3\u679c<\/p>\n\n\n\n\n<h5 class=\"wp-block-heading\">4. \u8c03\u6574\u7b97\u6cd5\u53c2\u6570\uff08\u53ef\u9009\uff09<\/h5>\n\n\n\n\n<p>\u5728 <code>demoSVMMultiClass.py<\/code> \u7684\u7b2c28-37\u884c\u53ef\u4ee5\u8c03\u6574\u7b97\u6cd5\u53c2\u6570\uff1a<\/p>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\"># \u8bbe\u7f6e\u53c2\u6570\noptions = {\n    &#039;test_size&#039;: 0.3,          # \u6d4b\u8bd5\u96c6\u6bd4\u4f8b\uff0830%\u6d4b\u8bd5\uff0c70%\u8bad\u7ec3\uff09\n                               # \u53ef\u8c03\u6574\u4e3a0.2-0.4\u4e4b\u95f4\n  \n    &#039;kernel&#039;: &#039;rbf&#039;,           # \u6838\u51fd\u6570\u7c7b\u578b\n                               # &#039;linear&#039;\uff1a\u7ebf\u6027\u6838\uff0c\u9002\u5408\u7ebf\u6027\u53ef\u5206\u95ee\u9898\n                               # &#039;rbf&#039;\uff1a\u5f84\u5411\u57fa\u6838\uff0c\u9002\u5408\u5927\u90e8\u5206\u975e\u7ebf\u6027\u95ee\u9898\uff08\u63a8\u8350\uff09\n                               # &#039;poly&#039;\uff1a\u591a\u9879\u5f0f\u6838\uff0c\u9002\u5408\u7279\u5b9a\u975e\u7ebf\u6027\u95ee\u9898\n  \n    &#039;C&#039;: 1.0,                  # \u60e9\u7f5a\u7cfb\u6570\uff08\u8d8a\u5927\u5bf9\u8bef\u5206\u7c7b\u7684\u60e9\u7f5a\u8d8a\u5927\uff09\n                               # \u5efa\u8bae\u8303\u56f4\uff1a0.1-100\n  \n    &#039;gamma&#039;: &#039;scale&#039;,          # \u6838\u7cfb\u6570\n                               # &#039;scale&#039;\uff1a\u81ea\u52a8\u8ba1\u7b97\uff08\u63a8\u8350\uff09\n                               # &#039;auto&#039;\uff1a\u81ea\u52a8\u8ba1\u7b97\n                               # \u4e5f\u53ef\u8bbe\u7f6e\u4e3a\u5177\u4f53\u6570\u503c\uff0c\u59820.1\u30011\u300110\u7b49\n  \n    &#039;degree&#039;: 3,               # \u591a\u9879\u5f0f\u6838\u9636\u6570\uff08\u4ec5poly\u6838\u9700\u8981\uff09\n                               # \u5efa\u8bae\u8303\u56f4\uff1a2-5\n  \n    &#039;standardize&#039;: True,       # \u662f\u5426\u6807\u51c6\u5316\u6570\u636e\uff08\u63a8\u8350True\uff09\n  \n    &#039;figflag&#039;: True,           # \u662f\u5426\u7ed8\u56fe\uff08True\/False\uff09\n  \n    &#039;random_state&#039;: 123456     # \u968f\u673a\u79cd\u5b50\uff08\u8bbe\u7f6e\u56fa\u5b9a\u503c\u4fdd\u8bc1\u7ed3\u679c\u53ef\u91cd\u590d\uff09\n}<\/code><\/pre>\n\n\n\n\n<p><strong>\u53c2\u6570\u8c03\u4f18\u5efa\u8bae\uff1a<\/strong><\/p>\n\n\n\n\n<p>1. \u9996\u5148\u4f7f\u7528\u9ed8\u8ba4\u53c2\u6570\u8fd0\u884c\uff0c\u89c2\u5bdf\u7ed3\u679c 2. \u5982\u679c\u51c6\u786e\u7387\u4e0d\u7406\u60f3\uff0c\u5c1d\u8bd5\u8c03\u6574\u6838\u51fd\u6570\u7c7b\u578b 3. \u8c03\u6574\u60e9\u7f5a\u7cfb\u6570 <code>C<\/code>\uff0c\u901a\u5e38\u5728[0.1, 1, 10, 100]\u4e2d\u9009\u62e9 4. \u5982\u679c\u4f7f\u7528RBF\u6838\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u4e0d\u540c\u7684 <code>gamma<\/code> \u503c 5. \u53ef\u4ee5\u4f7f\u7528 <code>sklearn.model_selection.GridSearchCV<\/code> \u8fdb\u884c\u81ea\u52a8\u53c2\u6570\u641c\u7d22<\/p>\n\n\n\n\n<p><strong>\u4f7f\u7528GridSearchCV\u81ea\u52a8\u8c03\u53c2\uff1a<\/strong><\/p>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">from sklearn.model_selection import GridSearchCV\nfrom sklearn.svm import SVC\n\n# \u5b9a\u4e49\u53c2\u6570\u7f51\u683c\nparam_grid = {\n    &#039;C&#039;: [0.1, 1, 10, 100],\n    &#039;gamma&#039;: [&#039;scale&#039;, &#039;auto&#039;, 0.1, 1],\n    &#039;kernel&#039;: [&#039;rbf&#039;, &#039;linear&#039;]\n}\n\n# \u521b\u5efa\u7f51\u683c\u641c\u7d22\u5bf9\u8c61\ngrid_search = GridSearchCV(SVC(), param_grid, cv=5, scoring=&#039;accuracy&#039;)\ngrid_search.fit(X_train, y_train)\n\n# \u8f93\u51fa\u6700\u4f73\u53c2\u6570\nprint(f&quot;\u6700\u4f73\u53c2\u6570: {grid_search.best_params_}&quot;)\nprint(f&quot;\u6700\u4f73\u51c6\u786e\u7387: {grid_search.best_score_:.4f}&quot;)<\/code><\/pre>\n\n\n\n\n<h5 class=\"wp-block-heading\">5. \u8fd0\u884c\u7a0b\u5e8f<\/h5>\n\n\n\n\n<p>\u5b8c\u6210\u4ee5\u4e0a\u4fee\u6539\u540e\uff0c\u5728\u547d\u4ee4\u884c\u8fd0\u884c\uff1a<\/p>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">python my_demo.py  # \u6216\u8005\u4f60\u4fee\u6539\u540e\u7684\u811a\u672c\u540d\u79f0<\/code><\/pre>\n\n\n\n\n<p>\u7a0b\u5e8f\u4f1a\u81ea\u52a8\u5b8c\u6210\u6570\u636e\u52a0\u8f7d\u3001\u6a21\u578b\u8bad\u7ec3\u3001\u9884\u6d4b\u548c\u53ef\u89c6\u5316\u7b49\u6240\u6709\u6b65\u9aa4\u3002<\/p>\n\n\n\n\n<h4 class=\"wp-block-heading\">\u56db\u3001\u5173\u4e8e\u5b8c\u6574\u7248\u4e0e\u516c\u5f00\u7248\u4ee3\u7801<\/h4>\n\n\n\n\n<p>> \u4ee3\u7801\u5206\u4e3a\u5b8c\u6574\u7248\u548c\u516c\u5f00\u7248\uff08\u8bd5\u7528\u7248\uff09\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7528\u6237\u7684\u9700\u6c42\u3002<\/p>\n\n\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>\u529f\u80fd<\/strong><\/td><td><strong>\u5b8c\u6574\u7248<\/strong><\/td><td><strong>\u516c\u5f00\u7248<\/strong><\/td><\/tr><tr><td>\u6570\u636e\u5bfc\u5165\u3001\u53c2\u6570\u8bbe\u7f6e<\/td><td>\u221a<\/td><td>\u221a<\/td><\/tr><tr><td>\u8f6f\u4ef6\u5168\u90e8\u6e90\u7801<\/td><td>\u221a<\/td><td>\u00d7<\/td><\/tr><tr><td>\u6838\u5fc3\u51fd\u6570\u6e90\u7801<\/td><td>\u5b8c\u6574\u53ef\u89c1<\/td><td>\u52a0\u5bc6\uff08.pyd\u6587\u4ef6\uff09<\/td><\/tr><tr><td>\u6570\u636e\u6837\u672c\u6570\u9650\u5236<\/td><td>\u65e0\u9650\u5236<\/td><td>\u6700\u5927100\u4e2a\u6837\u672c<\/td><\/tr><tr><td>\u6240\u6709\u6838\u51fd\u6570\u652f\u6301<\/td><td>\u221a<\/td><td>\u221a<\/td><\/tr><tr><td>\u5b8c\u6574\u53ef\u89c6\u5316\u529f\u80fd<\/td><td>\u221a<\/td><td>\u221a<\/td><\/tr><tr><td>\u753b\u56fe\u6c34\u5370<\/td><td>\u65e0\u6c34\u5370<\/td><td>\u6709\u6c34\u5370\u6807\u8bc6<\/td><\/tr><tr><td>\u89c6\u9891\u6559\u7a0b<\/td><td>\u221a<\/td><td>\u00d7<\/td><\/tr><tr><td>\u6280\u672f\u652f\u6301<\/td><td>\u63d0\u4f9b\u6280\u672f\u652f\u6301<\/td><td>\u65e0\u6280\u672f\u652f\u6301<\/td><\/tr><tr><td>\u4ee3\u7801\u6ce8\u91ca<\/td><td>\u8be6\u7ec6\u6ce8\u91ca<\/td><td>\u90e8\u5206\u6ce8\u91ca<\/td><\/tr><tr><td>\u8de8\u5e73\u53f0\u652f\u6301<\/td><td>Windows\/Linux\/Mac<\/td><td>\u4ec5\u9650\u7f16\u8bd1\u5e73\u53f0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h3 class=\"wp-block-heading has-background\" style=\"background-color:#e8f5e9;padding-top:15px;padding-bottom:15px;padding-left:20px;padding-right:20px\">\ud83d\udce5 \u4e94\u3001\u83b7\u53d6\u516c\u5f00\u7248\u7a0b\u5e8f<\/h3>\n\n\n\n\n\n\n\n<p>\u6ce8\uff1a\u516c\u5f00\u7248\u4ee3\u7801\u9700\u4f7f\u7528MATLAB2022a\u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/p>\n\n\n\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h3 class=\"wp-block-heading has-background\" style=\"background-color:#fff3e0;padding-top:15px;padding-bottom:15px;padding-left:20px;padding-right:20px\">\ud83d\udc8e \u516d\u3001\u83b7\u53d6\u5b8c\u6574\u7248\u7a0b\u5e8f<\/h3>\n\n\n\n<p class=\"is-style-iw-2em\">\u70b9\u51fb\u4e0b\u9762&#8221;<span style=\"color: #0000ff;\">\u7acb\u5373\u652f\u4ed8<\/span>&#8220;\u6309\u94ae\uff0c\u4ed8\u6b3e\u540e\u83b7\u53d6\u5b8c\u6574\u7248\u4ee3\u7801\u4e0b\u8f7d\u94fe\u63a5\u548c\u552e\u540e\u8054\u7cfb\u65b9\u5f0f~<strong>\u4ed8\u6b3e\u5b8c\u6210\u540e\u5237\u65b0\u4e00\u4e0b\u672c\u9875\u9762\u5373\u53ef\u770b\u5230\u4e0b\u8f7d\u94fe\u63a5\u3002<\/strong><\/p>\n\n\n\n<p class=\"is-style-iw-2em\"><span style=\"color: #ff9900;\">\uff08\u6ce8\u610f\uff1a\u652f\u4ed8\u8df3\u8f6c\u5931\u8d25\u7684\u8bdd\uff0c\u8bf7\u4f7f\u7528\u6d4f\u89c8\u5668\u6253\u5f00\u672c\u9875\u9762\uff09<\/span><\/p>\n\n\n<p><div class=\"erphp-wppay\">\n\t\t\t\t\u60a8\u9700\u8981\u5148\u652f\u4ed8 <b>39\u5143<\/b> \u624d\u80fd\u67e5\u770b\u6b64\u5904\u5185\u5bb9\uff01<a href=\"javascript:;\" class=\"erphp-wppay-loader\" data-post=\"3773\">\u7acb\u5373\u652f\u4ed8<\/a>\n\t\t\t<\/div><\/p>\n\n\n\n\n\n\n<h4 class=\"wp-block-heading\">\u4e03\u3001\u5b8c\u6574\u7248\u4ee3\u7801\u91cd\u8981\u66f4\u65b0<\/h4>\n\n\n\n\n<ul>\n<li><strong>2025-01-10<\/strong>: \u5b8c\u6210\u521d\u7248\u4ee3\u7801<\/li>\n<li>\u5b9e\u73b0\u57fa\u4e8eSVM\u7684\u591a\u5206\u7c7b\u7b97\u6cd5\uff08Python\u7248\uff09<\/li>\n<li>\u652f\u6301\u591a\u79cd\u6838\u51fd\u6570\uff08\u7ebf\u6027\u3001RBF\u3001\u591a\u9879\u5f0f\uff09<\/li>\n<li>\u5b9e\u73b0One-vs-Rest\u591a\u5206\u7c7b\u7b56\u7565<\/li>\n<li>\u63d0\u4f9b\u5b8c\u6574\u7684\u6027\u80fd\u8bc4\u4f30\u6307\u6807<\/li>\n<li>\u751f\u62105\u79cd\u53ef\u89c6\u5316\u7ed3\u679c<\/li>\n<li>\u652f\u6301\u4e2d\u6587\u5b57\u4f53\u663e\u793a<\/li>\n<li>\u81ea\u52a8\u4fdd\u5b58\u56fe\u7247\u5230figure\u6587\u4ef6\u5939<\/li>\n<li>\u5b8c\u6574\u7684\u7c7b\u578b\u6ce8\u89e3\u548c\u6587\u6863\u5b57\u7b26\u4e32<\/li>\n<\/ul>\n\n\n\n\n<h4 class=\"wp-block-heading\">\u516b\u3001\u5e38\u89c1\u95ee\u9898<\/h4>\n\n\n\n\n<h5 class=\"wp-block-heading\">Q1: \u8fd0\u884c\u7a0b\u5e8f\u65f6\u63d0\u793a &#8220;No module named &#8216;xxx'&#8221;\uff1f<\/h5>\n\n\n\n\n<p><strong>A:<\/strong> \u8fd9\u662f\u4f9d\u8d56\u5e93\u672a\u5b89\u88c5\u7684\u95ee\u9898\uff0c\u8bf7\uff1a<\/p>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">pip install -r requirements.txt<\/code><\/pre>\n\n\n\n\n<p>\u6216\u5355\u72ec\u5b89\u88c5\u7f3a\u5931\u7684\u5e93\uff1a<\/p>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">pip install \u5e93\u540d<\/code><\/pre>\n\n\n\n\n<h5 class=\"wp-block-heading\">Q2: \u56fe\u7247\u4e2d\u6587\u663e\u793a\u4e3a\u65b9\u6846\uff1f<\/h5>\n\n\n\n\n<p><strong>A:<\/strong> \u8fd9\u662f\u4e2d\u6587\u5b57\u4f53\u95ee\u9898\uff0c\u53ef\u4ee5\uff1a<\/p>\n\n\n\n\n<p>1. \u786e\u4fdd\u7cfb\u7edf\u5b89\u88c5\u4e86\u4e2d\u6587\u5b57\u4f53\uff08\u5982SimHei\u3001Microsoft YaHei\uff09 2. \u4fee\u6539\u4ee3\u7801\u4e2d\u7684\u5b57\u4f53\u8bbe\u7f6e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">   plt.rcParams[&#039;font.sans-serif&#039;] = [&#039;SimHei&#039;]  # Windows\n   # \u6216\n   plt.rcParams[&#039;font.sans-serif&#039;] = [&#039;Heiti TC&#039;]  # Mac<\/code><\/pre>\n\n\n\n\n<h5 class=\"wp-block-heading\">Q3: \u8fd0\u884c\u65f6\u63d0\u793a\u627e\u4e0d\u5230\u6587\u4ef6\uff1f<\/h5>\n\n\n\n\n<p><strong>A:<\/strong> \u8bf7\u786e\u4fdd\uff1a<\/p>\n\n\n\n\n<p>1. \u547d\u4ee4\u884c\u5f53\u524d\u76ee\u5f55\u5c31\u662f\u7a0b\u5e8f\u6240\u5728\u76ee\u5f55\uff08\u4f7f\u7528 <code>cd<\/code> \u547d\u4ee4\u5207\u6362\uff09 2. <code>iris.csv<\/code> \u6587\u4ef6\u4e0ePython\u811a\u672c\u5728\u540c\u4e00\u76ee\u5f55 3. \u4f7f\u7528 <code>os.getcwd()<\/code> \u67e5\u770b\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55<\/p>\n\n\n\n\n<h5 class=\"wp-block-heading\">Q4: \u51c6\u786e\u7387\u5f88\u4f4e\u600e\u4e48\u529e\uff1f<\/h5>\n\n\n\n\n<p><strong>A:<\/strong> \u53ef\u4ee5\u5c1d\u8bd5\uff1a<\/p>\n\n\n\n\n<p>1. \u66f4\u6362\u6838\u51fd\u6570\u7c7b\u578b\uff08&#8217;linear&#8217;\u3001&#8217;rbf&#8217;\u3001&#8217;poly&#8217;\uff09 2. \u8c03\u6574\u60e9\u7f5a\u7cfb\u6570 <code>C<\/code>\uff08\u5c1d\u8bd50.1\u30011\u300110\u3001100\uff09 3. \u786e\u4fdd\u6570\u636e\u5df2\u6807\u51c6\u5316\uff08<code>standardize=True<\/code>\uff09 4. \u589e\u52a0\u8bad\u7ec3\u96c6\u6bd4\u4f8b\uff08\u5982 <code>test_size=0.2<\/code>\uff09 5. \u4f7f\u7528GridSearchCV\u8fdb\u884c\u53c2\u6570\u641c\u7d22 6. \u68c0\u67e5\u6570\u636e\u8d28\u91cf\u548c\u6807\u7b7e\u662f\u5426\u6b63\u786e<\/p>\n\n\n\n\n<h5 class=\"wp-block-heading\">Q5: \u5982\u4f55\u4fdd\u5b58\u8bad\u7ec3\u597d\u7684\u6a21\u578b\uff1f<\/h5>\n\n\n\n\n<p><strong>A:<\/strong> \u4f7f\u7528 <code>joblib<\/code> \u6216 <code>pickle<\/code>\uff1a<\/p>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">import joblib\n\n# \u4fdd\u5b58\u6a21\u578b\njoblib.dump(models, &#039;svm_models.pkl&#039;)\njoblib.dump(info, &#039;model_info.pkl&#039;)\n\n# \u52a0\u8f7d\u6a21\u578b\nmodels = joblib.load(&#039;svm_models.pkl&#039;)\ninfo = joblib.load(&#039;model_info.pkl&#039;)<\/code><\/pre>\n\n\n\n\n<h5 class=\"wp-block-heading\">Q6: \u5982\u4f55\u7528\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u9884\u6d4b\u65b0\u6570\u636e\uff1f<\/h5>\n\n\n\n\n<p><strong>A:<\/strong><\/p>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\"># \u5047\u8bbe new_data \u662f\u65b0\u7684\u7279\u5f81\u6570\u636e\nif info[&#039;standardize&#039;]:\n    # \u4f7f\u7528\u8bad\u7ec3\u65f6\u7684\u6807\u51c6\u5316\u53c2\u6570\n    new_data_std = (new_data - info[&#039;X_mean&#039;]) \/ info[&#039;X_std&#039;]\nelse:\n    new_data_std = new_data\n\n# \u9884\u6d4b\npredictions = models[0].predict(new_data_std)\nprint(f&quot;\u9884\u6d4b\u7ed3\u679c: {predictions}&quot;)<\/code><\/pre>\n\n\n\n\n<h5 class=\"wp-block-heading\">Q7: \u7a0b\u5e8f\u8fd0\u884c\u65f6\u95f4\u8fc7\u957f\uff1f<\/h5>\n\n\n\n\n<p><strong>A:<\/strong><\/p>\n\n\n\n\n<p>1. \u51cf\u5c11\u6837\u672c\u6570\u91cf 2. \u51cf\u5c11\u7279\u5f81\u7ef4\u5ea6\uff08\u4f7f\u7528PCA\u964d\u7ef4\uff09 3. \u4f7f\u7528\u7ebf\u6027\u6838\u51fd\u6570\uff08<code>kernel=&#039;linear&#039;<\/code>\uff0c\u901f\u5ea6\u6700\u5feb\uff09 4. \u51cf\u5c0f <code>C<\/code> \u503c 5. \u4f7f\u7528\u66f4\u5feb\u7684SVM\u5b9e\u73b0\uff08\u5982LinearSVC\uff09<\/p>\n\n\n\n\n<h5 class=\"wp-block-heading\">Q8\uff1a \u5982\u4f55\u5f15\u7528\u672c\u4ee3\u7801\uff1f<\/h5>\n\n\n\n\n<p><strong>A:<\/strong> \u5982\u679c\u5728\u8bba\u6587\u6216\u62a5\u544a\u4e2d\u4f7f\u7528\u4e86\u672c\u4ee3\u7801\uff0c\u5efa\u8bae\u6ce8\u660e\uff1a<\/p>\n\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-text\">\u4ee3\u7801\u6765\u6e90\uff1aMr.\u770b\u6d77\uff0cSVM\u591a\u5206\u7c7b\u7b97\u6cd5Python\u5b9e\u73b0\n\u7f51\u7ad9\uff1awww.khsci.com\/docs\n\u4f7f\u7528\u7684\u673a\u5668\u5b66\u4e60\u5e93\uff1ascikit-learn<\/code><\/pre>\n\n\n\n\n<p>&#8212;<\/p>\n\n\n\n\n<p>*\u5982\u6709\u5176\u4ed6\u95ee\u9898\uff0c\u6b22\u8fce\u8bbf\u95ee www.khsci.com\/docs \u83b7\u53d6\u66f4\u591a\u5e2e\u52a9\u4fe1\u606f\u3002*<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u4ee3\u7801\u8fd0\u884c\u73af\u5883 Python: Python 3.8\u53ca\u4ee5\u4e0a\u7248\u672c \u4f9d\u8d56\u5e93: numpy &gt;= 1.21. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,7],"tags":[13,8,10,9,11],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.13 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SVM\u591a\u5206\u7c7b\u7b97\u6cd5 - \u57fa\u4e8e\u652f\u6301\u5411\u91cf\u673a\u7684\u591a\u7c7b\u522b\u5206\u7c7b\u3010Python\u3011 - \u5de5\u5177\u7bb1\u6587\u6863<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SVM\u591a\u5206\u7c7b\u7b97\u6cd5 - \u57fa\u4e8e\u652f\u6301\u5411\u91cf\u673a\u7684\u591a\u7c7b\u522b\u5206\u7c7b\u3010Python\u3011 - \u5de5\u5177\u7bb1\u6587\u6863\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u4ee3\u7801\u8fd0\u884c\u73af\u5883 Python: Python 3.8\u53ca\u4ee5\u4e0a\u7248\u672c \u4f9d\u8d56\u5e93: numpy &gt;= 1.21. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/\" \/>\n<meta property=\"og:site_name\" content=\"\u5de5\u5177\u7bb1\u6587\u6863\" \/>\n<meta property=\"article:published_time\" content=\"2025-10-11T14:06:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-10-11T16:15:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig1.png\" \/>\n<meta name=\"author\" content=\"kuohao\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"kuohao\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/\"},\"author\":{\"name\":\"kuohao\",\"@id\":\"https:\/\/www.khsci.com\/docs\/#\/schema\/person\/44b1dc94a273203f641349de36ebc0a5\"},\"headline\":\"SVM\u591a\u5206\u7c7b\u7b97\u6cd5 &#8211; \u57fa\u4e8e\u652f\u6301\u5411\u91cf\u673a\u7684\u591a\u7c7b\u522b\u5206\u7c7b\u3010Python\u3011\",\"datePublished\":\"2025-10-11T14:06:08+00:00\",\"dateModified\":\"2025-10-11T16:15:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/\"},\"wordCount\":122,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.khsci.com\/docs\/#\/schema\/person\/44b1dc94a273203f641349de36ebc0a5\"},\"keywords\":[\"Python\",\"SVM\",\"\u591a\u5206\u7c7b\",\"\u652f\u6301\u5411\u91cf\u673a\",\"\u673a\u5668\u5b66\u4e60\"],\"articleSection\":{\"1\":\"\u6a21\u5f0f\u8bc6\u522b\/\u5206\u7c7b\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/\",\"url\":\"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/\",\"name\":\"SVM\u591a\u5206\u7c7b\u7b97\u6cd5 - \u57fa\u4e8e\u652f\u6301\u5411\u91cf\u673a\u7684\u591a\u7c7b\u522b\u5206\u7c7b\u3010Python\u3011 - \u5de5\u5177\u7bb1\u6587\u6863\",\"isPartOf\":{\"@id\":\"https:\/\/www.khsci.com\/docs\/#website\"},\"datePublished\":\"2025-10-11T14:06:08+00:00\",\"dateModified\":\"2025-10-11T16:15:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.khsci.com\/docs\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SVM\u591a\u5206\u7c7b\u7b97\u6cd5 &#8211; \u57fa\u4e8e\u652f\u6301\u5411\u91cf\u673a\u7684\u591a\u7c7b\u522b\u5206\u7c7b\u3010Python\u3011\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.khsci.com\/docs\/#website\",\"url\":\"https:\/\/www.khsci.com\/docs\/\",\"name\":\"\u5de5\u5177\u7bb1\u6587\u6863\",\"description\":\"khToolbox\",\"publisher\":{\"@id\":\"https:\/\/www.khsci.com\/docs\/#\/schema\/person\/44b1dc94a273203f641349de36ebc0a5\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.khsci.com\/docs\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.khsci.com\/docs\/#\/schema\/person\/44b1dc94a273203f641349de36ebc0a5\",\"name\":\"kuohao\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.khsci.com\/docs\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/khsci.com\/docs\/wp-content\/uploads\/2022\/11\/logo-1.png\",\"contentUrl\":\"https:\/\/khsci.com\/docs\/wp-content\/uploads\/2022\/11\/logo-1.png\",\"width\":255,\"height\":97,\"caption\":\"kuohao\"},\"logo\":{\"@id\":\"https:\/\/www.khsci.com\/docs\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/www.khsci.com\/docs\/index.php\/author\/kuohao\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SVM\u591a\u5206\u7c7b\u7b97\u6cd5 - \u57fa\u4e8e\u652f\u6301\u5411\u91cf\u673a\u7684\u591a\u7c7b\u522b\u5206\u7c7b\u3010Python\u3011 - \u5de5\u5177\u7bb1\u6587\u6863","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/","og_locale":"zh_CN","og_type":"article","og_title":"SVM\u591a\u5206\u7c7b\u7b97\u6cd5 - \u57fa\u4e8e\u652f\u6301\u5411\u91cf\u673a\u7684\u591a\u7c7b\u522b\u5206\u7c7b\u3010Python\u3011 - \u5de5\u5177\u7bb1\u6587\u6863","og_description":"\u4e00\u3001\u4ee3\u7801\u8fd0\u884c\u73af\u5883 Python: Python 3.8\u53ca\u4ee5\u4e0a\u7248\u672c \u4f9d\u8d56\u5e93: numpy &gt;= 1.21. [&hellip;]","og_url":"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/","og_site_name":"\u5de5\u5177\u7bb1\u6587\u6863","article_published_time":"2025-10-11T14:06:08+00:00","article_modified_time":"2025-10-11T16:15:02+00:00","og_image":[{"url":"https:\/\/www.khsci.com\/docs\/wp-content\/uploads\/2025\/10\/python_fig1.png"}],"author":"kuohao","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"kuohao","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/#article","isPartOf":{"@id":"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/"},"author":{"name":"kuohao","@id":"https:\/\/www.khsci.com\/docs\/#\/schema\/person\/44b1dc94a273203f641349de36ebc0a5"},"headline":"SVM\u591a\u5206\u7c7b\u7b97\u6cd5 &#8211; \u57fa\u4e8e\u652f\u6301\u5411\u91cf\u673a\u7684\u591a\u7c7b\u522b\u5206\u7c7b\u3010Python\u3011","datePublished":"2025-10-11T14:06:08+00:00","dateModified":"2025-10-11T16:15:02+00:00","mainEntityOfPage":{"@id":"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/"},"wordCount":122,"commentCount":0,"publisher":{"@id":"https:\/\/www.khsci.com\/docs\/#\/schema\/person\/44b1dc94a273203f641349de36ebc0a5"},"keywords":["Python","SVM","\u591a\u5206\u7c7b","\u652f\u6301\u5411\u91cf\u673a","\u673a\u5668\u5b66\u4e60"],"articleSection":{"1":"\u6a21\u5f0f\u8bc6\u522b\/\u5206\u7c7b"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/","url":"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/","name":"SVM\u591a\u5206\u7c7b\u7b97\u6cd5 - \u57fa\u4e8e\u652f\u6301\u5411\u91cf\u673a\u7684\u591a\u7c7b\u522b\u5206\u7c7b\u3010Python\u3011 - \u5de5\u5177\u7bb1\u6587\u6863","isPartOf":{"@id":"https:\/\/www.khsci.com\/docs\/#website"},"datePublished":"2025-10-11T14:06:08+00:00","dateModified":"2025-10-11T16:15:02+00:00","breadcrumb":{"@id":"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.khsci.com\/docs\/index.php\/2025\/10\/11\/svmclasspy\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.khsci.com\/docs\/"},{"@type":"ListItem","position":2,"name":"SVM\u591a\u5206\u7c7b\u7b97\u6cd5 &#8211; \u57fa\u4e8e\u652f\u6301\u5411\u91cf\u673a\u7684\u591a\u7c7b\u522b\u5206\u7c7b\u3010Python\u3011"}]},{"@type":"WebSite","@id":"https:\/\/www.khsci.com\/docs\/#website","url":"https:\/\/www.khsci.com\/docs\/","name":"\u5de5\u5177\u7bb1\u6587\u6863","description":"khToolbox","publisher":{"@id":"https:\/\/www.khsci.com\/docs\/#\/schema\/person\/44b1dc94a273203f641349de36ebc0a5"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.khsci.com\/docs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/www.khsci.com\/docs\/#\/schema\/person\/44b1dc94a273203f641349de36ebc0a5","name":"kuohao","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.khsci.com\/docs\/#\/schema\/person\/image\/","url":"https:\/\/khsci.com\/docs\/wp-content\/uploads\/2022\/11\/logo-1.png","contentUrl":"https:\/\/khsci.com\/docs\/wp-content\/uploads\/2022\/11\/logo-1.png","width":255,"height":97,"caption":"kuohao"},"logo":{"@id":"https:\/\/www.khsci.com\/docs\/#\/schema\/person\/image\/"},"url":"https:\/\/www.khsci.com\/docs\/index.php\/author\/kuohao\/"}]}},"_links":{"self":[{"href":"https:\/\/www.khsci.com\/docs\/index.php\/wp-json\/wp\/v2\/posts\/3773"}],"collection":[{"href":"https:\/\/www.khsci.com\/docs\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.khsci.com\/docs\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.khsci.com\/docs\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.khsci.com\/docs\/index.php\/wp-json\/wp\/v2\/comments?post=3773"}],"version-history":[{"count":13,"href":"https:\/\/www.khsci.com\/docs\/index.php\/wp-json\/wp\/v2\/posts\/3773\/revisions"}],"predecessor-version":[{"id":3816,"href":"https:\/\/www.khsci.com\/docs\/index.php\/wp-json\/wp\/v2\/posts\/3773\/revisions\/3816"}],"wp:attachment":[{"href":"https:\/\/www.khsci.com\/docs\/index.php\/wp-json\/wp\/v2\/media?parent=3773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.khsci.com\/docs\/index.php\/wp-json\/wp\/v2\/categories?post=3773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.khsci.com\/docs\/index.php\/wp-json\/wp\/v2\/tags?post=3773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}