{"id":644,"date":"2021-12-23T14:56:03","date_gmt":"2021-12-23T06:56:03","guid":{"rendered":"http:\/\/139.9.1.231\/?p=644"},"modified":"2021-12-23T14:56:05","modified_gmt":"2021-12-23T06:56:05","slug":"pca","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2021\/12\/23\/pca\/","title":{"rendered":"PCA"},"content":{"rendered":"\n<h2>\u964d\u7ef4\u2014\u2014PCA<\/h2>\n\n\n\n<p>\u964d\u7ef4\u5c31\u662f\u4e00\u79cd\u5bf9\u9ad8\u7ef4\u5ea6\u7279\u5f81\u6570\u636e\u9884\u5904\u7406\u65b9\u6cd5\u3002\u964d\u7ef4\u662f\u5c06\u9ad8\u7ef4\u5ea6\u7684\u6570\u636e\u4fdd\u7559\u4e0b\u6700\u91cd\u8981\u7684\u4e00\u4e9b\u7279\u5f81\uff0c\u53bb\u9664\u566a\u58f0\u548c\u4e0d\u91cd\u8981\u7684\u7279\u5f81\uff0c\u4ece\u800c\u5b9e\u73b0\u63d0\u5347\u6570\u636e\u5904\u7406\u901f\u5ea6\u7684\u76ee\u7684\u3002\u5728\u5b9e\u9645\u7684\u751f\u4ea7\u548c\u5e94\u7528\u4e2d\uff0c\u964d\u7ef4\u5728\u4e00\u5b9a\u7684\u4fe1\u606f\u635f\u5931\u8303\u56f4\u5185\uff0c\u53ef\u4ee5\u4e3a\u6211\u4eec\u8282\u7701\u5927\u91cf\u7684\u65f6\u95f4\u548c\u6210\u672c\u3002\u964d\u7ef4\u4e5f\u6210\u4e3a\u5e94\u7528\u975e\u5e38\u5e7f\u6cdb\u7684\u6570\u636e\u9884\u5904\u7406\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u964d\u7ef4\u5177\u6709\u5982\u4e0b\u4e00\u4e9b\u4f18\u70b9\uff1a<\/p>\n\n\n\n<ul><li>\u4f7f\u5f97\u6570\u636e\u96c6\u66f4\u6613\u4f7f\u7528\u3002<\/li><li>\u964d\u4f4e\u7b97\u6cd5\u7684\u8ba1\u7b97\u5f00\u9500\u3002<\/li><li>\u53bb\u9664\u566a\u58f0\u3002<\/li><li>\u4f7f\u5f97\u7ed3\u679c\u5bb9\u6613\u7406\u89e3\u3002<\/li><\/ul>\n\n\n\n<p>\u964d\u7ef4\u7684\u7b97\u6cd5\u6709\u5f88\u591a\uff0c\u6bd4\u5982<a rel=\"noreferrer noopener\" href=\"https:\/\/mp.weixin.qq.com\/s\/Dv51K8JETakIKe5dPBAPVg\" target=\"_blank\">\u5947\u5f02\u503c\u5206\u89e3(SVD)<\/a>\u3001\u4e3b\u6210\u5206\u5206\u6790(PCA)\u3001\u56e0\u5b50\u5206\u6790(FA)\u3001\u72ec\u7acb\u6210\u5206\u5206\u6790(ICA)\u3002<\/p>\n\n\n\n<p>       PCA(Principal Component Analysis)\uff0c\u5373\u4e3b\u6210\u5206\u5206\u6790\u65b9\u6cd5\uff0c\u662f\u4e00\u79cd\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684\u6570\u636e\u964d\u7ef4\u7b97\u6cd5\u3002PCA\u7684\u4e3b\u8981\u601d\u60f3\u662f\u5c06n\u7ef4\u7279\u5f81\u6620\u5c04\u5230k\u7ef4\u4e0a\uff0c\u8fd9k\u7ef4\u662f\u5168\u65b0\u7684\u6b63\u4ea4\u7279\u5f81\u4e5f\u88ab\u79f0\u4e3a\u4e3b\u6210\u5206\uff0c\u662f\u5728\u539f\u6709n\u7ef4\u7279\u5f81\u7684\u57fa\u7840\u4e0a\u91cd\u65b0\u6784\u9020\u51fa\u6765\u7684k\u7ef4\u7279\u5f81\u3002PCA\u7684\u5de5\u4f5c\u5c31\u662f\u4ece\u539f\u59cb\u7684\u7a7a\u95f4\u4e2d\u987a\u5e8f\u5730\u627e\u4e00\u7ec4\u76f8\u4e92\u6b63\u4ea4\u7684\u5750\u6807\u8f74\uff0c\u65b0\u7684\u5750\u6807\u8f74\u7684\u9009\u62e9\u4e0e\u6570\u636e\u672c\u8eab\u662f\u5bc6\u5207\u76f8\u5173\u7684\u3002\u5176\u4e2d\uff0c\u7b2c\u4e00\u4e2a\u65b0\u5750\u6807\u8f74\u9009\u62e9\u662f\u539f\u59cb\u6570\u636e\u4e2d\u65b9\u5dee\u6700\u5927\u7684\u65b9\u5411\uff0c\u7b2c\u4e8c\u4e2a\u65b0\u5750\u6807\u8f74\u9009\u53d6\u662f\u4e0e\u7b2c\u4e00\u4e2a\u5750\u6807\u8f74\u6b63\u4ea4\u7684\u5e73\u9762\u4e2d\u4f7f\u5f97\u65b9\u5dee\u6700\u5927\u7684\uff0c\u7b2c\u4e09\u4e2a\u8f74\u662f\u4e0e\u7b2c1,2\u4e2a\u8f74\u6b63\u4ea4\u7684\u5e73\u9762\u4e2d\u65b9\u5dee\u6700\u5927\u7684\u3002\u4f9d\u6b21\u7c7b\u63a8\uff0c\u53ef\u4ee5\u5f97\u5230n\u4e2a\u8fd9\u6837\u7684\u5750\u6807\u8f74\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u83b7\u5f97\u7684\u65b0\u7684\u5750\u6807\u8f74\uff0c\u6211\u4eec\u53d1\u73b0\uff0c\u5927\u90e8\u5206\u65b9\u5dee\u90fd\u5305\u542b\u5728\u524d\u9762k\u4e2a\u5750\u6807\u8f74\u4e2d\uff0c\u540e\u9762\u7684\u5750\u6807\u8f74\u6240\u542b\u7684\u65b9\u5dee\u51e0\u4e4e\u4e3a0\u3002\u4e8e\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u5ffd\u7565\u4f59\u4e0b\u7684\u5750\u6807\u8f74\uff0c\u53ea\u4fdd\u7559\u524d\u9762k\u4e2a\u542b\u6709\u7edd\u5927\u90e8\u5206\u65b9\u5dee\u7684\u5750\u6807\u8f74\u3002\u4e8b\u5b9e\u4e0a\uff0c\u8fd9\u76f8\u5f53\u4e8e\u53ea\u4fdd\u7559\u5305\u542b\u7edd\u5927\u90e8\u5206\u65b9\u5dee\u7684\u7ef4\u5ea6\u7279\u5f81\uff0c\u800c\u5ffd\u7565\u5305\u542b\u65b9\u5dee\u51e0\u4e4e\u4e3a0\u7684\u7279\u5f81\u7ef4\u5ea6\uff0c\u5b9e\u73b0\u5bf9\u6570\u636e\u7279\u5f81\u7684\u964d\u7ef4\u5904\u7406\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5982\u4f55\u5f97\u5230\u8fd9\u4e9b\u5305\u542b\u6700\u5927\u5dee\u5f02\u6027\u7684\u4e3b\u6210\u5206\u65b9\u5411\u5462\uff1f<\/p>\n\n\n\n<p>\u4e8b\u5b9e\u4e0a\uff0c\u901a\u8fc7\u8ba1\u7b97\u6570\u636e\u77e9\u9635\u7684\u534f\u65b9\u5dee\u77e9\u9635\uff0c\u7136\u540e\u5f97\u5230\u534f\u65b9\u5dee\u77e9\u9635\u7684\u7279\u5f81\u503c\u7279\u5f81\u5411\u91cf\uff0c\u9009\u62e9\u7279\u5f81\u503c\u6700\u5927(\u5373\u65b9\u5dee\u6700\u5927)\u7684k\u4e2a\u7279\u5f81\u6240\u5bf9\u5e94\u7684\u7279\u5f81\u5411\u91cf\u7ec4\u6210\u7684\u77e9\u9635\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u6570\u636e\u77e9\u9635\u8f6c\u6362\u5230\u65b0\u7684\u7a7a\u95f4\u5f53\u4e2d\uff0c\u5b9e\u73b0\u6570\u636e\u7279\u5f81\u7684\u964d\u7ef4\u3002<\/p>\n\n\n\n<p><strong>\u57fa\u4e8e\u7279\u5f81\u503c\u5206\u89e3\u534f\u65b9\u5dee\u77e9\u9635\u5b9e\u73b0PCA\u7b97\u6cd5<\/strong><\/p>\n\n\n\n<p>\u8f93\u5165\uff1a\u6570\u636e\u96c6&nbsp;<img src=\"https:\/\/www.zhihu.com\/equation?tex=X%3D%5Cleft%5C%7B+x_%7B1%7D%2Cx_%7B2%7D%2Cx_%7B3%7D%2C...%2Cx_%7Bn%7D+%5Cright%5C%7D\" alt=\"[\u516c\u5f0f]\" class=\"\">&nbsp;\uff0c\u9700\u8981\u964d\u5230k\u7ef4\u3002<\/p>\n\n\n\n<p>1) \u53bb\u5e73\u5747\u503c(\u5373\u53bb\u4e2d\u5fc3\u5316)\uff0c\u5373\u6bcf\u4e00\u4f4d\u7279\u5f81\u51cf\u53bb\u5404\u81ea\u7684\u5e73\u5747\u503c\u3002<\/p>\n\n\n\n<p>2) \u8ba1\u7b97\u534f\u65b9\u5dee\u77e9\u9635&nbsp;<img src=\"https:\/\/www.zhihu.com\/equation?tex=%5Cfrac%7B1%7D%7Bn%7DXX%5ET\" alt=\"[\u516c\u5f0f]\">,\u6ce8\uff1a\u8fd9\u91cc\u9664\u6216\u4e0d\u9664\u6837\u672c\u6570\u91cfn\u6216n-1,\u5176\u5b9e\u5bf9\u6c42\u51fa\u7684\u7279\u5f81\u5411\u91cf\u6ca1\u6709\u5f71\u54cd\u3002<\/p>\n\n\n\n<p>3) \u7528\u7279\u5f81\u503c\u5206\u89e3\u65b9\u6cd5\u6c42\u534f\u65b9\u5dee\u77e9\u9635<img src=\"https:\/\/www.zhihu.com\/equation?tex=%5Cfrac%7B1%7D%7Bn%7DXX%5ET\" alt=\"[\u516c\u5f0f]\">&nbsp;\u7684\u7279\u5f81\u503c\u4e0e\u7279\u5f81\u5411\u91cf\u3002<\/p>\n\n\n\n<p>4) \u5bf9\u7279\u5f81\u503c\u4ece\u5927\u5230\u5c0f\u6392\u5e8f\uff0c\u9009\u62e9\u5176\u4e2d\u6700\u5927\u7684k\u4e2a\u3002\u7136\u540e\u5c06\u5176\u5bf9\u5e94\u7684k\u4e2a\u7279\u5f81\u5411\u91cf\u5206\u522b\u4f5c\u4e3a\u884c\u5411\u91cf\u7ec4\u6210\u7279\u5f81\u5411\u91cf\u77e9\u9635P\u3002<\/p>\n\n\n\n<p>5) \u5c06\u6570\u636e\u8f6c\u6362\u5230k\u4e2a\u7279\u5f81\u5411\u91cf\u6784\u5efa\u7684\u65b0\u7a7a\u95f4\u4e2d\uff0c\u5373Y=PX\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2>&nbsp;PCA\u5b9e\u4f8b<\/h2>\n\n\n\n<p>\uff081\uff09PCA\u7684Python\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>##Python\u5b9e\u73b0PCA<\/em>\nimport numpy as np\ndef pca(X,k):<em>#k is the components you want<\/em>\n  <em>#mean of each feature<\/em>\n  n_samples, n_features = X.shape\n  mean=np.array(&#91;np.mean(X&#91;:,i]) for i in range(n_features)])\n  <em>#normalization<\/em>\n  norm_X=X-mean\n  <em>#scatter matrix<\/em>\n  scatter_matrix=np.dot(np.transpose(norm_X),norm_X)\n  <em>#Calculate the eigenvectors and eigenvalues<\/em>\n  eig_val, eig_vec = np.linalg.eig(scatter_matrix)\n  eig_pairs = &#91;(np.abs(eig_val&#91;i]), eig_vec&#91;:,i]) for i in range(n_features)]\n  <em># sort eig_vec based on eig_val from highest to lowest<\/em>\n  eig_pairs.sort(reverse=True)\n  <em># select the top k eig_vec<\/em>\n  feature=np.array(&#91;ele&#91;1] for ele in eig_pairs&#91;:k]])\n  <em>#get new data<\/em>\n  data=np.dot(norm_X,np.transpose(feature))\n  return data\n\nX = np.array(&#91;&#91;-1, 1], &#91;-2, -1], &#91;-3, -2], &#91;1, 1], &#91;2, 1], &#91;3, 2]])\n\nprint(pca(X,1))<\/code><\/pre>\n\n\n\n<p>\u7528sklearn\u7684PCA\u4e0e\u6211\u4eec\u7684PCA\u505a\u4e2a\u6bd4\u8f83\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><em>##\u7528sklearn\u7684PCA<\/em>\nfrom sklearn.decomposition import PCA\nimport numpy as np\nX = np.array(&#91;&#91;-1, 1], &#91;-2, -1], &#91;-3, -2], &#91;1, 1], &#91;2, 1], &#91;3, 2]])\npca=PCA(n_components=1)\npca.fit(X)\nprint(pca.transform(X))<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u964d\u7ef4\u2014\u2014PCA \u964d\u7ef4\u5c31\u662f\u4e00\u79cd\u5bf9\u9ad8\u7ef4\u5ea6\u7279\u5f81\u6570\u636e\u9884\u5904\u7406\u65b9\u6cd5\u3002\u964d\u7ef4\u662f\u5c06\u9ad8\u7ef4\u5ea6\u7684\u6570\u636e\u4fdd\u7559\u4e0b\u6700\u91cd\u8981\u7684\u4e00\u4e9b\u7279\u5f81\uff0c\u53bb\u9664\u566a\u58f0\u548c &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2021\/12\/23\/pca\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">PCA<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,12],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/644"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=644"}],"version-history":[{"count":8,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/644\/revisions"}],"predecessor-version":[{"id":652,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/644\/revisions\/652"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=644"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}