logo
Loading...

Truncated SVD 與自行計算的 SVD 值不完全相同? - Cupoy

我嘗試比較 sklearn 的 TruncatedSVD 以及 np.linalg.svd 取前 k...

Truncated SVD 與自行計算的 SVD 值不完全相同?

2021/01/16 下午 04:04
計數方法詞向量實作介紹
Chi-Kang Su
觀看數:58
回答數:2
收藏數:0

我嘗試比較 sklearn 的 TruncatedSVD 以及 np.linalg.svd 取前 k 值的結果,發現正負號有所差異,且後續的值也有所不同。想請問導致這樣的原因為何? ```python svd = TruncatedSVD(n_components=140) sk_us = svd.fit_transform(co_matrix) print(sk_us) u, s, vh = np.linalg.svd(co_matrix) print(u[:, :140] @ np.diag(s[:140])) ``` ![image](http://kwassistfile.cupoy.com/000001770A2B6DF6000000216375706F795F72656C656173655155455354/1610598311753/large)

回答列表

  • 2021/01/17 上午 08:30
    Even Pan
    贊同數:0
    不贊同數:0
    留言數:0

    發現第一個collumn有不一樣的特性 ```python svd = TruncatedSVD(n_components=5) sk_us = svd.fit_transform(co_matrix) print(sk_us) u, s, vh = np.linalg.svd(co_matrix) svd_manual = u[:, :5] @ np.diag(s[:5]) print(svd_manual) abs(sk_us - svd_manual) <= 0.01 ``` ![image](http://kwassistfile.cupoy.com/000001770DBFA7F30000001A6375706F795F72656C65617365414E53/1610598311760/large) 看起來TruncatedSVD 的第一個column有經過糾正 Adjusts the columns of u and the rows of v such that the loadings in the columns in u that are largest in absolute value are always positive. https://github.com/scikit-learn/scikit-learn/blob/42aff4e2edd8e8887478f6ff1628f27de97be6a3/sklearn/utils/extmath.py#L504-L547

  • 2021/01/23 上午 02:23
    張維元 (WeiYuan)
    贊同數:0
    不贊同數:0
    留言數:0

    嗨,你好
    你的問題是什麼,可以多描述一點嗎?

    嗨,你好,我是維元,持續在不同的平台發表對 #資料科學、 #網頁開發 或 #軟體職涯 相關的文章。如果對於內文有疑問都歡迎與我們進一步的交流,都可以追蹤我的 Facebook技術部落格 ,也會不定時的舉辦分享活動,一起來玩玩吧 ヽ(●´∀`●)ノ