本文整理匯總了Python中scipy.linalg.svd方法的典型用法代碼示例。如果您正苦於以下問題:Python linalg.svd方法的具體用法?Python linalg.svd怎麽用?Python linalg.svd使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scipy.linalg
的用法示例。
在下文中一共展示了linalg.svd方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: flatten
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import svd [as 別名]
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
示例2: fit
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import svd [as 別名]
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
示例3: svd_log_det
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import svd [as 別名]
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))
示例4: __init__
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import svd [as 別名]
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)
示例5: test_svd_flip
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import svd [as 別名]
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)
示例6: transform
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import svd [as 別名]
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]
示例7: csvd
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import svd [as 別名]
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)
示例8: get_zca_whitening_principal_components_img
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import svd [as 別名]
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
示例9: truncated_svd
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import svd [as 別名]
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 #
####################
示例10: estimate_ld
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import svd [as 別名]
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
示例11: _column_covariances
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import svd [as 別名]
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
示例12: zca_whiten
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import svd [as 別名]
def zca_whiten(self, X):
"""
Perform ZCA whitening (aka Mahalanobis whitening).
Parameters
----------
X : array (M samples x D features)
data matrix.
Returns
-------
X : array (M samples x D features)
whitened data.
"""
# Covariance matrix
Sigma = np.cov(X.T)
# Singular value decomposition
U, S, V = svd(Sigma)
# Whitening constant to prevent division by zero
epsilon = 1e-5
# ZCA whitening matrix
W = np.dot(U, np.dot(np.diag(1.0 / np.sqrt(S + epsilon)), V))
# Apply whitening matrix
return np.dot(X, W)
示例13: fc_decomposition
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import svd [as 別名]
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
示例14: compute_von_neumann_entropy
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import svd [as 別名]
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)
示例15: split_truncate_theta
# 需要導入模塊: from scipy import linalg [as 別名]
# 或者: from scipy.linalg import svd [as 別名]
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