{"id":936755,"date":"2024-12-26T19:36:02","date_gmt":"2024-12-26T11:36:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/936755.html"},"modified":"2024-12-26T19:36:05","modified_gmt":"2024-12-26T11:36:05","slug":"python%e5%a6%82%e4%bd%95%e9%99%8d%e4%bd%8epca","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/936755.html","title":{"rendered":"python\u5982\u4f55\u964d\u4f4epca"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25072908\/897dcc71-dc3f-4d40-8dd5-a96050d9f117.webp\" alt=\"python\u5982\u4f55\u964d\u4f4epca\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u964d\u4f4ePCA\u7684\u7ef4\u5ea6\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5e93\u5982Scikit-learn\u3001\u9009\u62e9\u5408\u9002\u7684\u4e3b\u6210\u5206\u6570\u3001\u6570\u636e\u6807\u51c6\u5316\u6765\u5b9e\u73b0\u3002<\/strong>PCA\uff0c\u4e3b\u6210\u5206\u5206\u6790\uff0c\u662f\u4e00\u79cd\u7edf\u8ba1\u6280\u672f\uff0c\u7528\u4e8e\u964d\u7ef4\u548c\u7279\u5f81\u63d0\u53d6\u3002\u901a\u8fc7PCA\uff0c\u53ef\u4ee5\u5c06\u9ad8\u7ef4\u6570\u636e\u8f6c\u5316\u4e3a\u4f4e\u7ef4\u6570\u636e\uff0c\u540c\u65f6\u4fdd\u7559\u5c3d\u53ef\u80fd\u591a\u7684\u539f\u59cb\u6570\u636e\u7279\u5f81\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5730\u964d\u4f4ePCA\u7684\u7ef4\u5ea6\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001SCIKIT-LEARN\u5e93\u4f7f\u7528<\/p>\n<\/p>\n<p><p>Scikit-learn\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u5e93\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u5b9e\u73b0PCA\u7684\u6a21\u5757\u3002\u4f7f\u7528Scikit-learn\u8fdb\u884cPCA\uff0c\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u964d\u7ef4\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528Scikit-learn\u6765\u964d\u4f4e\u6570\u636e\u7ef4\u5ea6\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u5e93\u548c\u6570\u636e\u51c6\u5907<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u548c\u51c6\u5907\u6570\u636e\u3002\u901a\u5e38\uff0c\u6570\u636e\u9700\u8981\u4ee5\u6570\u7ec4\u6216DataFrame\u7684\u5f62\u5f0f\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from sklearn.decomposition import PCA<\/p>\n<p>from sklearn.preprocessing import StandardScaler<\/p>\n<h2><strong>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u96c6<\/strong><\/h2>\n<p>data = np.array([[2.5, 2.4], [0.5, 0.7], [2.2, 2.9], [1.9, 2.2], [3.1, 3.0]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6570\u636e\u6807\u51c6\u5316<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u8fdb\u884cPCA\u4e4b\u524d\uff0c\u901a\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u6807\u51c6\u5316\u5904\u7406\u3002\u8fd9\u662f\u56e0\u4e3aPCA\u5bf9\u6570\u636e\u7684\u91cf\u7eb2\u654f\u611f\uff0c\u6807\u51c6\u5316\u53ef\u4ee5\u786e\u4fdd\u6bcf\u4e2a\u7279\u5f81\u5bf9PCA\u7684\u8d21\u732e\u76f8\u540c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">scaler = StandardScaler()<\/p>\n<p>data_standardized = scaler.fit_transform(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6267\u884cPCA<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528Scikit-learn\u7684PCA\u7c7b\uff0c\u6307\u5b9a\u8981\u964d\u4f4e\u5230\u7684\u7ef4\u5ea6\u6570\u3002\u7136\u540e\u5c06\u6807\u51c6\u5316\u7684\u6570\u636e\u4f20\u5165PCA\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pca = PCA(n_components=1)  # \u964d\u4f4e\u52301\u7ef4<\/p>\n<p>data_reduced = pca.fit_transform(data_standardized)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u89e3\u91ca\u7ed3\u679c<\/strong><\/li>\n<\/ol>\n<p><p>PCA\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u89e3\u91ca\u964d\u7ef4\u7ed3\u679c\uff0c\u67e5\u770b\u5404\u4e3b\u6210\u5206\u7684\u65b9\u5dee\u6bd4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Expl<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ned variance ratio:&quot;, pca.explained_variance_ratio_)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>Scikit-learn\u5e93\u63d0\u4f9b\u4e86\u7b80\u6d01\u7684\u63a5\u53e3\u6765\u5904\u7406PCA\uff0c\u6613\u4e8e\u4f7f\u7528\u4e14\u9ad8\u6548\u3002\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u5c24\u4e3a\u63a8\u8350\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u9009\u62e9\u5408\u9002\u7684\u4e3b\u6210\u5206\u6570<\/p>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u4e3b\u6210\u5206\u6570\u662fPCA\u6210\u529f\u7684\u5173\u952e\u3002\u9009\u62e9\u8fc7\u5c11\u7684\u4e3b\u6210\u5206\u53ef\u80fd\u4f1a\u4e22\u5931\u91cd\u8981\u4fe1\u606f\uff0c\u800c\u9009\u62e9\u8fc7\u591a\u5219\u65e0\u6cd5\u8fbe\u5230\u964d\u7ef4\u7684\u6548\u679c\u3002\u4ee5\u4e0b\u662f\u9009\u62e9\u4e3b\u6210\u5206\u6570\u7684\u51e0\u4e2a\u7b56\u7565\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u7d2f\u8ba1\u65b9\u5dee\u8d21\u732e\u7387<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u5e38\uff0c\u9009\u62e9\u7d2f\u8ba1\u65b9\u5dee\u8d21\u732e\u7387\u8fbe\u523085%\u621690%\u7684\u4e3b\u6210\u5206\u6570\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u67e5\u770bPCA\u7684\u65b9\u5dee\u6bd4\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pca = PCA().fit(data_standardized)<\/p>\n<p>cumulative_variance = np.cumsum(pca.explained_variance_ratio_)<\/p>\n<p>num_components = np.argmax(cumulative_variance &gt;= 0.85) + 1<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u788e\u77f3\u56fe<\/strong><\/li>\n<\/ol>\n<p><p>\u788e\u77f3\u56fe\u901a\u8fc7\u7ed8\u5236\u5404\u4e3b\u6210\u5206\u65b9\u5dee\u6bd4\uff0c\u5e2e\u52a9\u8bc6\u522b\u201c\u8098\u90e8\u201d\u70b9\uff0c\u4ece\u800c\u786e\u5b9a\u5408\u9002\u7684\u4e3b\u6210\u5206\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>plt.plot(range(1, len(pca.explained_variance_ratio_) + 1), cumulative_variance, marker=&#39;o&#39;)<\/p>\n<p>plt.xlabel(&#39;Number of Components&#39;)<\/p>\n<p>plt.ylabel(&#39;Cumulative Explained Variance&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u4ea4\u53c9\u9a8c\u8bc1<\/strong><\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ea4\u53c9\u9a8c\u8bc1\u6765\u9009\u62e9\u6700\u4f18\u7684\u4e3b\u6210\u5206\u6570\uff0c\u786e\u4fddPCA\u5bf9\u6a21\u578b\u6027\u80fd\u7684\u63d0\u5347\u3002<\/p>\n<\/p>\n<p><p><strong>\u9009\u62e9\u5408\u9002\u7684\u4e3b\u6210\u5206\u6570\u9700\u8981\u7ed3\u5408\u6570\u636e\u7279\u6027\u548c\u4e1a\u52a1\u9700\u6c42\u8fdb\u884c\u5224\u65ad\uff0c\u901a\u5e38\u9700\u8981\u591a\u79cd\u65b9\u6cd5\u7ed3\u5408\u4f7f\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e09\u3001\u6570\u636e\u6807\u51c6\u5316\u7684\u91cd\u8981\u6027<\/p>\n<\/p>\n<p><p>\u6570\u636e\u6807\u51c6\u5316\u662fPCA\u524d\u7684\u4e00\u9879\u5173\u952e\u6b65\u9aa4\uff0c\u56e0\u4e3aPCA\u5bf9\u6570\u636e\u7684\u5c3a\u5ea6\u654f\u611f\u3002\u6807\u51c6\u5316\u53ef\u4ee5\u786e\u4fdd\u6240\u6709\u7279\u5f81\u4ee5\u540c\u7b49\u7684\u6743\u91cd\u5bf9\u5206\u6790\u7ed3\u679c\u4ea7\u751f\u5f71\u54cd\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4e3a\u4ec0\u4e48\u9700\u8981\u6807\u51c6\u5316<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u6570\u636e\u96c6\u4e2d\uff0c\u4e0d\u540c\u7279\u5f81\u53ef\u80fd\u5177\u6709\u4e0d\u540c\u7684\u91cf\u7eb2\uff08\u4f8b\u5982\uff0c\u8eab\u9ad8\u4ee5\u5398\u7c73\u8ba1\uff0c\u4f53\u91cd\u4ee5\u5343\u514b\u8ba1\uff09\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u65b9\u5dee\u8f83\u5927\u7684\u7279\u5f81\u5bf9PCA\u7ed3\u679c\u7684\u5f71\u54cd\u66f4\u5927\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6807\u51c6\u5316\u65b9\u6cd5<\/strong><\/li>\n<\/ol>\n<p><p>\u5e38\u7528\u7684\u6807\u51c6\u5316\u65b9\u6cd5\u5305\u62ecz-score\u6807\u51c6\u5316\u548c\u6700\u5c0f-\u6700\u5927\u6807\u51c6\u5316\u3002\u5728PCA\u4e2d\uff0cz-score\u6807\u51c6\u5316\u662f\u9996\u9009\uff0c\u56e0\u4e3a\u5b83\u5c06\u6570\u636e\u8f6c\u5316\u4e3a\u5747\u503c\u4e3a0\u3001\u65b9\u5dee\u4e3a1\u7684\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">scaler = StandardScaler()<\/p>\n<p>data_standardized = scaler.fit_transform(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6807\u51c6\u5316\u662fPCA\u5206\u6790\u7684\u57fa\u7840\u6b65\u9aa4\uff0c\u786e\u4fdd\u6bcf\u4e2a\u7279\u5f81\u7684\u8d21\u732e\u5747\u8861\uff0c\u907f\u514d\u4fe1\u606f\u504f\u5dee\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u56db\u3001PCA\u7684\u4f18\u7f3a\u70b9<\/p>\n<\/p>\n<p><p>PCA\u867d\u7136\u662f\u4e00\u79cd\u5e38\u7528\u7684\u964d\u7ef4\u6280\u672f\uff0c\u4f46\u5728\u5e94\u7528\u8fc7\u7a0b\u4e2d\u4e5f\u6709\u4e00\u4e9b\u9700\u8981\u6ce8\u610f\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f18\u70b9<\/strong><\/li>\n<\/ol>\n<ul>\n<li><strong>\u964d\u7ef4\u6548\u679c\u663e\u8457<\/strong>\uff1a\u901a\u8fc7PCA\uff0c\u53ef\u4ee5\u5728\u4fdd\u7559\u6570\u636e\u4e3b\u8981\u7279\u5f81\u7684\u540c\u65f6\u5927\u5e45\u964d\u4f4e\u6570\u636e\u7ef4\u5ea6\u3002<\/li>\n<li><strong>\u566a\u58f0\u8fc7\u6ee4<\/strong>\uff1aPCA\u80fd\u591f\u6709\u6548\u8fc7\u6ee4\u6389\u6570\u636e\u4e2d\u7684\u566a\u58f0\uff0c\u63d0\u9ad8\u6a21\u578b\u7684\u9c81\u68d2\u6027\u3002<\/li>\n<li><strong>\u7279\u5f81\u53ef\u89c6\u5316<\/strong>\uff1a\u964d\u7ef4\u540e\u7684\u6570\u636e\u66f4\u6613\u4e8e\u53ef\u89c6\u5316\uff0c\u4fbf\u4e8e\u7406\u89e3\u6570\u636e\u7ed3\u6784\u3002<\/li>\n<\/ul>\n<ol start=\"2\">\n<li><strong>\u7f3a\u70b9<\/strong><\/li>\n<\/ol>\n<ul>\n<li><strong>\u7ebf\u6027\u5047\u8bbe<\/strong>\uff1aPCA\u5047\u8bbe\u6570\u636e\u662f\u7ebf\u6027\u53ef\u5206\u7684\uff0c\u5bf9\u975e\u7ebf\u6027\u6570\u636e\u7684\u6548\u679c\u6709\u9650\u3002<\/li>\n<li><strong>\u4fe1\u606f\u4e22\u5931<\/strong>\uff1a\u964d\u7ef4\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4e22\u5931\u90e8\u5206\u4fe1\u606f\uff0c\u5c24\u5176\u662f\u9009\u62e9\u8f83\u5c11\u4e3b\u6210\u5206\u65f6\u3002<\/li>\n<li><strong>\u89e3\u91ca\u6027\u5dee<\/strong>\uff1aPCA\u7684\u4e3b\u6210\u5206\u662f\u7ebf\u6027\u7ec4\u5408\uff0c\u53ef\u80fd\u96be\u4ee5\u76f4\u89c2\u89e3\u91ca\u3002<\/li>\n<\/ul>\n<p><p><strong>\u5728\u4f7f\u7528PCA\u65f6\uff0c\u9700\u8981\u6743\u8861\u5176\u4f18\u7f3a\u70b9\uff0c\u6839\u636e\u5177\u4f53\u6570\u636e\u548c\u5e94\u7528\u573a\u666f\u8fdb\u884c\u9009\u62e9\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e94\u3001PCA\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6848\u4f8b<\/p>\n<\/p>\n<p><p>PCA\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u573a\u666f\uff0c\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5178\u578b\u6848\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u56fe\u50cf\u5904\u7406<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u4e2d\uff0cPCA\u5e38\u7528\u4e8e\u964d\u7ef4\u548c\u538b\u7f29\u3002\u4f8b\u5982\uff0c\u5728\u4eba\u8138\u8bc6\u522b\u4e2d\uff0c\u901a\u8fc7PCA\u53ef\u4ee5\u5c06\u9ad8\u7ef4\u7684\u56fe\u50cf\u6570\u636e\u964d\u5230\u8f83\u4f4e\u7ef4\u5ea6\uff0c\u63d0\u9ad8\u8bc6\u522b\u6548\u7387\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u57fa\u56e0\u6570\u636e\u5206\u6790<\/strong><\/li>\n<\/ol>\n<p><p>\u57fa\u56e0\u6570\u636e\u901a\u5e38\u5177\u6709\u9ad8\u7ef4\u7279\u6027\uff0cPCA\u53ef\u4ee5\u5e2e\u52a9\u63d0\u53d6\u4e3b\u8981\u7279\u5f81\uff0c\u7b80\u5316\u5206\u6790\u548c\u53ef\u89c6\u5316\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u91d1\u878d\u6570\u636e\u5206\u6790<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u91d1\u878d\u5206\u6790\u4e2d\uff0cPCA\u7528\u4e8e\u51cf\u5c11\u7279\u5f81\u6570\u91cf\uff0c\u5206\u6790\u80a1\u7968\u6216\u5176\u4ed6\u91d1\u878d\u4ea7\u54c1\u7684\u4e3b\u8981\u8d8b\u52bf\u3002<\/p>\n<\/p>\n<p><p><strong>PCA\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u80fd\u591f\u6709\u6548\u964d\u4f4e\u6570\u636e\u590d\u6742\u5ea6\uff0c\u63d0\u9ad8\u5206\u6790\u6548\u7387\uff0c\u662f\u6570\u636e\u79d1\u5b66\u4e2d\u7684\u91cd\u8981\u5de5\u5177\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u5927\u5bb6\u5bf9\u5982\u4f55\u5728Python\u4e2d\u964d\u4f4ePCA\u7684\u7ef4\u5ea6\u6709\u4e86\u66f4\u6df1\u5165\u7684\u7406\u89e3\u3002PCA\u4f5c\u4e3a\u4e00\u79cd\u7ecf\u5178\u7684\u964d\u7ef4\u6280\u672f\uff0c\u5177\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u4ef7\u503c\u3002\u5728\u5b9e\u8df5\u4e2d\uff0c\u5145\u5206\u7406\u89e3PCA\u7684\u539f\u7406\u548c\u5e94\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5904\u7406\u9ad8\u7ef4\u6570\u636e\uff0c\u63d0\u9ad8\u6570\u636e\u5206\u6790\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>PCA\u7684\u57fa\u672c\u539f\u7406\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u4e3b\u6210\u5206\u5206\u6790\uff08PCA\uff09\u662f\u4e00\u79cd\u964d\u7ef4\u6280\u672f\uff0c\u4e3b\u8981\u7528\u4e8e\u7b80\u5316\u6570\u636e\u96c6\uff0c\u540c\u65f6\u5c3d\u53ef\u80fd\u4fdd\u7559\u6570\u636e\u7684\u53d8\u5f02\u6027\u3002\u901a\u8fc7\u5c06\u539f\u59cb\u53d8\u91cf\u8f6c\u6362\u4e3a\u65b0\u7684\u4e0d\u76f8\u5173\u53d8\u91cf\uff08\u4e3b\u6210\u5206\uff09\uff0cPCA\u5e2e\u52a9\u6211\u4eec\u53d1\u73b0\u6570\u636e\u4e2d\u7684\u6a21\u5f0f\u548c\u7ed3\u6784\uff0c\u901a\u5e38\u7528\u4e8e\u6570\u636e\u9884\u5904\u7406\u3001\u53ef\u89c6\u5316\u548c\u7279\u5f81\u63d0\u53d6\u7b49\u4efb\u52a1\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5b9e\u73b0PCA\u964d\u7ef4\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u5b9e\u73b0PCA\u964d\u7ef4\uff0c\u901a\u5e38\u4f7f\u7528<code>sklearn<\/code>\u5e93\u3002\u9996\u5148\u9700\u8981\u5bfc\u5165<code>PCA<\/code>\u7c7b\uff0c\u7136\u540e\u521b\u5efaPCA\u5bf9\u8c61\u5e76\u6307\u5b9a\u8981\u4fdd\u7559\u7684\u4e3b\u6210\u5206\u6570\u91cf\uff0c\u63a5\u7740\u8c03\u7528<code>fit_transform<\/code>\u65b9\u6cd5\u5bf9\u6570\u636e\u8fdb\u884c\u964d\u7ef4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">from sklearn.decomposition import PCA\npca = PCA(n_components=2)  # \u4fdd\u7559\u4e24\u4e2a\u4e3b\u6210\u5206\nreduced_data = pca.fit_transform(original_data)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u6570\u636e\u7684\u7ef4\u5ea6\uff0c\u540c\u65f6\u4fdd\u7559\u5927\u90e8\u5206\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u9009\u62e9PCA\u4e2d\u7684\u4e3b\u6210\u5206\u6570\u91cf\uff1f<\/strong><br \/>\u9009\u62e9\u4e3b\u6210\u5206\u7684\u6570\u91cf\u901a\u5e38\u57fa\u4e8e\u89e3\u91ca\u7684\u65b9\u5dee\u6bd4\u4f8b\u3002\u53ef\u4ee5\u7ed8\u5236\u65b9\u5dee\u89e3\u91ca\u56fe\uff08Scree Plot\uff09\uff0c\u89c2\u5bdf\u6bcf\u4e2a\u4e3b\u6210\u5206\u6240\u89e3\u91ca\u7684\u65b9\u5dee\uff0c\u9009\u62e9\u62d0\u70b9\u5904\u7684\u4e3b\u6210\u5206\u6570\u91cf\u3002\u4e00\u822c\u800c\u8a00\uff0c\u9009\u62e9\u80fd\u591f\u89e3\u91ca70%\u81f390%\u53d8\u5f02\u6027\u7684\u4e3b\u6210\u5206\u6570\u91cf\u662f\u4e00\u4e2a\u5408\u7406\u7684\u8d77\u70b9\u3002<\/p>\n<p><strong>PCA\u964d\u7ef4\u540e\u6570\u636e\u7684\u53ef\u89c6\u5316\u5982\u4f55\u8fdb\u884c\uff1f<\/strong><br \/>\u5728\u5b8c\u6210PCA\u964d\u7ef4\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u6216Seaborn\u7b49\u5e93\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3002\u901a\u8fc7\u7ed8\u5236\u6563\u70b9\u56fe\uff0c\u53ef\u4ee5\u76f4\u89c2\u5730\u89c2\u5bdf\u964d\u7ef4\u540e\u7684\u6570\u636e\u5206\u5e03\u53ca\u5176\u7ed3\u6784\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import matplotlib.pyplot as plt\n\nplt.scatter(reduced_data[:, 0], reduced_data[:, 1])\nplt.xlabel(&#39;\u4e3b\u6210\u52061&#39;)\nplt.ylabel(&#39;\u4e3b\u6210\u52062&#39;)\nplt.title(&#39;PCA\u964d\u7ef4\u7ed3\u679c&#39;)\nplt.show()\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u53ef\u89c6\u5316\u65b9\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u7684\u5206\u5e03\u548c\u6f5c\u5728\u7684\u805a\u7c7b\u7ed3\u6784\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u964d\u4f4ePCA\u7684\u7ef4\u5ea6\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5e93\u5982Scikit-learn\u3001\u9009\u62e9\u5408\u9002\u7684\u4e3b\u6210\u5206\u6570\u3001\u6570\u636e\u6807\u51c6\u5316\u6765\u5b9e\u73b0 [&hellip;]","protected":false},"author":3,"featured_media":936759,"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\/936755"}],"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=936755"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/936755\/revisions"}],"predecessor-version":[{"id":936760,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/936755\/revisions\/936760"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/936759"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=936755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=936755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=936755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}