<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 淺談機器學習的奇異值分解 > 原文: [https://machinelearningmastery.com/singular-value-decomposition-for-machine-learning/](https://machinelearningmastery.com/singular-value-decomposition-for-machine-learning/) 矩陣分解,也稱為矩陣分解,涉及使用其組成元素描述給定矩陣。 也許最著名和最廣泛使用的矩陣分解方法是奇異值分解或 SVD。所有矩陣都有一個 SVD,這使得它比其他方法更穩定,例如特征分解。因此,它經常用于各種應用,包括壓縮,去噪和數據縮減。 在本教程中,您將發現用于將矩陣分解為其組成元素的奇異值分解方法。 完成本教程后,您將了解: * 奇異值分解是什么以及涉及什么。 * 如何計算 SVD 并從 SVD 元素重建矩形和方形矩陣。 * 如何使用 SVD 計算偽逆并執行降維 讓我們開始吧。 * **更新 Mar / 2018** :修復了重建中的拼寫錯誤。為清晰起見,將代碼中的 V 更改為 VT。修正了偽逆方程中的拼寫錯誤。 ![A Gentle Introduction to Singular-Value Decomposition](img/f2ce7cb34e8831e36bbc9775cd8fded9.jpg) 奇異值分解 照片由 [Chris Heald](https://www.flickr.com/photos/husker_alum/8628799410/) 拍攝,保留一些權利。 ## 教程概述 本教程分為 5 個部分;他們是: 1. 奇異值分解 2. 計算奇異值分解 3. 從 SVD 重構矩陣 4. 偽逆的 SVD 5. 用于降維的 SVD ## 奇異值分解 奇異值分解(簡稱 SVD)是一種矩陣分解方法,用于將矩陣減少到其組成部分,以使某些后續矩陣計算更簡單。 為簡單起見,我們將重點關注實值矩陣的 SVD,并忽略復數的情況。 ``` A = U . Sigma . V^T ``` 其中 A 是我們希望分解的真實 mxn 矩陣,U 是 mxm 矩陣,Sigma(通常由大寫希臘字母 Sigma 表示)是 mxn 對角矩陣,V ^ T 是 nxn 矩陣的轉置,其中 T 是一個上標。 > 奇異值分解是線性代數的一個亮點。 - 第 371 頁,[線性代數導論](http://amzn.to/2AZ7R8j),第五版,2016 年。 Sigma 矩陣中的對角線值稱為原始矩陣 A 的奇異值.U 矩陣的列稱為 A 的左奇異向量,V 列稱為 A 的右奇異向量。 通過迭代數值方法計算 SVD。我們不會詳細介紹這些方法。每個矩形矩陣都具有奇異值分解,盡管得到的矩陣可能包含復數,浮點運算的局限性可能會導致某些矩陣無法整齊地分解。 > 奇異值分解(SVD)提供了另一種將矩陣分解為奇異向量和奇異值的方法。 SVD 允許我們發現一些與特征分解相同的信息。但是,SVD 更普遍適用。 - 第 44-45 頁,[深度學習](http://amzn.to/2B3MsuU),2016 年。 SVD 廣泛用于計算其他矩陣運算,例如矩陣逆運算,但也作為機器學習中的數據簡化方法。 SVD 還可用于最小二乘線性回歸,圖像壓縮和去噪數據。 > 奇異值分解(SVD)在統計學,機器學習和計算機科學中有許多應用。將 SVD 應用于矩陣就像在 X 射線視覺中查看它... - 第 297 頁,[無線性代數廢話指南](http://amzn.to/2k76D4C),2017 年 ## 計算奇異值分解 可以通過調用 svd()函數來計算 SVD。 該函數采用矩陣并返回 U,Sigma 和 V ^ T 元素。 Sigma 對角矩陣作為奇異值的向量返回。 V 矩陣以轉置的形式返回,例如, V.T. 下面的示例定義了 3×2 矩陣并計算奇異值分解。 ``` # Singular-value decomposition from numpy import array from scipy.linalg import svd # define a matrix A = array([[1, 2], [3, 4], [5, 6]]) print(A) # SVD U, s, VT = svd(A) print(U) print(s) print(VT) ``` 首先運行該示例打印定義的 3×2 矩陣,然后打印 3×3U 矩陣,2 元素 Sigma 向量和從分解計算的 2×2V ^ T 矩陣元素。 ``` [[1 2] [3 4] [5 6]] [[-0.2298477 0.88346102 0.40824829] [-0.52474482 0.24078249 -0.81649658] [-0.81964194 -0.40189603 0.40824829]] [ 9.52551809 0.51430058] [[-0.61962948 -0.78489445] [-0.78489445 0.61962948]] ``` ## 從 SVD 重構矩陣 可以從 U,Sigma 和 V ^ T 元素重建原始矩陣。 從 svd()返回的 U,s 和 V 元素不能直接相乘。 必須使用 diag()函數將 s 向量轉換為對角矩陣。默認情況下,此函數將創建一個相對于原始矩陣 m x m 的方陣。這導致問題,因為矩陣的大小不符合矩陣乘法的規則,其中矩陣中的列數必須與后續矩陣中的行數匹配。 在創建方形 Sigma 對角矩陣之后,矩陣的大小相對于我們正在分解的原始 m x n 矩陣,如下所示: ``` U (m x m) . Sigma (m x m) . V^T (n x n) ``` 事實上,我們要求: ``` U (m x m) . Sigma (m x n) . V^T (n x n) ``` 我們可以通過創建所有零值 m x n(例如更多行)的新 Sigma 格式來實現這一點,并用通過 diag()計算的方形對角矩陣填充矩陣的前 n x n 部分。 ``` # Reconstruct SVD from numpy import array from numpy import diag from numpy import dot from numpy import zeros from scipy.linalg import svd # define a matrix A = array([[1, 2], [3, 4], [5, 6]]) print(A) # Singular-value decomposition U, s, VT = svd(A) # create m x n Sigma matrix Sigma = zeros((A.shape[0], A.shape[1])) # populate Sigma with n x n diagonal matrix Sigma[:A.shape[1], :A.shape[1]] = diag(s) # reconstruct matrix B = U.dot(Sigma.dot(VT)) print(B) ``` 首先運行該示例打印原始矩陣,然后打印從 SVD 元素重建的矩陣。 ``` [[1 2] [3 4] [5 6]] [[ 1\. 2.] [ 3\. 4.] [ 5\. 6.]] ``` 上述與 Sigma 對角線的復雜性僅存在于 m 和 n 不相等的情況下。當重建方形矩陣時,可以直接使用對角矩陣,如下所述。 ``` # Reconstruct SVD from numpy import array from numpy import diag from numpy import dot from scipy.linalg import svd # define a matrix A = array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(A) # Singular-value decomposition U, s, VT = svd(A) # create n x n Sigma matrix Sigma = diag(s) # reconstruct matrix B = U.dot(Sigma.dot(VT)) print(B) ``` 運行該示例打印原始 3×3 矩陣和直接從 SVD 元素重建的版本。 ``` [[1 2 3] [4 5 6] [7 8 9]] [[ 1\. 2\. 3.] [ 4\. 5\. 6.] [ 7\. 8\. 9.]] ``` ## 偽逆的 SVD 偽逆是矩形矩陣到矩形矩陣的矩陣逆的推廣,其中行和列的數量不相等。 在該方法的兩個獨立發現者或廣義逆之后,它也被稱為 Moore-Penrose 逆。 > 沒有為非正方形的矩陣定義矩陣求逆。 [...]當 A 的列數多于行數時,使用 pseudoinverse 求解線性方程式提供了許多可能的解決方案之一。 - 第 46 頁,[深度學習](http://amzn.to/2B3MsuU),2016 年。 偽逆表示為 A ^ +,其中 A 是被反轉的矩陣,+是上標。 使用 A 的奇異值分解計算偽逆: ``` A^+ = V . D^+ . U^T ``` 或者,沒有點符號: ``` A^+ = VD^+U^T ``` 其中 A ^ +是偽逆,D ^ +是對角矩陣 Sigma 的偽逆,U ^ T 是 U 的轉置。 我們可以通過 SVD 操作獲得 U 和 V. ``` A = U . Sigma . V^T ``` 可以通過從 Sigma 創建對角矩陣來計算 D ^ +,計算 Sigma 中每個非零元素的倒數,并且如果原始矩陣是矩形則采用轉置。 ``` s11, 0, 0 Sigma = ( 0, s22, 0) 0, 0, s33 ``` ``` 1/s11, 0, 0 D^+ = ( 0, 1/s22, 0) 0, 0, 1/s33 ``` 偽逆提供了一種求解線性回歸方程的方法,特別是當行數多于列時,通常就是這種情況。 NumPy 提供函數 pinv()來計算矩形矩陣的偽逆。 下面的示例定義了一個 4×2 矩陣并計算偽逆。 ``` # Pseudoinverse from numpy import array from numpy.linalg import pinv # define matrix A = array([ [0.1, 0.2], [0.3, 0.4], [0.5, 0.6], [0.7, 0.8]]) print(A) # calculate pseudoinverse B = pinv(A) print(B) ``` 首先運行示例打印定義的矩陣,然后打印計算的偽逆。 ``` [[ 0.1 0.2] [ 0.3 0.4] [ 0.5 0.6] [ 0.7 0.8]] [[ -1.00000000e+01 -5.00000000e+00 9.04289323e-15 5.00000000e+00] [ 8.50000000e+00 4.50000000e+00 5.00000000e-01 -3.50000000e+00]] ``` 我們可以通過 SVD 手動計算偽逆,并將結果與??pinv()函數進行比較。 首先,我們必須計算 SVD。接下來,我們必須計算 s 數組中每個值的倒數。然后可以將 s 數組轉換為具有添加的零行的對角矩陣,以使其成為矩形。最后,我們可以從元素中計算出偽逆。 具體實施是: ``` A^+ = V . D^+ . U^V ``` 下面列出了完整的示例。 ``` # Pseudoinverse via SVD from numpy import array from numpy.linalg import svd from numpy import zeros from numpy import diag # define matrix A = array([ [0.1, 0.2], [0.3, 0.4], [0.5, 0.6], [0.7, 0.8]]) print(A) # calculate svd U, s, VT = svd(A) # reciprocals of s d = 1.0 / s # create m x n D matrix D = zeros(A.shape) # populate D with n x n diagonal matrix D[:A.shape[1], :A.shape[1]] = diag(d) # calculate pseudoinverse B = VT.T.dot(D.T).dot(U.T) print(B) ``` 首先運行示例打印定義的矩形矩陣和與 pinv()函數匹配上述結果的偽逆。 ``` [[ 0.1 0.2] [ 0.3 0.4] [ 0.5 0.6] [ 0.7 0.8]] [[ -1.00000000e+01 -5.00000000e+00 9.04831765e-15 5.00000000e+00] [ 8.50000000e+00 4.50000000e+00 5.00000000e-01 -3.50000000e+00]] ``` ## 用于降維的 SVD SVD 的一種流行應用是降低尺寸。 具有大量特征的數據(例如,比觀察(行)更多的特征(列))可以減少到與預測問題最相關的較小特征子集。 結果是具有較低等級的矩陣,據說接近原始矩陣。 為此,我們可以對原始數據執行 SVD 操作,并在 Sigma 中選擇前 k 個最大奇異值。這些列可以從 Sigma 和從 V ^ T 中選擇的行中選擇。 然后可以重建原始向量 A 的近似 B. ``` B = U . Sigmak . V^Tk ``` 在自然語言處理中,該方法可以用于文檔中的單詞出現或單詞頻率的矩陣,并且被稱為潛在語義分析或潛在語義索引。 在實踐中,我們可以保留并使用名為 T 的數據的描述子集。這是矩陣或投影的密集摘要。 ``` T = U . Sigmak ``` 此外,可以計算該變換并將其應用于原始矩陣 A 以及其他類似的矩陣。 ``` T = V^Tk . A ``` 下面的示例演示了使用 SVD 減少數據。 首先定義 3×10 矩陣,列數多于行數。計算 SVD 并僅選擇前兩個特征。重新組合元素以給出原始矩陣的準確再現。最后,變換以兩種不同的方式計算。 ``` from numpy import array from numpy import diag from numpy import zeros from scipy.linalg import svd # define a matrix A = array([ [1,2,3,4,5,6,7,8,9,10], [11,12,13,14,15,16,17,18,19,20], [21,22,23,24,25,26,27,28,29,30]]) print(A) # Singular-value decomposition U, s, VT = svd(A) # create m x n Sigma matrix Sigma = zeros((A.shape[0], A.shape[1])) # populate Sigma with n x n diagonal matrix Sigma[:A.shape[0], :A.shape[0]] = diag(s) # select n_elements = 2 Sigma = Sigma[:, :n_elements] VT = VT[:n_elements, :] # reconstruct B = U.dot(Sigma.dot(VT)) print(B) # transform T = U.dot(Sigma) print(T) T = A.dot(VT.T) print(T) ``` 首先運行該示例打印定義的矩陣然后重建近似,然后是原始矩陣的兩個等效變換。 ``` [[ 1 2 3 4 5 6 7 8 9 10] [11 12 13 14 15 16 17 18 19 20] [21 22 23 24 25 26 27 28 29 30]] [[ 1\. 2\. 3\. 4\. 5\. 6\. 7\. 8\. 9\. 10.] [ 11\. 12\. 13\. 14\. 15\. 16\. 17\. 18\. 19\. 20.] [ 21\. 22\. 23\. 24\. 25\. 26\. 27\. 28\. 29\. 30.]] [[-18.52157747 6.47697214] [-49.81310011 1.91182038] [-81.10462276 -2.65333138]] [[-18.52157747 6.47697214] [-49.81310011 1.91182038] [-81.10462276 -2.65333138]] ``` scikit-learn 提供了一個直接實現此功能的 TruncatedSVD 類。 可以創建 TruncatedSVD 類,您必須在其中指定要選擇的所需要素或組件的數量,例如, 2.一旦創建,您可以通過調用 fit()函數來擬合變換(例如,計算 V ^ Tk),然后通過調用 transform()函數將其應用于原始矩陣。結果是上面稱為 T 的 A 的變換。 下面的示例演示了 TruncatedSVD 類。 ``` from numpy import array from sklearn.decomposition import TruncatedSVD # define array A = array([ [1,2,3,4,5,6,7,8,9,10], [11,12,13,14,15,16,17,18,19,20], [21,22,23,24,25,26,27,28,29,30]]) print(A) # svd svd = TruncatedSVD(n_components=2) svd.fit(A) result = svd.transform(A) print(result) ``` 首先運行示例打印定義的矩陣,然后打印矩陣的轉換版本。 我們可以看到值與上面手動計算的值匹配,除了某些值上的符號。考慮到所涉及的計算的性質以及所使用的底層庫和方法的差異,我們可以預期在符號方面存在一些不穩定性。只要對變換進行了重復訓練,這種符號的不穩定性在實踐中就不應成為問題。 ``` [[ 1 2 3 4 5 6 7 8 9 10] [11 12 13 14 15 16 17 18 19 20] [21 22 23 24 25 26 27 28 29 30]] [[ 18.52157747 6.47697214] [ 49.81310011 1.91182038] [ 81.10462276 -2.65333138]] ``` ## 擴展 本節列出了一些擴展您可能希望探索的教程的想法。 * 在您自己的數據上試驗 SVD 方法。 * 研究并列出了 SVD 在機器學習中的 10 個應用。 * 將 SVD 作為數據縮減技術應用于表格數據集。 如果你探索任何這些擴展,我很想知道。 ## 進一步閱讀 如果您希望深入了解,本節將提供有關該主題的更多資源。 ### 圖書 * 第 12 章,奇異值和 Jordan 分解,[線性代數和矩陣分析統計](http://amzn.to/2A9ceNv),2014。 * 第 4 章,奇異值分解和第 5 章,關于 SVD 的更多內容,[數值線性代數](http://amzn.to/2kjEF4S),1997。 * 第 2.4 節奇異值分解,[矩陣計算](http://amzn.to/2B9xnLD),2012。 * 第 7 章奇異值分解(SVD),[線性代數導論](http://amzn.to/2AZ7R8j),第 5 版,2016 年。 * 第 2.8 節奇異值分解,[深度學習](http://amzn.to/2B3MsuU),2016 年。 * 第 7.D 節極性分解和奇異值分解,[線性代數完成權](http://amzn.to/2BGuEqI),第三版,2015 年。 * 第 3 講奇異值分解,[數值線性代數](http://amzn.to/2BI9kRH),1997。 * 第 2.6 節奇異值分解,[數字秘籍:科學計算的藝術](http://amzn.to/2BezVEE),第三版,2007。 * 第 2.9 節 Moore-Penrose 偽逆,[深度學習](http://amzn.to/2B3MsuU),2016。 ### API * [numpy.linalg.svd()API](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.linalg.svd.html) * [numpy.matrix.H API](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.matrix.H.html) * [numpy.diag()API](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.diag.html) * [numpy.linalg.pinv()API](https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.linalg.pinv.html) 。 * [sklearn.decomposition.TruncatedSVD API](http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.TruncatedSVD.html) ### 用品 * 維基百科上的[矩陣分解](https://en.wikipedia.org/wiki/Matrix_decomposition) * [維基百科上的奇異值分解](https://en.wikipedia.org/wiki/Singular-value_decomposition) * [維基百科上的奇異值](https://en.wikipedia.org/wiki/Singular_value) * [維基百科上的 Moore-Penrose 逆](https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse) * [維基百科上的潛在語義分析](https://en.wikipedia.org/wiki/Latent_semantic_analysis) ## 摘要 在本教程中,您發現了奇異值分解方法,用于將矩陣分解為其組成元素。 具體來說,你學到了: * 奇異值分解是什么以及涉及什么。 * 如何計算 SVD 并從 SVD 元素重建矩形和方形矩陣。 * 如何使用 SVD 計算偽逆并執行降維。 你有任何問題嗎? 在下面的評論中提出您的問題,我會盡力回答。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看