{"id":991801,"date":"2024-12-27T08:32:08","date_gmt":"2024-12-27T00:32:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/991801.html"},"modified":"2024-12-27T08:32:10","modified_gmt":"2024-12-27T00:32:10","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e9%99%8d%e7%bb%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/991801.html","title":{"rendered":"\u5982\u4f55\u7528python\u964d\u7ef4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065914\/494fd8cb-31dc-47f7-a886-3a79d3efd9f9.webp\" alt=\"\u5982\u4f55\u7528python\u964d\u7ef4\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u8fdb\u884c\u964d\u7ef4\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4e3b\u6210\u5206\u5206\u6790(PCA)\u3001\u7ebf\u6027\u5224\u522b\u5206\u6790(LDA)\u3001\u5947\u5f02\u503c\u5206\u89e3(SVD)\u3001t\u5206\u5e03\u968f\u673a\u90bb\u5c45\u5d4c\u5165(t-SNE)\u3001\u975e\u8d1f\u77e9\u9635\u5206\u89e3(NMF)\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u96c6\u548c\u5206\u6790\u9700\u6c42\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<strong>\u4e3b\u6210\u5206\u5206\u6790(PCA)<\/strong>\u662f\u6700\u4e3a\u5e38\u7528\u548c\u57fa\u7840\u7684\u964d\u7ef4\u65b9\u6cd5\u3002PCA\u901a\u8fc7\u7ebf\u6027\u53d8\u6362\u5c06\u539f\u59cb\u6570\u636e\u6295\u5f71\u5230\u4f4e\u7ef4\u7a7a\u95f4\uff0c\u4fdd\u7559\u6570\u636e\u96c6\u4e2d\u7684\u5927\u90e8\u5206\u65b9\u5dee\uff0c\u4f7f\u5f97\u964d\u7ef4\u540e\u7684\u6570\u636e\u80fd\u591f\u6709\u6548\u4ee3\u8868\u539f\u59cb\u6570\u636e\u3002PCA\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\u6807\u51c6\u5316\u6570\u636e\u3001\u8ba1\u7b97\u534f\u65b9\u5dee\u77e9\u9635\u3001\u8ba1\u7b97\u534f\u65b9\u5dee\u77e9\u9635\u7684\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf\u3001\u9009\u62e9\u4e3b\u8981\u6210\u5206\u8fdb\u884c\u964d\u7ef4\u3002\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecdPCA\u7684\u5b9e\u73b0\u8fc7\u7a0b\u53ca\u5176\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4e3b\u6210\u5206\u5206\u6790(PCA)<\/h3>\n<\/p>\n<p><h4>1. \u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>\u4e3b\u6210\u5206\u5206\u6790\uff08PCA\uff09\u662f\u4e00\u79cd\u7ebf\u6027\u964d\u7ef4\u6280\u672f\uff0c\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u7684\u964d\u7ef4\u548c\u7279\u5f81\u63d0\u53d6\u3002\u5b83\u901a\u8fc7\u5bf9\u6570\u636e\u8fdb\u884c\u6b63\u4ea4\u53d8\u6362\uff0c\u5c06\u6570\u636e\u6295\u5f71\u5230\u65b0\u7684\u5750\u6807\u7cfb\u4e2d\uff0c\u4f7f\u5f97\u6295\u5f71\u540e\u7684\u6570\u636e\u65b9\u5dee\u6700\u5927\u5316\u3002PCA\u7684\u76ee\u6807\u662f\u627e\u5230\u6570\u636e\u7684\u4e3b\u6210\u5206\uff0c\u5373\u4e00\u7ec4\u4e92\u76f8\u6b63\u4ea4\u7684\u5411\u91cf\uff0c\u8fd9\u4e9b\u5411\u91cf\u80fd\u591f\u6355\u83b7\u6570\u636e\u4e2d\u7684\u6700\u5927\u4fe1\u606f\u91cf\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b9e\u73b0\u6b65\u9aa4<\/h4>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6807\u51c6\u5316\u6570\u636e<\/strong>\uff1a\u5bf9\u6570\u636e\u8fdb\u884c\u6807\u51c6\u5316\u5904\u7406\uff0c\u4f7f\u5f97\u6bcf\u4e2a\u7279\u5f81\u7684\u5747\u503c\u4e3a0\uff0c\u65b9\u5dee\u4e3a1\u3002\u8fd9\u4e00\u6b65\u80fd\u591f\u6d88\u9664\u4e0d\u540c\u7279\u5f81\u91cf\u7eb2\u4e4b\u95f4\u7684\u5f71\u54cd\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8ba1\u7b97\u534f\u65b9\u5dee\u77e9\u9635<\/strong>\uff1a\u5bf9\u4e8e\u6807\u51c6\u5316\u540e\u7684\u6570\u636e\uff0c\u8ba1\u7b97\u5176\u534f\u65b9\u5dee\u77e9\u9635\u3002\u534f\u65b9\u5dee\u77e9\u9635\u53cd\u6620\u4e86\u4e0d\u540c\u7279\u5f81\u4e4b\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf<\/strong>\uff1a\u8ba1\u7b97\u534f\u65b9\u5dee\u77e9\u9635\u7684\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf\u3002\u7279\u5f81\u503c\u8868\u793a\u6570\u636e\u5728\u7279\u5f81\u5411\u91cf\u65b9\u5411\u4e0a\u7684\u65b9\u5dee\u5927\u5c0f\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u62e9\u4e3b\u8981\u6210\u5206<\/strong>\uff1a\u6839\u636e\u7279\u5f81\u503c\u7684\u5927\u5c0f\u6392\u5e8f\uff0c\u9009\u62e9\u524dk\u4e2a\u7279\u5f81\u5411\u91cf\u4f5c\u4e3a\u4e3b\u8981\u6210\u5206\uff0c\u7528\u4e8e\u6570\u636e\u7684\u964d\u7ef4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u964d\u7ef4\u8f6c\u6362<\/strong>\uff1a\u5c06\u539f\u59cb\u6570\u636e\u6295\u5f71\u5230\u9009\u5b9a\u7684\u4e3b\u8981\u6210\u5206\u4e0a\uff0c\u5f97\u5230\u964d\u7ef4\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h4>3. Python\u5b9e\u73b0<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528scikit-learn\u5e93\u4e2d\u7684<code>PCA<\/code>\u7c7b\u6765\u5b9e\u73b0\u4e3b\u6210\u5206\u5206\u6790\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.decomposition import PCA<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>X = np.array([[2.5, 2.4],<\/p>\n<p>              [0.5, 0.7],<\/p>\n<p>              [2.2, 2.9],<\/p>\n<p>              [1.9, 2.2],<\/p>\n<p>              [3.1, 3.0],<\/p>\n<p>              [2.3, 2.7],<\/p>\n<p>              [2, 1.6],<\/p>\n<p>              [1, 1.1],<\/p>\n<p>              [1.5, 1.6],<\/p>\n<p>              [1.1, 0.9]])<\/p>\n<h2><strong>\u521b\u5efaPCA\u5bf9\u8c61\uff0c\u8bbe\u7f6e\u4fdd\u7559\u7684\u6210\u5206\u6570\u91cf<\/strong><\/h2>\n<p>pca = PCA(n_components=1)<\/p>\n<h2><strong>\u62df\u5408\u6a21\u578b\u5e76\u8fdb\u884c\u964d\u7ef4<\/strong><\/h2>\n<p>X_reduced = pca.fit_transform(X)<\/p>\n<p>print(&quot;\u964d\u7ef4\u540e\u7684\u6570\u636e\uff1a&quot;)<\/p>\n<p>print(X_reduced)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0cPCA\u53ef\u4ee5\u6709\u6548\u5730\u51cf\u5c11\u6570\u636e\u7684\u7ef4\u5ea6\uff0c\u540c\u65f6\u4fdd\u7559\u5c3d\u53ef\u80fd\u591a\u7684\u539f\u59cb\u4fe1\u606f\u3002\u8fd9\u5bf9\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3001\u964d\u566a\u3001\u7279\u5f81\u9009\u62e9\u7b49\u4efb\u52a1\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u7ebf\u6027\u5224\u522b\u5206\u6790(LDA)<\/h3>\n<\/p>\n<p><h4>1. \u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>\u7ebf\u6027\u5224\u522b\u5206\u6790\uff08LDA\uff09\u662f\u4e00\u79cd\u7528\u4e8e\u5206\u7c7b\u7684\u964d\u7ef4\u6280\u672f\u3002\u4e0ePCA\u4e0d\u540c\uff0cLDA\u4e0d\u4ec5\u8003\u8651\u6570\u636e\u7684\u65b9\u5dee\uff0c\u8fd8\u8003\u8651\u7c7b\u5185\u548c\u7c7b\u95f4\u7684\u5206\u5e03\u3002LDA\u7684\u76ee\u6807\u662f\u627e\u5230\u4e00\u4e2a\u6295\u5f71\u65b9\u5411\uff0c\u4f7f\u5f97\u6295\u5f71\u540e\u7c7b\u5185\u65b9\u5dee\u6700\u5c0f\u5316\uff0c\u7c7b\u95f4\u65b9\u5dee\u6700\u5927\u5316\uff0c\u4ece\u800c\u63d0\u9ad8\u5206\u7c7b\u7684\u53ef\u5206\u6027\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b9e\u73b0\u6b65\u9aa4<\/h4>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8ba1\u7b97\u7c7b\u5185\u3001\u7c7b\u95f4\u6563\u5ea6\u77e9\u9635<\/strong>\uff1a\u5bf9\u4e8e\u6bcf\u4e2a\u7c7b\u522b\uff0c\u8ba1\u7b97\u7c7b\u5185\u6563\u5ea6\u77e9\u9635\uff1b\u5bf9\u4e8e\u6240\u6709\u7c7b\u522b\uff0c\u8ba1\u7b97\u7c7b\u95f4\u6563\u5ea6\u77e9\u9635\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6c42\u89e3\u5e7f\u4e49\u7279\u5f81\u503c\u95ee\u9898<\/strong>\uff1a\u901a\u8fc7\u6c42\u89e3\u5e7f\u4e49\u7279\u5f81\u503c\u95ee\u9898\uff0c\u627e\u5230\u80fd\u591f\u4f18\u5316\u7c7b\u5185\u548c\u7c7b\u95f4\u6563\u5ea6\u7684\u6295\u5f71\u65b9\u5411\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u62e9\u6295\u5f71\u65b9\u5411<\/strong>\uff1a\u9009\u62e9\u524dk\u4e2a\u7279\u5f81\u5411\u91cf\u4f5c\u4e3a\u6295\u5f71\u65b9\u5411\uff0c\u7528\u4e8e\u964d\u7ef4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6295\u5f71\u6570\u636e<\/strong>\uff1a\u5c06\u539f\u59cb\u6570\u636e\u6295\u5f71\u5230\u9009\u5b9a\u7684\u65b9\u5411\u4e0a\uff0c\u5f97\u5230\u964d\u7ef4\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h4>3. Python\u5b9e\u73b0<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0cLDA\u53ef\u4ee5\u901a\u8fc7scikit-learn\u5e93\u4e2d\u7684<code>LinearDiscriminantAnalysis<\/code>\u7c7b\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA<\/p>\n<p>from sklearn.datasets import load_iris<\/p>\n<h2><strong>\u52a0\u8f7d\u793a\u4f8b\u6570\u636e\u96c6<\/strong><\/h2>\n<p>data = load_iris()<\/p>\n<p>X = data.data<\/p>\n<p>y = data.target<\/p>\n<h2><strong>\u521b\u5efaLDA\u5bf9\u8c61\uff0c\u8bbe\u7f6e\u4fdd\u7559\u7684\u6210\u5206\u6570\u91cf<\/strong><\/h2>\n<p>lda = LDA(n_components=2)<\/p>\n<h2><strong>\u62df\u5408\u6a21\u578b\u5e76\u8fdb\u884c\u964d\u7ef4<\/strong><\/h2>\n<p>X_reduced = lda.fit_transform(X, y)<\/p>\n<p>print(&quot;\u964d\u7ef4\u540e\u7684\u6570\u636e\uff1a&quot;)<\/p>\n<p>print(X_reduced)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>LDA\u901a\u8fc7\u6700\u5927\u5316\u7c7b\u95f4\u6563\u5ea6\u4e0e\u7c7b\u5185\u6563\u5ea6\u7684\u6bd4\u503c\uff0c\u80fd\u591f\u6709\u6548\u63d0\u9ad8\u6570\u636e\u7684\u53ef\u5206\u6027\uff0c\u5bf9\u4e8e\u5206\u7c7b\u4efb\u52a1\u4e2d\u7684\u964d\u7ef4\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5947\u5f02\u503c\u5206\u89e3(SVD)<\/h3>\n<\/p>\n<p><h4>1. \u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>\u5947\u5f02\u503c\u5206\u89e3\uff08SVD\uff09\u662f\u4e00\u79cd\u77e9\u9635\u5206\u89e3\u6280\u672f\uff0c\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u964d\u7ef4\u3002SVD\u5c06\u4e00\u4e2a\u77e9\u9635\u5206\u89e3\u4e3a\u4e09\u4e2a\u77e9\u9635\u7684\u4e58\u79ef\uff0c\u80fd\u591f\u63d0\u53d6\u51fa\u6570\u636e\u4e2d\u7684\u91cd\u8981\u7ed3\u6784\u4fe1\u606f\u3002SVD\u7684\u964d\u7ef4\u8fc7\u7a0b\u7c7b\u4f3c\u4e8ePCA\uff0c\u4f46\u5b83\u4e0d\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u4e2d\u5fc3\u5316\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b9e\u73b0\u6b65\u9aa4<\/h4>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8ba1\u7b97SVD<\/strong>\uff1a\u5bf9\u4e8e\u7ed9\u5b9a\u6570\u636e\u77e9\u9635\uff0c\u8ba1\u7b97\u5176\u5947\u5f02\u503c\u5206\u89e3\uff0c\u5f97\u5230U\u3001\u03a3\u3001V^T\u77e9\u9635\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u62e9\u5947\u5f02\u503c<\/strong>\uff1a\u6839\u636e\u5947\u5f02\u503c\u7684\u5927\u5c0f\uff0c\u9009\u62e9\u524dk\u4e2a\u5947\u5f02\u503c\u5bf9\u5e94\u7684\u5411\u91cf\uff0c\u7528\u4e8e\u964d\u7ef4\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u964d\u7ef4\u8f6c\u6362<\/strong>\uff1a\u5c06\u539f\u59cb\u6570\u636e\u6295\u5f71\u5230\u9009\u5b9a\u7684\u5947\u5f02\u503c\u5bf9\u5e94\u7684\u5411\u91cf\u4e0a\uff0c\u5f97\u5230\u964d\u7ef4\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h4>3. Python\u5b9e\u73b0<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528numpy\u5e93\u4e2d\u7684<code>svd<\/code>\u51fd\u6570\u6765\u5b9e\u73b0SVD\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>X = np.array([[2.5, 2.4],<\/p>\n<p>              [0.5, 0.7],<\/p>\n<p>              [2.2, 2.9],<\/p>\n<p>              [1.9, 2.2],<\/p>\n<p>              [3.1, 3.0],<\/p>\n<p>              [2.3, 2.7],<\/p>\n<p>              [2, 1.6],<\/p>\n<p>              [1, 1.1],<\/p>\n<p>              [1.5, 1.6],<\/p>\n<p>              [1.1, 0.9]])<\/p>\n<h2><strong>\u8ba1\u7b97SVD<\/strong><\/h2>\n<p>U, S, Vt = np.linalg.svd(X, full_matrices=False)<\/p>\n<h2><strong>\u9009\u62e9\u524dk\u4e2a\u5947\u5f02\u503c\u5bf9\u5e94\u7684\u5411\u91cf<\/strong><\/h2>\n<p>k = 1<\/p>\n<p>X_reduced = U[:, :k] @ np.diag(S[:k])<\/p>\n<p>print(&quot;\u964d\u7ef4\u540e\u7684\u6570\u636e\uff1a&quot;)<\/p>\n<p>print(X_reduced)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>SVD\u5728\u6570\u636e\u538b\u7f29\u3001\u7279\u5f81\u63d0\u53d6\u3001\u534f\u540c\u8fc7\u6ee4\u7b49\u9886\u57df\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u80fd\u591f\u6709\u6548\u63d0\u53d6\u6570\u636e\u4e2d\u7684\u91cd\u8981\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001t\u5206\u5e03\u968f\u673a\u90bb\u5c45\u5d4c\u5165(t-SNE)<\/h3>\n<\/p>\n<p><h4>1. \u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>t-SNE\u662f\u4e00\u79cd\u975e\u7ebf\u6027\u964d\u7ef4\u6280\u672f\uff0c\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u7684\u53ef\u89c6\u5316\u3002\u4e0e\u7ebf\u6027\u65b9\u6cd5\u4e0d\u540c\uff0ct-SNE\u901a\u8fc7\u6784\u9020\u6982\u7387\u5206\u5e03\uff0c\u5c06\u9ad8\u7ef4\u6570\u636e\u6620\u5c04\u5230\u4f4e\u7ef4\u7a7a\u95f4\uff0c\u540c\u65f6\u4fdd\u7559\u6570\u636e\u7684\u5c40\u90e8\u7ed3\u6784\u3002t-SNE\u5728\u5904\u7406\u590d\u6742\u6570\u636e\u96c6\u65f6\uff0c\u80fd\u591f\u6709\u6548\u63ed\u793a\u6570\u636e\u7684\u6f5c\u5728\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b9e\u73b0\u6b65\u9aa4<\/h4>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6784\u5efa\u6982\u7387\u5206\u5e03<\/strong>\uff1a\u5728\u9ad8\u7ef4\u7a7a\u95f4\u4e2d\uff0c\u8ba1\u7b97\u6570\u636e\u70b9\u5bf9\u4e4b\u95f4\u7684\u76f8\u4f3c\u6027\uff0c\u6784\u5efa\u6761\u4ef6\u6982\u7387\u5206\u5e03\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4f4e\u7ef4\u6620\u5c04<\/strong>\uff1a\u5728\u4f4e\u7ef4\u7a7a\u95f4\u4e2d\uff0c\u5bfb\u627e\u6570\u636e\u70b9\u7684\u6620\u5c04\uff0c\u4f7f\u5f97\u9ad8\u7ef4\u548c\u4f4e\u7ef4\u7a7a\u95f4\u7684\u6982\u7387\u5206\u5e03\u5c3d\u53ef\u80fd\u76f8\u4f3c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6700\u5c0f\u5316KL\u6563\u5ea6<\/strong>\uff1a\u901a\u8fc7\u68af\u5ea6\u4e0b\u964d\u6cd5\uff0c\u6700\u5c0f\u5316\u9ad8\u7ef4\u548c\u4f4e\u7ef4\u6982\u7387\u5206\u5e03\u4e4b\u95f4\u7684KL\u6563\u5ea6\uff0c\u5f97\u5230\u6700\u7ec8\u7684\u4f4e\u7ef4\u5d4c\u5165\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h4>3. Python\u5b9e\u73b0<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0ct-SNE\u53ef\u4ee5\u901a\u8fc7scikit-learn\u5e93\u4e2d\u7684<code>TSNE<\/code>\u7c7b\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.manifold import TSNE<\/p>\n<p>from sklearn.datasets import load_iris<\/p>\n<h2><strong>\u52a0\u8f7d\u793a\u4f8b\u6570\u636e\u96c6<\/strong><\/h2>\n<p>data = load_iris()<\/p>\n<p>X = data.data<\/p>\n<h2><strong>\u521b\u5efat-SNE\u5bf9\u8c61\uff0c\u8bbe\u7f6e\u4fdd\u7559\u7684\u6210\u5206\u6570\u91cf<\/strong><\/h2>\n<p>tsne = TSNE(n_components=2)<\/p>\n<h2><strong>\u62df\u5408\u6a21\u578b\u5e76\u8fdb\u884c\u964d\u7ef4<\/strong><\/h2>\n<p>X_reduced = tsne.fit_transform(X)<\/p>\n<p>print(&quot;\u964d\u7ef4\u540e\u7684\u6570\u636e\uff1a&quot;)<\/p>\n<p>print(X_reduced)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>t-SNE\u5728\u5904\u7406\u9ad8\u7ef4\u6570\u636e\u7684\u53ef\u89c6\u5316\u4efb\u52a1\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u80fd\u591f\u6709\u6548\u63ed\u793a\u6570\u636e\u7684\u6f5c\u5728\u6a21\u5f0f\u548c\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u975e\u8d1f\u77e9\u9635\u5206\u89e3(NMF)<\/h3>\n<\/p>\n<p><h4>1. \u57fa\u672c\u6982\u5ff5<\/h4>\n<\/p>\n<p><p>\u975e\u8d1f\u77e9\u9635\u5206\u89e3\uff08NMF\uff09\u662f\u4e00\u79cd\u77e9\u9635\u5206\u89e3\u6280\u672f\uff0c\u4e3b\u8981\u7528\u4e8e\u975e\u8d1f\u6570\u636e\u7684\u964d\u7ef4\u548c\u7279\u5f81\u63d0\u53d6\u3002NMF\u5c06\u4e00\u4e2a\u975e\u8d1f\u77e9\u9635\u5206\u89e3\u4e3a\u4e24\u4e2a\u975e\u8d1f\u77e9\u9635\u7684\u4e58\u79ef\uff0c\u80fd\u591f\u63d0\u53d6\u51fa\u6570\u636e\u4e2d\u7684\u6f5c\u5728\u4e3b\u9898\u548c\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><h4>2. \u5b9e\u73b0\u6b65\u9aa4<\/h4>\n<\/p>\n<ol>\n<li>\n<p><strong>\u521d\u59cb\u5316\u77e9\u9635<\/strong>\uff1a\u968f\u673a\u521d\u59cb\u5316\u4e24\u4e2a\u975e\u8d1f\u77e9\u9635W\u548cH\uff0c\u4f7f\u5f97\u5b83\u4eec\u7684\u4e58\u79ef\u63a5\u8fd1\u4e8e\u539f\u59cb\u77e9\u9635\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fed\u4ee3\u66f4\u65b0<\/strong>\uff1a\u901a\u8fc7\u8fed\u4ee3\u66f4\u65b0W\u548cH\uff0c\u6700\u5c0f\u5316\u5b83\u4eec\u7684\u4e58\u79ef\u4e0e\u539f\u59cb\u77e9\u9635\u4e4b\u95f4\u7684\u8bef\u5dee\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6536\u655b\u6761\u4ef6<\/strong>\uff1a\u6839\u636e\u8bef\u5dee\u7684\u53d8\u5316\uff0c\u5224\u65ad\u662f\u5426\u8fbe\u5230\u6536\u655b\u6761\u4ef6\uff0c\u505c\u6b62\u8fed\u4ee3\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h4>3. Python\u5b9e\u73b0<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528scikit-learn\u5e93\u4e2d\u7684<code>NMF<\/code>\u7c7b\u6765\u5b9e\u73b0\u975e\u8d1f\u77e9\u9635\u5206\u89e3\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.decomposition import NMF<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u975e\u8d1f\u6570\u636e<\/strong><\/h2>\n<p>X = np.array([[2, 3],<\/p>\n<p>              [3, 4],<\/p>\n<p>              [4, 5],<\/p>\n<p>              [5, 6]])<\/p>\n<h2><strong>\u521b\u5efaNMF\u5bf9\u8c61\uff0c\u8bbe\u7f6e\u4fdd\u7559\u7684\u6210\u5206\u6570\u91cf<\/strong><\/h2>\n<p>nmf = NMF(n_components=2, init=&#39;random&#39;, random_state=0)<\/p>\n<h2><strong>\u62df\u5408\u6a21\u578b\u5e76\u8fdb\u884c\u5206\u89e3<\/strong><\/h2>\n<p>W = nmf.fit_transform(X)<\/p>\n<p>H = nmf.components_<\/p>\n<p>print(&quot;\u5206\u89e3\u540e\u7684\u77e9\u9635\uff1a&quot;)<\/p>\n<p>print(&quot;W:&quot;)<\/p>\n<p>print(W)<\/p>\n<p>print(&quot;H:&quot;)<\/p>\n<p>print(H)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>NMF\u5728\u6587\u672c\u6316\u6398\u3001\u56fe\u50cf\u5904\u7406\u7b49\u9886\u57df\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u80fd\u591f\u6709\u6548\u63d0\u53d6\u6570\u636e\u4e2d\u7684\u6f5c\u5728\u4e3b\u9898\u548c\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u5404\u79cd\u65b9\u6cd5\uff0cPython\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u5de5\u5177\u548c\u5e93\uff0c\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u6709\u6548\u5730\u8fdb\u884c\u6570\u636e\u7684\u964d\u7ef4\u548c\u7279\u5f81\u63d0\u53d6\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u6570\u636e\u7684\u7279\u6027\u548c\u5206\u6790\u7684\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u964d\u7ef4\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u964d\u7ef4\u5728\u6570\u636e\u5206\u6790\u4e2d\u7684\u91cd\u8981\u6027\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u964d\u7ef4\u662f\u6307\u5c06\u9ad8\u7ef4\u6570\u636e\u8f6c\u6362\u4e3a\u4f4e\u7ef4\u6570\u636e\u7684\u8fc7\u7a0b\uff0c\u8fd9\u5728\u6570\u636e\u5206\u6790\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\u5177\u6709\u91cd\u8981\u610f\u4e49\u3002\u9ad8\u7ef4\u6570\u636e\u53ef\u80fd\u4f1a\u5bfc\u81f4\u201c\u7ef4\u5ea6\u8bc5\u5492\u201d\uff0c\u4f7f\u5f97\u6a21\u578b\u7684\u8bad\u7ec3\u548c\u9884\u6d4b\u53d8\u5f97\u590d\u6742\u4e14\u4e0d\u7a33\u5b9a\u3002\u901a\u8fc7\u964d\u7ef4\uff0c\u53ef\u4ee5\u4fdd\u7559\u6570\u636e\u4e2d\u7684\u4e3b\u8981\u4fe1\u606f\uff0c\u540c\u65f6\u51cf\u5c11\u8ba1\u7b97\u6210\u672c\uff0c\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u548c\u53ef\u89e3\u91ca\u6027\u3002<\/p>\n<p><strong>Python\u4e2d\u5e38\u7528\u7684\u964d\u7ef4\u65b9\u6cd5\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u964d\u7ef4\u6280\u672f\u53ef\u4f9b\u9009\u62e9\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4e3b\u6210\u5206\u5206\u6790\uff08PCA\uff09\u3001t-SNE\u3001\u7ebf\u6027\u5224\u522b\u5206\u6790\uff08LDA\uff09\u548c\u81ea\u7f16\u7801\u5668\u7b49\u3002PCA\u662f\u4e00\u79cd\u7ebf\u6027\u964d\u7ef4\u65b9\u6cd5\uff0c\u9002\u5408\u5904\u7406\u5927\u591a\u6570\u6570\u636e\u96c6\uff1b\u800ct-SNE\u5219\u9002\u5408\u53ef\u89c6\u5316\u9ad8\u7ef4\u6570\u636e\uff0c\u5c24\u5176\u662f\u5728\u4fdd\u7559\u5c40\u90e8\u7ed3\u6784\u65b9\u9762\u8868\u73b0\u51fa\u8272\u3002\u9009\u62e9\u5408\u9002\u7684\u964d\u7ef4\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u7279\u6027\u548c\u5206\u6790\u7684\u76ee\u6807\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u964d\u7ef4\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u8bf8\u5982Scikit-learn\u548cTensorFlow\u7b49\u5e93\u6765\u5b9e\u73b0\u964d\u7ef4\u3002\u4ee5PCA\u4e3a\u4f8b\uff0c\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u964d\u7ef4\uff1a\u9996\u5148\uff0c\u5bfc\u5165\u6240\u9700\u7684\u5e93\u5e76\u52a0\u8f7d\u6570\u636e\uff1b\u63a5\u7740\uff0c\u521b\u5efaPCA\u5bf9\u8c61\u5e76\u6307\u5b9a\u964d\u7ef4\u540e\u7684\u7ef4\u5ea6\uff1b\u6700\u540e\uff0c\u8c03\u7528fit_transform\u65b9\u6cd5\u6765\u6267\u884c\u964d\u7ef4\u64cd\u4f5c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from sklearn.decomposition import PCA\nfrom sklearn.datasets import load_iris\n\n# \u52a0\u8f7d\u6570\u636e\ndata = load_iris()\nX = data.data\n\n# \u521b\u5efaPCA\u5bf9\u8c61\npca = PCA(n_components=2)\n\n# \u6267\u884c\u964d\u7ef4\nX_reduced = pca.fit_transform(X)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u5feb\u901f\u5730\u5c06\u6570\u636e\u4ece\u9ad8\u7ef4\u7a7a\u95f4\u6620\u5c04\u5230\u4f4e\u7ef4\u7a7a\u95f4\uff0c\u4fbf\u4e8e\u540e\u7eed\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u8fdb\u884c\u964d\u7ef4\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4e3b\u6210\u5206\u5206\u6790(PCA)\u3001\u7ebf\u6027\u5224\u522b\u5206\u6790(LDA)\u3001\u5947\u5f02\u503c\u5206\u89e3(SVD)\u3001t\u5206\u5e03 [&hellip;]","protected":false},"author":3,"featured_media":991813,"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\/991801"}],"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=991801"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/991801\/revisions"}],"predecessor-version":[{"id":991815,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/991801\/revisions\/991815"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/991813"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=991801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=991801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=991801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}