Python scipy.linalg.svd() Examples
The following are 30
code examples of scipy.linalg.svd().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
scipy.linalg
, or try the search function
.
Example #1
Source File: zca.py From zca with GNU General Public License v3.0 | 7 votes |
def fit(self, X, y=None): """Compute the mean, whitening and dewhitening matrices. Parameters ---------- X : array-like with shape [n_samples, n_features] The data used to compute the mean, whitening and dewhitening matrices. """ X = check_array(X, accept_sparse=None, copy=self.copy, ensure_2d=True) X = as_float_array(X, copy=self.copy) self.mean_ = X.mean(axis=0) X_ = X - self.mean_ cov = np.dot(X_.T, X_) / (X_.shape[0]-1) U, S, _ = linalg.svd(cov) s = np.sqrt(S.clip(self.regularization)) s_inv = np.diag(1./s) s = np.diag(s) self.whiten_ = np.dot(np.dot(U, s_inv), U.T) self.dewhiten_ = np.dot(np.dot(U, s), U.T) return self
Example #2
Source File: instrument_model.py From isofit with Apache License 2.0 | 6 votes |
def _column_covariances(X, uniformity_thresh): """.""" Xvert = _high_frequency_vert(X, sigma=4.0) Xvertp = _high_frequency_vert(X, sigma=3.0) models = [] use_C = [] for i in range(X.shape[2]): xsub = Xvert[:, :, i] xsubp = Xvertp[:, :, i] mu = xsub.mean(axis=0) dists = s.sqrt(pow((xsub - mu), 2).sum(axis=1)) distsp = s.sqrt(pow((xsubp - mu), 2).sum(axis=1)) thresh = _percentile(dists, 95.0) uthresh = dists * uniformity_thresh #use = s.logical_and(dists<thresh, abs(dists-distsp) < uthresh) use = dists < thresh C = s.cov(xsub[use, :], rowvar=False) [U, V, D] = svd(C) V[V < 1e-8] = 1e-8 C = U.dot(s.diagflat(V)).dot(D) models.append(C) use_C.append(use) return s.array(models), Xvert, Xvertp, s.array(use_C).T
Example #3
Source File: linalg.py From revrand with Apache License 2.0 | 6 votes |
def svd_log_det(s): """ Compute the log of the determinant of :math:`A`, given its singular values from an SVD factorisation (i.e. :code:`s` from :code:`U, s, Ut = svd(A)`). Parameters ---------- s: ndarray the singular values from an SVD decomposition Examples -------- >>> A = np.array([[ 2, -1, 0], ... [-1, 2, -1], ... [ 0, -1, 2]]) >>> _, s, _ = np.linalg.svd(A) >>> np.isclose(svd_log_det(s), np.log(np.linalg.det(A))) True """ return np.sum(np.log(s))
Example #4
Source File: point_cloud.py From FRIDA with MIT License | 6 votes |
def flatten(self, ind): ''' Transform the set of points so that the subset of markers given as argument is as close as flat (wrt z-axis) as possible. Parameters ---------- ind : list of bools Lists of marker indices that should be all in the same subspace ''' # Transform references to indices if needed ind = [self.key2ind(s) for s in ind] # center point cloud around the group of indices centroid = self.X[:,ind].mean(axis=1, keepdims=True) X_centered = self.X - centroid # The rotation is given by left matrix of SVD U,S,V = la.svd(X_centered[:,ind], full_matrices=False) self.X = np.dot(U.T, X_centered) + centroid
Example #5
Source File: nonlin.py From lambda-packs with MIT License | 6 votes |
def __init__(self, alpha=None, reduction_method='restart', max_rank=None): GenericBroyden.__init__(self) self.alpha = alpha self.Gm = None if max_rank is None: max_rank = np.inf self.max_rank = max_rank if isinstance(reduction_method, str): reduce_params = () else: reduce_params = reduction_method[1:] reduction_method = reduction_method[0] reduce_params = (max_rank - 1,) + reduce_params if reduction_method == 'svd': self._reduce = lambda: self.Gm.svd_reduce(*reduce_params) elif reduction_method == 'simple': self._reduce = lambda: self.Gm.simple_reduce(*reduce_params) elif reduction_method == 'restart': self._reduce = lambda: self.Gm.restart_reduce(*reduce_params) else: raise ValueError("Unknown rank reduction method '%s'" % reduction_method)
Example #6
Source File: nonlin.py From Computable with MIT License | 6 votes |
def __init__(self, alpha=None, reduction_method='restart', max_rank=None): GenericBroyden.__init__(self) self.alpha = alpha self.Gm = None if max_rank is None: max_rank = np.inf self.max_rank = max_rank if isinstance(reduction_method, str): reduce_params = () else: reduce_params = reduction_method[1:] reduction_method = reduction_method[0] reduce_params = (max_rank - 1,) + reduce_params if reduction_method == 'svd': self._reduce = lambda: self.Gm.svd_reduce(*reduce_params) elif reduction_method == 'simple': self._reduce = lambda: self.Gm.simple_reduce(*reduce_params) elif reduction_method == 'restart': self._reduce = lambda: self.Gm.restart_reduce(*reduce_params) else: raise ValueError("Unknown rank reduction method '%s'" % reduction_method)
Example #7
Source File: test_extmath.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_svd_flip(): # Check that svd_flip works in both situations, and reconstructs input. rs = np.random.RandomState(1999) n_samples = 20 n_features = 10 X = rs.randn(n_samples, n_features) # Check matrix reconstruction U, S, V = linalg.svd(X, full_matrices=False) U1, V1 = svd_flip(U, V, u_based_decision=False) assert_almost_equal(np.dot(U1 * S, V1), X, decimal=6) # Check transposed matrix reconstruction XT = X.T U, S, V = linalg.svd(XT, full_matrices=False) U2, V2 = svd_flip(U, V, u_based_decision=True) assert_almost_equal(np.dot(U2 * S, V2), XT, decimal=6) # Check that different flip methods are equivalent under reconstruction U_flip1, V_flip1 = svd_flip(U, V, u_based_decision=True) assert_almost_equal(np.dot(U_flip1 * S, V_flip1), XT, decimal=6) U_flip2, V_flip2 = svd_flip(U, V, u_based_decision=False) assert_almost_equal(np.dot(U_flip2 * S, V_flip2), XT, decimal=6)
Example #8
Source File: discriminant_analysis.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def transform(self, X): """Project data to maximize class separation. Parameters ---------- X : array-like, shape (n_samples, n_features) Input data. Returns ------- X_new : array, shape (n_samples, n_components) Transformed data. """ if self.solver == 'lsqr': raise NotImplementedError("transform not implemented for 'lsqr' " "solver (use 'svd' or 'eigen').") check_is_fitted(self, ['xbar_', 'scalings_'], all_or_any=any) X = check_array(X) if self.solver == 'svd': X_new = np.dot(X - self.xbar_, self.scalings_) elif self.solver == 'eigen': X_new = np.dot(X, self.scalings_) return X_new[:, :self._max_components]
Example #9
Source File: audio_tools.py From tools with BSD 3-Clause "New" or "Revised" License | 6 votes |
def csvd(arr): """ Do the complex SVD of a 2D array, returning real valued U, S, VT http://stemblab.github.io/complex-svd/ """ C_r = arr.real C_i = arr.imag block_x = C_r.shape[0] block_y = C_r.shape[1] K = np.zeros((2 * block_x, 2 * block_y)) # Upper left K[:block_x, :block_y] = C_r # Lower left K[:block_x, block_y:] = C_i # Upper right K[block_x:, :block_y] = -C_i # Lower right K[block_x:, block_y:] = C_r return svd(K, full_matrices=False)
Example #10
Source File: prepro.py From LapSRN-tensorflow with Apache License 2.0 | 6 votes |
def get_zca_whitening_principal_components_img(X): """Return the ZCA whitening principal components matrix. Parameters ----------- x : numpy array Batch of image with dimension of [n_example, row, col, channel] (default). """ flatX = np.reshape(X, (X.shape[0], X.shape[1] * X.shape[2] * X.shape[3])) print("zca : computing sigma ..") sigma = np.dot(flatX.T, flatX) / flatX.shape[0] print("zca : computing U, S and V ..") U, S, V = linalg.svd(sigma) print("zca : computing principal components ..") principal_components = np.dot(np.dot(U, np.diag(1. / np.sqrt(S + 10e-7))), U.T) return principal_components
Example #11
Source File: nonlin.py From GraphicDesignPatternByPython with MIT License | 6 votes |
def __init__(self, alpha=None, reduction_method='restart', max_rank=None): GenericBroyden.__init__(self) self.alpha = alpha self.Gm = None if max_rank is None: max_rank = np.inf self.max_rank = max_rank if isinstance(reduction_method, str): reduce_params = () else: reduce_params = reduction_method[1:] reduction_method = reduction_method[0] reduce_params = (max_rank - 1,) + reduce_params if reduction_method == 'svd': self._reduce = lambda: self.Gm.svd_reduce(*reduce_params) elif reduction_method == 'simple': self._reduce = lambda: self.Gm.simple_reduce(*reduce_params) elif reduction_method == 'restart': self._reduce = lambda: self.Gm.restart_reduce(*reduce_params) else: raise ValueError("Unknown rank reduction method '%s'" % reduction_method)
Example #12
Source File: extmath.py From mpnum with BSD 3-Clause "New" or "Revised" License | 6 votes |
def truncated_svd(A, k): """Compute the truncated SVD of the matrix `A` i.e. the `k` largest singular values as well as the corresponding singular vectors. It might return less singular values/vectors, if one dimension of `A` is smaller than `k`. In the background it performs a full SVD. Therefore, it might be inefficient when `k` is much smaller than the dimensions of `A`. :param A: A real or complex matrix :param k: Number of singular values/vectors to compute :returns: u, s, v, where u: left-singular vectors s: singular values in descending order v: right-singular vectors """ u, s, v = np.linalg.svd(A) k_prime = min(k, len(s)) return u[:, :k_prime], s[:k_prime], v[:k_prime] #################### # Randomized SVD # ####################
Example #13
Source File: ldref.py From focus with GNU General Public License v3.0 | 6 votes |
def estimate_ld(self, snps=None, adjust=0.1, return_eigvals=False): G = self.get_geno(snps) n, p = G.shape col_mean = np.nanmean(G, axis=0) # impute missing with column mean inds = np.where(np.isnan(G)) G[inds] = np.take(col_mean, inds[1]) if return_eigvals: G = (G - np.mean(G, axis=0)) / np.std(G, axis=0) _, S, V = lin.svd(G, full_matrices=True) # adjust D = np.full(p, adjust) D[:len(S)] = D[:len(S)] + (S**2 / n) return np.dot(V.T * D, V), D else: return np.corrcoef(G.T) + np.eye(p) * adjust
Example #14
Source File: separable_approx.py From gputools with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _separable_series2(h, N=1): """ finds separable approximations to the 2d function 2d h returns res = (hx, hy)[N] s.t. h \approx sum_i outer(res[i,0],res[i,1]) """ if min(h.shape)<N: raise ValueError("smallest dimension of h is smaller than approximation order! (%s < %s)"%(min(h.shape),N)) U, S, V = linalg.svd(h) hx = [-U[:, n] * np.sqrt(S[n]) for n in range(N)] hy = [-V[n, :] * np.sqrt(S[n]) for n in range(N)] return np.array(list(zip(hx, hy)))
Example #15
Source File: kdl_template.py From SciPy2015 with BSD 3-Clause "New" or "Revised" License | 5 votes |
def np_ortho(shape, random_state): """ Builds a theano variable filled with orthonormal random values """ g = random_state.randn(*shape) o_g = linalg.svd(g)[0] return o_g.astype(theano.config.floatX)
Example #16
Source File: transformations.py From qiskit-terra with Apache License 2.0 | 5 votes |
def _choi_to_kraus(data, input_dim, output_dim, atol=ATOL_DEFAULT): """Transform Choi representation to Kraus representation.""" # Check if hermitian matrix if is_hermitian_matrix(data, atol=atol): # Get eigen-decomposition of Choi-matrix # This should be a call to la.eigh, but there is an OpenBlas # threading issue that is causing segfaults. # Need schur here since la.eig does not # guarentee orthogonality in degenerate subspaces w, v = la.schur(data, output='complex') w = w.diagonal().real # Check eigenvalues are non-negative if len(w[w < -atol]) == 0: # CP-map Kraus representation kraus = [] for val, vec in zip(w, v.T): if abs(val) > atol: k = np.sqrt(val) * vec.reshape( (output_dim, input_dim), order='F') kraus.append(k) # If we are converting a zero matrix, we need to return a Kraus set # with a single zero-element Kraus matrix if not kraus: kraus.append(np.zeros((output_dim, input_dim), dtype=complex)) return kraus, None # Non-CP-map generalized Kraus representation mat_u, svals, mat_vh = la.svd(data) kraus_l = [] kraus_r = [] for val, vec_l, vec_r in zip(svals, mat_u.T, mat_vh.conj()): kraus_l.append( np.sqrt(val) * vec_l.reshape((output_dim, input_dim), order='F')) kraus_r.append( np.sqrt(val) * vec_r.reshape((output_dim, input_dim), order='F')) return kraus_l, kraus_r
Example #17
Source File: utils.py From qiskit-terra with Apache License 2.0 | 5 votes |
def _funm_svd(matrix, func): """Apply real scalar function to singular values of a matrix. Args: matrix (array_like): (N, N) Matrix at which to evaluate the function. func (callable): Callable object that evaluates a scalar function f. Returns: ndarray: funm (N, N) Value of the matrix function specified by func evaluated at `A`. """ unitary1, singular_values, unitary2 = la.svd(matrix) diag_func_singular = np.diag(func(singular_values)) return unitary1.dot(diag_func_singular).dot(unitary2)
Example #18
Source File: prone.py From CogDL-TensorFlow with MIT License | 5 votes |
def _get_embedding_dense(self, matrix, dimension): # get dense embedding via SVD t1 = time.time() U, s, Vh = linalg.svd( matrix, full_matrices=False, check_finite=False, overwrite_a=True ) U = np.array(U) U = U[:, :dimension] s = s[:dimension] s = np.sqrt(s) U = U * s U = preprocessing.normalize(U, "l2") print("densesvd time", time.time() - t1) return U
Example #19
Source File: acc_fc.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def fc_decomposition(model, args): W = model.arg_params[args.layer+'_weight'].asnumpy() b = model.arg_params[args.layer+'_bias'].asnumpy() W = W.reshape((W.shape[0],-1)) b = b.reshape((b.shape[0],-1)) u, s, v = LA.svd(W, full_matrices=False) s = np.diag(s) t = u.dot(s.dot(v)) rk = args.K P = u[:,:rk] Q = s[:rk,:rk].dot(v[:rk,:]) name1 = args.layer + '_red' name2 = args.layer + '_rec' def sym_handle(data, node): W1, W2 = Q, P sym1 = mx.symbol.FullyConnected(data=data, num_hidden=W1.shape[0], no_bias=True, name=name1) sym2 = mx.symbol.FullyConnected(data=sym1, num_hidden=W2.shape[0], no_bias=False, name=name2) return sym2 def arg_handle(arg_shape_dic, arg_params): W1, W2 = Q, P W1 = W1.reshape(arg_shape_dic[name1+'_weight']) weight1 = mx.ndarray.array(W1) W2 = W2.reshape(arg_shape_dic[name2+'_weight']) b2 = b.reshape(arg_shape_dic[name2+'_bias']) weight2 = mx.ndarray.array(W2) bias2 = mx.ndarray.array(b2) arg_params[name1 + '_weight'] = weight1 arg_params[name2 + '_weight'] = weight2 arg_params[name2 + '_bias'] = bias2 new_model = utils.replace_conv_layer(args.layer, model, sym_handle, arg_handle) return new_model
Example #20
Source File: prone.py From CogDL-TensorFlow with MIT License | 5 votes |
def _get_embedding_rand(self, matrix): # Sparse randomized tSVD for fast embedding t1 = time.time() l = matrix.shape[0] smat = sp.csc_matrix(matrix) # convert to sparse CSC format print("svd sparse", smat.data.shape[0] * 1.0 / l ** 2) U, Sigma, VT = randomized_svd( smat, n_components=self.dimension, n_iter=5, random_state=None ) U = U * np.sqrt(Sigma) U = preprocessing.normalize(U, "l2") print("sparsesvd time", time.time() - t1) return U
Example #21
Source File: vne.py From PHATE with GNU General Public License v2.0 | 5 votes |
def compute_von_neumann_entropy(data, t_max=100): """ Determines the Von Neumann entropy of data at varying matrix powers. The user should select a value of t around the "knee" of the entropy curve. Parameters ---------- t_max : int, default: 100 Maximum value of t to test Returns ------- entropy : array, shape=[t_max] The entropy of the diffusion affinities for each value of t Examples -------- >>> import numpy as np >>> import phate >>> X = np.eye(10) >>> X[0,0] = 5 >>> X[3,2] = 4 >>> h = phate.vne.compute_von_neumann_entropy(X) >>> phate.vne.find_knee_point(h) 23 """ _, eigenvalues, _ = svd(data) entropy = [] eigenvalues_t = np.copy(eigenvalues) for _ in range(t_max): prob = eigenvalues_t / np.sum(eigenvalues_t) prob = prob + np.finfo(float).eps entropy.append(-np.sum(prob * np.log(prob))) eigenvalues_t = eigenvalues_t * eigenvalues entropy = np.array(entropy) return np.array(entropy)
Example #22
Source File: test_lapack.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_sgesdd_lwork_bug_workaround(): # Test that SGESDD lwork is sufficiently large for LAPACK. # # This checks that workaround around an apparent LAPACK bug # actually works. cf. gh-5401 # # xslow: requires 1GB+ of memory p = subprocess.Popen([sys.executable, '-c', 'import numpy as np; ' 'from scipy.linalg import svd; ' 'a = np.zeros([9537, 9537], dtype=np.float32); ' 'svd(a)'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # Check if it an error occurred within 5 sec; the computation can # take substantially longer, and we will not wait for it to finish for j in range(50): time.sleep(0.1) if p.poll() is not None: returncode = p.returncode break else: # Didn't exit in time -- probably entered computation. The # error is raised before entering computation, so things are # probably OK. returncode = 0 p.terminate() assert_equal(returncode, 0, "Code apparently failed: " + p.stdout.read())
Example #23
Source File: a_mps.py From tenpy with GNU General Public License v3.0 | 5 votes |
def split_truncate_theta(theta, chi_max, eps): """Split and truncate a two-site wave function in mixed canonical form. Split a two-site wave function as follows:: vL --(theta)-- vR => vL --(A)--diag(S)--(B)-- vR | | | | i j i j Afterwards, truncate in the new leg (labeled ``vC``). Parameters ---------- theta : np.Array[ndim=4] Two-site wave function in mixed canonical form, with legs ``vL, i, j, vR``. chi_max : int Maximum number of singular values to keep eps : float Discard any singular values smaller than that. Returns ------- A : np.Array[ndim=3] Left-canonical matrix on site i, with legs ``vL, i, vC`` S : np.Array[ndim=1] Singular/Schmidt values. B : np.Array[ndim=3] Right-canonical matrix on site j, with legs ``vC, j, vR`` """ chivL, dL, dR, chivR = theta.shape theta = np.reshape(theta, [chivL * dL, dR * chivR]) X, Y, Z = svd(theta, full_matrices=False) # truncate chivC = min(chi_max, np.sum(Y > eps)) piv = np.argsort(Y)[::-1][:chivC] # keep the largest `chivC` singular values X, Y, Z = X[:, piv], Y[piv], Z[piv, :] # renormalize S = Y / np.linalg.norm(Y) # == Y/sqrt(sum(Y**2)) # split legs of X and Z A = np.reshape(X, [chivL, dL, chivC]) B = np.reshape(Z, [chivC, dR, chivR]) return A, S, B
Example #24
Source File: test_arpack.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_svd_LM_zeros_matrix_gh_3452(): # Regression test for a github issue. # https://github.com/scipy/scipy/issues/3452 # Note that for complex dype the size of this matrix is too small for k=1. n, m, k = 4, 2, 1 A = np.zeros((n, m)) U, s, VH = svds(A, k) # Check some generic properties of svd. _check_svds(A, k, U, s, VH) # Check that the singular values are zero. assert_array_equal(s, 0)
Example #25
Source File: test_arpack.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def test_svd_LM_zeros_matrix(): # Check that svds can deal with matrices containing only zeros. k = 1 for n, m in (3, 4), (4, 4), (4, 3): for t in float, complex: A = np.zeros((n, m), dtype=t) U, s, VH = svds(A, k) # Check some generic properties of svd. _check_svds(A, k, U, s, VH) # Check that the singular values are zero. assert_array_equal(s, 0)
Example #26
Source File: test_arpack.py From GraphicDesignPatternByPython with MIT License | 5 votes |
def sorted_svd(m, k, which='LM'): # Compute svd of a dense matrix m, and return singular vectors/values # sorted. if isspmatrix(m): m = m.todense() u, s, vh = svd(m) if which == 'LM': ii = np.argsort(s)[-k:] elif which == 'SM': ii = np.argsort(s)[:k] else: raise ValueError("unknown which=%r" % (which,)) return u[:, ii], s[ii], vh[ii]
Example #27
Source File: ma_pca.py From tedana with GNU Lesser General Public License v2.1 | 5 votes |
def _icatb_svd(data, n_comps=None): """ Run Singular Value Decomposition (SVD) on input data and extracts the given number of components (n_comps). Parameters ---------- data : array The data to compute SVD for n_comps : int Number of PCA components to be kept Returns ------- V : 2D array Eigenvectors from SVD Lambda : float Eigenvalues """ if not n_comps: n_comps = np.min((data.shape[0], data.shape[1])) _, Lambda, vh = svd(data, full_matrices=False) # Sort eigen vectors in Ascending order V = vh.T Lambda = Lambda / np.sqrt(data.shape[0] - 1) # Whitening (sklearn) inds = np.argsort(np.power(Lambda, 2)) Lambda = np.power(Lambda, 2)[inds] V = V[:, inds] sumAll = np.sum(Lambda) # Return only the extracted components V = V[:, (V.shape[1] - n_comps):] Lambda = Lambda[Lambda.shape[0] - n_comps:] sumUsed = np.sum(Lambda) retained = (sumUsed / sumAll) * 100 LGR.debug('{ret}% of non-zero components retained'.format(ret=retained)) return V, Lambda
Example #28
Source File: cifar10.py From vat_chainer with MIT License | 5 votes |
def ZCA(data, reg=1e-6): mean = np.mean(data, axis=0) mdata = data - mean sigma = np.dot(mdata.T, mdata) / mdata.shape[0] U, S, V = linalg.svd(sigma) components = np.dot(np.dot(U, np.diag(1 / np.sqrt(S) + reg)), U.T) whiten = np.dot(data - mean, components.T) return components, mean, whiten
Example #29
Source File: prone.py From nodevectors with MIT License | 5 votes |
def svd_dense(matrix, dimension): """ dense embedding via linalg SVD """ U, s, Vh = linalg.svd(matrix, full_matrices=False, check_finite=False, overwrite_a=True) U = np.array(U) U = U[:, :dimension] s = s[:dimension] s = np.sqrt(s) U = U * s U = preprocessing.normalize(U, "l2") return U
Example #30
Source File: subsampling.py From Effective-Quadratures with GNU Lesser General Public License v2.1 | 5 votes |
def __init__(self, subsampling_algorithm): self.subsampling_algorithm = subsampling_algorithm if self.subsampling_algorithm == 'qr': self.algorithm = lambda A, k : get_qr_column_pivoting(A, k) elif self.subsampling_algorithm == 'svd': self.algorithm = lambda A, k : get_svd_subset_selection(A, k) elif self.subsampling_algorithm == 'newton': self.algorithm = lambda A, k : get_newton_determinant_maximization(A, k) elif self.subsampling_algorithm == 'random': self.algorithm = 0 #np.random.choice(int(m), m_refined, replace=False) elif self.subsampling_algorithm == None: self.algorithm = lambda A, k: _get_all_pivots(A, k)