LoginSignup
1
1

More than 5 years have passed since last update.

Pythonで特異値分解(SVD)、低ランク近似(LRA)する

Last updated at Posted at 2017-01-14

高速化したいならLapackとかPythonから使うことないと思うので、そういう科学技術計算をcudaとかCとかC++でやる人の答え合わせ程度に使っていただけると

と思ってメモ

ソースコード

low_rank_approximation.py
import numpy as np
from scipy import linalg

def low_rank_approximation(a,rank):
    u, s, v = linalg.svd(a)
    ur = u[:, :rank]
    sr = np.matrix(linalg.diagsvd(s[:rank], rank,rank))
    vr = v[:rank, :]
    return np.asarray(ur*sr*vr)

A = np.array([[1,2,3],[4,5,6],[7,8,9]])
print A
B = low_rank_approximation(A,1)
print B

# python low_rank_approximation.py
# [[1 2 3]
#  [4 5 6]
#  [7 8 9]]
# [[ 1.73621779  2.07174246  2.40726714]
#  [ 4.2071528   5.02018649  5.83322018]
#  [ 6.6780878   7.96863051  9.25917322]]

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1