Linear Algebra, Mathematics, Math, Principal Component, Python.
return np.sign(M) * np.where(S>0, S, 0) def _svd(M, rank): return fbpca.pca(M, k=min(rank, np.min(M.shape)), raw=True) def norm_op(M): return _svd(M, 1)[1][0] def svd_reconstruct(M, rank, min_sv): u, s, v = _svd(M, rank) s -= min_sv nnz = (s > 0).sum() return u[:,:nnz] @ np.diag(s[:nnz]) @ v[:nnz],