我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用scipy.linalg.eigvalsh()。
def distance_riemann(A, B): """Riemannian distance between two covariance matrices A and B. .. math:: d = {\left( \sum_i \log(\lambda_i)^2 \\right)}^{-1/2} where :math:`\lambda_i` are the joint eigenvalues of A and B :param A: First covariance matrix :param B: Second covariance matrix :returns: Riemannian distance between A and B """ return numpy.sqrt((numpy.log(eigvalsh(A, B))**2).sum())
def evidence_approximation(self, X, T, alpha=1e-2, beta=1e-2, tol=1e-3, max_iter=int(1e2)): n_basis = self.n_basis n_sample = X.shape[0] Phi = self.nonlinear_transformation(X) PhiTPhi = np.outer(Phi, Phi) if Phi.ndim < 2 else Phi.T.dot(Phi) lamb = linalg.eigvalsh(PhiTPhi) gamma = np.sum(lamb / (lamb + alpha)) for iteration in range(max_iter): post_cov = linalg.inv(alpha * np.eye(n_basis) + beta * PhiTPhi) post_mean = beta * post_cov.dot(Phi.T.dot(T)) alpha = gamma / linalg.norm(post_mean)**2 beta = (n_sample - gamma) / linalg.norm(T - Phi.dot(post_mean))**2 # FIXME gamma_new = np.sum(lamb / (lamb + alpha)) if (linalg.norm(gamma_new - gamma) / linalg.norm(gamma) < tol): break gamma = gamma_new self.mean = post_mean self.prior_precision = alpha self.noise_precision = beta return self
def _validate_covars(covars, covariance_type, n_components): """Do basic checks on matrix covariance sizes and values.""" from scipy import linalg if covariance_type == 'spherical': if len(covars) != n_components: raise ValueError("'spherical' covars have length n_components") elif np.any(covars <= 0): raise ValueError("'spherical' covars must be non-negative") elif covariance_type == 'tied': if covars.shape[0] != covars.shape[1]: raise ValueError("'tied' covars must have shape (n_dim, n_dim)") elif (not np.allclose(covars, covars.T) or np.any(linalg.eigvalsh(covars) <= 0)): raise ValueError("'tied' covars must be symmetric, " "positive-definite") elif covariance_type == 'diag': if len(covars.shape) != 2: raise ValueError("'diag' covars must have shape " "(n_components, n_dim)") elif np.any(covars <= 0): raise ValueError("'diag' covars must be non-negative") elif covariance_type == 'full': if len(covars.shape) != 3: raise ValueError("'full' covars must have shape " "(n_components, n_dim, n_dim)") elif covars.shape[1] != covars.shape[2]: raise ValueError("'full' covars must have shape " "(n_components, n_dim, n_dim)") for n, cv in enumerate(covars): if (not np.allclose(cv, cv.T) or np.any(linalg.eigvalsh(cv) <= 0)): raise ValueError("component %d of 'full' covars must be " "symmetric, positive-definite" % n) else: raise ValueError("covariance_type must be one of " + "'spherical', 'tied', 'diag', 'full'")